Skip to content

Commit

Permalink
Merge pull request #328 from sysblok/dev
Browse files Browse the repository at this point in the history
cleanup of old cmds, refactor FB client
  • Loading branch information
alexeyqu authored Jul 16, 2024
2 parents a9b11e3 + 8035e90 commit a6ae150
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 982 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
42 changes: 0 additions & 42 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ def init_handlers(self):
self.manager_reply_handler("trello_board_state_job"),
"получить сводку о состоянии доски",
)
self.add_manager_handler(
"get_editorial_board_stats",
CommandCategories.STATS,
self.manager_reply_handler("editorial_board_stats_job"),
"получить статистику изменений за неделю",
)
self.add_manager_handler(
"get_editorial_board_visual_stats",
CommandCategories.STATS,
Expand All @@ -106,12 +100,6 @@ def init_handlers(self):
self.admin_broadcast_handler("publication_plans_job"),
"рассылка сводки о публикуемых на неделе постах",
)
self.add_manager_handler(
"get_publication_plans",
CommandCategories.SUMMARY,
self.manager_reply_handler("publication_plans_job"),
"получить сводку о публикуемых на неделе постах",
)
self.add_manager_handler(
"fill_posts_list",
CommandCategories.REGISTRY,
Expand All @@ -127,12 +115,6 @@ def init_handlers(self):
),
"заполнить реестр постов из Focalboard (пока не работает)",
)
self.add_admin_handler(
"send_editorial_report",
CommandCategories.BROADCAST,
self.admin_broadcast_handler("editorial_report_job"),
"рассылка сводки по результатам редакторского созвона",
)
self.add_admin_handler(
"hr_acquisition",
CommandCategories.HR,
Expand All @@ -157,30 +139,12 @@ def init_handlers(self):
self.admin_broadcast_handler("hr_status_job"),
"разослать статус по работе hr (по новичкам и участинкам на испытательном)",
)
self.add_manager_handler(
"get_editorial_report",
CommandCategories.SUMMARY,
self.manager_reply_handler("editorial_report_job"),
"получить сводку по результатам редакторского созвона",
)
self.add_manager_handler(
"create_folders_for_illustrators",
CommandCategories.REGISTRY,
self.manager_reply_handler("create_folders_for_illustrators_job"),
"создать папки для иллюстраторов",
)
self.add_manager_handler(
"get_illustrative_report_members",
CommandCategories.SUMMARY,
self.manager_reply_handler("illustrative_report_members_job"),
"получить сводку с папками для иллюстраторов (группы по иллюстраторам)",
)
self.add_manager_handler(
"get_illustrative_report_columns",
CommandCategories.SUMMARY,
self.manager_reply_handler("illustrative_report_columns_job"),
"получить сводку с папками для иллюстраторов (группы по колонкам)",
)
self.add_manager_handler(
"get_tasks_report",
CommandCategories.SUMMARY,
Expand All @@ -199,12 +163,6 @@ def init_handlers(self):
direct_message_only(handlers.get_tasks_report_focalboard),
"получить список задач из Focalboard",
)
self.add_manager_handler(
"get_articles_arts",
CommandCategories.SUMMARY,
self.manager_reply_handler("trello_get_articles_arts_job"),
"получить карточки по тегу искусство",
)
self.add_manager_handler(
"get_articles_rubric",
CommandCategories.SUMMARY,
Expand Down
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
5 changes: 0 additions & 5 deletions src/jobs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
from .db_fetch_curators_sheet_job import DBFetchCuratorsSheetJob
from .db_fetch_strings_sheet_job import DBFetchStringsSheetJob
from .db_fetch_team_sheet_job import DBFetchTeamSheetJob
from .editorial_board_stats_job import EditorialBoardStatsJob
from .editorial_board_visual_stats_job import EditorialBoardVisualStatsJob
from .editorial_report_job import EditorialReportJob
from .fb_analytics_report_job import FBAnalyticsReportJob
from .fill_posts_list_focalboard_job import FillPostsListFocalboardJob
from .fill_posts_list_job import FillPostsListJob
Expand All @@ -28,8 +26,6 @@
from .hr_get_members_without_telegram_job import HRGetMembersWithoutTelegramJob
from .hr_status_job import HRStatusJob
from .ig_analytics_report_job import IGAnalyticsReportJob
from .illustrative_report_columns_job import IllustrativeReportColumnsJob
from .illustrative_report_members_job import IllustrativeReportMembersJob
from .publication_plans_job import PublicationPlansJob
from .sample_job import SampleJob
from .send_reminders_job import SendRemindersJob
Expand All @@ -39,6 +35,5 @@
from .tg_analytics_report_job import TgAnalyticsReportJob
from .trello_board_state_job import TrelloBoardStateJob
from .trello_board_state_notifications_job import TrelloBoardStateNotificationsJob
from .trello_get_articles_arts_job import TrelloGetArticlesArtsJob
from .trello_get_articles_rubric_job import TrelloGetArticlesRubricJob
from .vk_analytics_report_job import VkAnalyticsReportJob
Loading

0 comments on commit a6ae150

Please sign in to comment.