BrandwatchLtd / bcr-api

Python Client Library for the Brandwatch Consumer Research API
MIT License
21 stars 23 forks source link

Not able to create new brandwatch query #38

Open ukrcbreddy1 opened 9 months ago

ukrcbreddy1 commented 9 months ago

Hello,

We are using this package to create a query over brandwatch. Previously we were able to do that with the same code base but right now it seem to be failing with the following error message, not sure if anything have been changed from Brandwatch side and if this API needs to be updated.

Tried the same code with 1.4.0, 1.5.1 version of bcr-api

Code:

project = BWProject(username=YOUR_ACCOUNT, project=YOUR_PROJECT, token_path=YOUR_TOKEN_PATH)
queries = BWQueries(project)

corporation_booleanQuery = '("Sasol" OR "Rio Tinto" OR "DRDGold" OR "Durban Roodepoort Deep Limited") AND (site:cer.org.za)'

queries.upload(name = 'testing 0' ,
             booleanQuery = corporation_booleanQuery,
             startDate = '2019-01-01',
             languages = ['en'],
             contentSources = ['news'])

Error message:

05:22:23 ERROR: There was an error with this request: 
https://api.brandwatch.com/query-validation
{"booleanQuery": "(\"Sasol\" OR \"Rio Tinto\" OR \"DRDGold\" OR \"Durban Roodepoort Deep Limited\") AND (site:cer.org.za)", "language": ["en"]}
[{'code': 0, 'message': "Unrecognized field 'language'"}]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/tmp/ipykernel_6175/1702768516.py in <module>
     17              startDate = query_start_date,
     18              # languages = languages,
---> 19              contentSources = ['news'])
     20     corporation_queries.append(f'{backfill_portfolio_name} Client Onboarding Mentions ' + str(idx)) # entities addition
     21     # corporation_queries.append(f'Corporation Mentions ' + str(idx))  # domains addition

/opt/conda/lib/python3.7/site-packages/bcr_api/bwresources.py in upload(self, create_only, modify_only, **kwargs)
    296             The uploaded query information in a dictionary of the form {query1name: query1id}
    297         """
--> 298         return self.upload_all([kwargs], create_only, modify_only)
    299 
    300     def upload_all(self, data_list, create_only=False, modify_only=False):

/opt/conda/lib/python3.7/site-packages/bcr_api/bwresources.py in upload_all(self, data_list, create_only, modify_only)
    320 
    321         queries = super(BWQueries, self).upload_all(
--> 322             data_list, create_only=False, modify_only=False
    323         )
    324 

/opt/conda/lib/python3.7/site-packages/bcr_api/bwresources.py in upload_all(self, data_list, create_only, modify_only)
    162         for data in data_list:
    163             # eventually make _fill_data() a BWResource func
--> 164             filled_data = self._fill_data(data)
    165             name = data["name"]
    166 

/opt/conda/lib/python3.7/site-packages/bcr_api/bwresources.py in _fill_data(self, data)
    506         # validating the query search - comment this out to skip validation
    507         self.project.validate_query_search(
--> 508             booleanQuery=filled["booleanQuery"], language=["en"]
    509         )
    510         return json.dumps(filled)

/opt/conda/lib/python3.7/site-packages/bcr_api/bwproject.py in validate_query_search(self, **kwargs)
    138 
    139         valid_search = self.request(
--> 140             verb=requests.post, address="query-validation", data=json.dumps(kwargs)
    141         )
    142         return valid_search

/opt/conda/lib/python3.7/site-packages/bcr_api/bwproject.py in request(self, verb, address, params, data)
    182             access_token=self.token,
    183             params=params,
--> 184             data=data,
    185         )
    186 

/opt/conda/lib/python3.7/site-packages/bcr_api/bwproject.py in bare_request(self, verb, address_root, address_suffix, access_token, params, data)
    238                     )
    239                 )
--> 240                 raise RuntimeError(response.json()["errors"])
    241 
    242         logger.debug(response.url)

RuntimeError: [{'code': 0, 'message': "Unrecognized field 'language'"}]
cloner-gan commented 8 months ago

I am having the same issue. "Unrecognized field 'language'" with default language settings. image

ukrcbreddy1 commented 7 months ago

Hey @cloner-gan , I believe the work around we did was.. we have used Brandwatch UI to create the queries manually. (Atleast that's what I remember). As we had to only create one query, it was fine in our case.

cloner-gan commented 7 months ago

Thanks @ukrcbreddy1, unfortunately I need the ability to add new queries. Glad you found a workaround though :)