Skip to content

Commit

Permalink
Merge pull request #15 from BrandwatchLtd/topics
Browse files Browse the repository at this point in the history
Topics
  • Loading branch information
jessicabowden authored Jul 15, 2016
2 parents c5d15e6 + f44c7b9 commit 3f4888d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions bwresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ def get_mentions(self, max_pages=None, **kwargs):
params = self._fill_mentions_params(kwargs)
all_mentions = []

while max_pages == None or params["pageSize"] < max_pages:
next_mentions = self._get_mentions_page(params, params["pageSize"])
while max_pages == None or params["page"] < max_pages:
next_mentions = self._get_mentions_page(params, params["page"])

if len(next_mentions) > 0:
all_mentions += next_mentions

if self.console_report:
print("Page " + str(params["pageSize"]) + " of " + self.resource_type + " " + kwargs["name"] + " retrieved")
print("Page " + str(params["page"]) + " of " + self.resource_type + " " + kwargs["name"] + " retrieved")
else:
break

params["pageSize"] += 1
params["page"] += 1

if self.console_report:
print(str(len(all_mentions)) + " mentions downloaded")
Expand All @@ -202,9 +202,12 @@ def num_mentions(self, **kwargs):
A count of the mentions in a given timeframe.
"""
params = self._fill_mentions_params(kwargs)
params["pageSize"] = 1
return self.project.get(endpoint="data/mentions/count", params=params)

def get_topics(self, **kwargs):
params = self._fill_mentions_params(kwargs)
return self.project.get(endpoint="data/volume/topics/queries", params=params)["topics"]

def _fill_mentions_params(self, data):
if "name" not in data:
raise KeyError("Must specify query or group name", data)
Expand All @@ -218,7 +221,8 @@ def _fill_mentions_params(self, data):
filled["startDate"] = data["startDate"]
filled["endDate"] = data["endDate"] if "endDate" in data else (
datetime.date.today() + datetime.timedelta(days=1)).isoformat()
filled["pageSize"] = data["pageSize"] if "pageSize" in data else 1
filled["pageSize"] = data["pageSize"] if "pageSize" in data else 5000
filled["page"] = data["page"] if "page" in data else 0

for param in data:
setting = self._name_to_id(param, data[param])
Expand All @@ -230,7 +234,7 @@ def _fill_mentions_params(self, data):
return filled

def _get_mentions_page(self, params, page):
params["pageSize"] = page
params["page"] = page
mentions = self.project.get(endpoint="data/mentions/fulltext", params=params)

if "errors" in mentions:
Expand Down

0 comments on commit 3f4888d

Please sign in to comment.