From b7211b7c754a75a0b2c765bb2176d4b607e2cd8f Mon Sep 17 00:00:00 2001 From: Eriks Karls Date: Wed, 26 Feb 2020 12:09:03 +0200 Subject: [PATCH] Notification parsing and deleting --- erepublik/access_points.py | 18 ++++++++++++- erepublik/citizen.py | 54 +++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/erepublik/access_points.py b/erepublik/access_points.py index 1066d45..cca400e 100644 --- a/erepublik/access_points.py +++ b/erepublik/access_points.py @@ -243,7 +243,7 @@ class ErepublikCompanyAPI(CitizenBaseAPI): data = dict(action_type="workOvertime", _token=self.token) return self.post("{}/economy/workOvertime".format(self.url), data=data) - def _post_economy_job_market_apply(self, citizen: int, salary: int) -> Response: + def _post_economy_job_market_apply(self, citizen: int, salary: float) -> Response: data = dict(_token=self.token, citizenId=citizen, salary=salary) return self.post("{}/economy/job-market-apply".format(self.url), data=data) @@ -424,6 +424,10 @@ class ErepublikMilitaryAPI(CitizenBaseAPI): data = dict(sideId=side_id, battleId=battle_id, _token=self.token, battleZoneId=zone_id) return self.post("{}/military/fight-shooot/{}".format(self.url, battle_id), data=data) + def _post_fight_deploy_deploy_report_data(self, deployment_id: int): + data = dict(_token=self.token, deploymentId=deployment_id) + return self.post(f"{self.url}/military/fightDeploy-deployReportData", json=data) + class ErepublikPoliticsAPI(CitizenBaseAPI): def _get_candidate_party(self, party_slug: str) -> Response: @@ -528,6 +532,18 @@ class ErepublikProfileAPI(CitizenBaseAPI): data = {"_token": self.token, "delete_alerts[]": notification_ids, "deleteAllAlerts": "1", "delete": "Delete"} return self.post("{}/main/messages-alerts/1".format(self.url), data=data) + def _post_main_notifications_ajax_community(self, notification_ids: List[int], page: int = 1) -> Response: + data = {"_token": self.token, "delete_alerts[]": notification_ids} + return self.post("{}/main/notificationsAjax/community/{}".format(self.url, page), data=data) + + def _post_main_notifications_ajax_system(self, notification_ids: List[int], page: int = 1) -> Response: + data = {"_token": self.token, "delete_alerts[]": notification_ids} + return self.post("{}/main/notificationsAjax/system/{}".format(self.url, page), data=data) + + def _post_main_notifications_ajax_report(self, notification_ids: List[int], page: int = 1) -> Response: + data = {"_token": self.token, "delete_alerts[]": notification_ids} + return self.post("{}/main/notificationsAjax/report/{}".format(self.url, page), data=data) + def _post_main_messages_compose(self, subject: str, body: str, citizens: List[int]) -> Response: url_pk = 0 if len(citizens) > 1 else str(citizens[0]) data = dict(citizen_name=",".join([str(x) for x in citizens]), diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 2c4a4ab..cfa1bd9 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -2067,6 +2067,47 @@ class CitizenSocial(BaseCitizen): for resident in resp["widgets"]["residents"]["residents"]: self.add_friend(resident["citizenId"]) + def get_community_notifications(self, page: int = 1) -> List[Dict[str, Any]]: + return self._get_main_notifications_ajax_community(page).json().get('alertsList', []) + + def get_system_notifications(self, page: int = 1) -> List[Dict[str, Any]]: + return self._get_main_notifications_ajax_system(page).json().get('alertsList', []) + + def get_report_notifications(self, page: int = 1) -> List[Dict[str, Any]]: + return self._get_main_notifications_ajax_report(page).json().get('alertsList', []) + + def delete_community_notification(self, notification_id: Union[int, List[int]]): + if not isinstance(notification_id, list): + notification_id = [notification_id] + self._post_main_notifications_ajax_community(notification_id) + + def delete_system_notification(self, notification_id: Union[int, List[int]]): + if not isinstance(notification_id, list): + notification_id = [notification_id] + self._post_main_notifications_ajax_system(notification_id) + + def delete_report_notification(self, notification_id: Union[int, List[int]]): + if not isinstance(notification_id, list): + notification_id = [notification_id] + self._post_main_notifications_ajax_report(notification_id) + + def get_all_notifications(self, page: int = 1) -> Dict[str, List[Dict[str, Any]]]: + return dict(community=self.get_community_notifications(), + system=self.get_system_notifications(page), + report=self.get_report_notifications(page)) + + def delete_all_notifications(self): + for kind, notifications in self.get_all_notifications(): + if notifications: + if kind == "community": + self.delete_community_notification([n['id'] for n in notifications]) + elif kind == "report": + self.delete_report_notification([n['id'] for n in notifications]) + elif kind == "system": + self.delete_system_notification([n['id'] for n in notifications]) + else: + self.report_error(f"Unsupported notification kind: \"{kind}\"!") + class Citizen(CitizenMilitary, CitizenAnniversary, CitizenEconomy, CitizenSocial, CitizenPolitics): debug: bool = False @@ -2208,21 +2249,8 @@ class Citizen(CitizenMilitary, CitizenAnniversary, CitizenEconomy, CitizenSocial if pps: self.details.next_pp.append(int(pps.group(1))) - def parse_notifications(self, page: int = 1) -> list: - community = self._get_main_notifications_ajax_community(page).json() - system = self._get_main_notifications_ajax_system(page).json() - return community['alertsList'] + system['alertsList'] - def delete_notifications(self): - response = self._get_main_notifications_ajax_community().json() - while response['totalAlerts']: - self._post_main_messages_alert([_['id'] for _ in response['alertList']]) - response = self._get_main_notifications_ajax_community().json() - response = self._get_main_notifications_ajax_system().json() - while response['totalAlerts']: - self._post_main_messages_alert([_['id'] for _ in response['alertList']]) - response = self._get_main_notifications_ajax_system().json() def collect_weekly_reward(self): self.update_weekly_challenge()