Skip to content

Commit

Permalink
feat: use _make_graph_api_call for all methods (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pochemuta authored Jul 2, 2024
1 parent 9a023c2 commit b2269d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[settings]
known_third_party = apiclient,bs4,conftest,dateutil,deepdiff,facebook,fakes,freezegun,googleapiclient,matplotlib,numpy,oauth2client,pytest,pytest_report,requests,schedule,setuptools,sheetfu,sqlalchemy,telegram,telethon,utils,vk_api
known_third_party = apiclient,bs4,conftest,dateutil,deepdiff,facebook,fakes,freezegun,googleapiclient,grpc,matplotlib,numpy,oauth2client,opentelemetry,pytest,pytest_report,requests,schedule,setuptools,sheetfu,sqlalchemy,telegram,telethon,utils,vk_api
67 changes: 38 additions & 29 deletions src/facebook/facebook_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from .facebook_objects import FacebookPage

logger = logging.getLogger(__name__)
BASE_URL = 'https://graph.facebook.com'
API_VERSION = 'v19.0'
BASE_URL = "https://graph.facebook.com"
API_VERSION = "v19.0"


class FacebookClient(Singleton):
Expand All @@ -36,33 +36,35 @@ def _update_from_config(self):
self._page_id = self._facebook_config["page_id"]

def _make_graph_api_call(self, uri: str, params: dict) -> dict:
params['access_token'] = self._facebook_config["token"]
params["access_token"] = self._facebook_config["token"]
response = requests.get(
'/'.join([BASE_URL, API_VERSION, uri]) + '?' +
'&'.join(f"{key}={value}" for key, value in params.items()))
"/".join([BASE_URL, API_VERSION, uri])
+ "?"
+ "&".join(f"{key}={value}" for key, value in params.items())
)
return response.json()

def get_page(self) -> FacebookPage:
"""
Get facebook page
"""
page_dict = self._make_graph_api_call(str(self._page_id), {
'fields': 'link,name,followers_count,fan_count'
})
page_dict = self._make_graph_api_call(
str(self._page_id), {"fields": "link,name,followers_count,fan_count"}
)
return FacebookPage.from_dict(page_dict)

def get_new_posts_count(self, since: datetime, until: datetime) -> int:
"""
Get the number of new posts for the period.
"""
result = self._make_graph_api_call(
str(self._page_id) + '/published_posts',
str(self._page_id) + "/published_posts",
{
'summary': 'total_count',
'since': int(datetime.timestamp(since)),
'until': int(datetime.timestamp(until)),
'limit': 0,
}
"summary": "total_count",
"since": int(datetime.timestamp(since)),
"until": int(datetime.timestamp(until)),
"limit": 0,
},
)
return result["summary"]["total_count"]

Expand Down Expand Up @@ -139,12 +141,15 @@ def get_new_fan_count(
return result

def _get_all_batches(
self, connection_name: str, since: datetime, until: datetime, **args
self, connection_name: str, since: datetime, until: datetime, **kwargs
) -> List[dict]:
result = []
args["since"] = since
args["until"] = until
page = self._api_client.get_connections(self._page_id, connection_name, **args)
params = {
"since": int(datetime.timestamp(since)),
"until": int(datetime.timestamp(until)),
}
params.update(kwargs)
page = self._make_graph_api_call(f"{self._page_id}/{connection_name}", params)
result += page["data"]
# process next
result += self._iterate_over_pages(connection_name, since, until, page, True)
Expand All @@ -157,19 +162,17 @@ def _iterate_over_pages(
connection_name: str,
since: datetime,
until: datetime,
previous_page: str,
previous_page: dict,
go_next: bool,
) -> List[dict]:
result = []
current_page = previous_page
while True:
direction_tag = "previous"
if go_next:
direction_tag = "next"
next = current_page.get("paging", {}).get(direction_tag)
if not next:
direction_tag = "previous" if not go_next else "next"
next_url = current_page.get("paging", {}).get(direction_tag)
if not next_url:
break
args = parse_qs(urlparse(next).query)
args = parse_qs(urlparse(next_url).query)
if go_next:
page_since = args.get("since")
if (
Expand All @@ -186,11 +189,17 @@ def _iterate_over_pages(
< since
):
break
args.pop("access_token", None)
current_page = self._api_client.get_connections(
self._page_id, connection_name, **args
args = {
key: value[0] for key, value in args.items() if key != "access_token"
}
current_page = self._make_graph_api_call(
f"{self._page_id}/{connection_name}", args
)
result += current_page["data"]
if "data" in current_page:
result += current_page["data"]
else:
logger.error(f"Error in pagination: {current_page}")
break
return result

@staticmethod
Expand Down

0 comments on commit b2269d3

Please sign in to comment.