New notifications API

This commit is contained in:
Eriks Karls 2020-02-18 20:11:34 +02:00
parent 2fd317153f
commit f6433908b4
2 changed files with 39 additions and 35 deletions

View File

@ -144,7 +144,6 @@ class Citizen(CitizenAPI):
return
html = resp.text
self.check_for_new_medals(html)
re_token = re.search(r'var csrfToken = \'(\w{32})\'', html)
re_login_token = re.search(r'<input type="hidden" id="_token" name="_token" value="(\w{32})">', html)
if re_token:
@ -219,8 +218,6 @@ class Citizen(CitizenAPI):
if self._errors_in_response(response):
self.get_csrf_token()
self.get(url, **kwargs)
else:
self.check_for_new_medals(response.text)
self.r = response
return response
@ -258,40 +255,38 @@ class Citizen(CitizenAPI):
elif json:
json.update({"_token": self.token})
response = self.post(url, data=data, json=json, **kwargs)
else:
self.check_for_new_medals(response.text)
self.r = response
return response
def check_for_new_medals(self, html: str):
new_medals = re.findall(r'(<div class="home_reward reward achievement">.*?<div class="bottom"></div>\s*</div>)',
html, re.M | re.S | re.I)
def check_for_new_medals(self):
notifications = self._get_main_citizen_daily_assistant().json()
data: Dict[Tuple[str, Union[float, str]], Dict[str, Union[int, str, float]]] = {}
for medal in new_medals:
try:
info = re.search(r"<h3>New Achievement</h3>.*?<p.*?>(.*?)</p>.*?"
r"achievement_recieved.*?<strong>(.*?)</strong>.*?"
r"<div title=\"(.*?)\">", medal, re.M | re.S)
about = info.group(1).strip()
title = info.group(2).strip()
award_id = re.search(r'"wall_enable_alerts_(\d+)', medal)
if award_id:
self._post_main_wall_post_automatic(**{'message': title, 'awardId': award_id.group(1)})
reward, currency = info.group(3).strip().split(" ")
while not isinstance(reward, float):
try:
reward = float(reward)
except ValueError:
reward = reward[:-1]
for medal in notifications.get('notifications', []):
if medal.get('details', {}).get('type') == "citizenAchievement":
params = medal.get('details', {}).get('achievement')
about = medal.get('details').get('description')
title = medal.get('title')
# award_id = re.search(r'"wall_enable_alerts_(\d+)', medal)
# if award_id:
# self._post_main_wall_post_automatic(**{'message': title, 'awardId': award_id.group(1)})
if params.get('ccValue'):
reward = params.get('ccValue')
currency = "Currency"
elif params.get('goldValue'):
reward = params.get('goldValue')
currency = "Gold"
else:
reward = params.get('energyValue')
currency = "Energy"
if (title, reward) not in data:
data[(title, reward)] = {'about': about, 'kind': title, 'reward': reward, "count": 1,
"currency": currency}
"currency": currency, "params": params}
else:
data[(title, reward)]['count'] += 1
except AttributeError:
continue
self._post_main_global_alerts_close(medal.get('id'))
if data:
msgs = ["{count} x {kind}, totaling {} {currency}\n"
@ -303,13 +298,13 @@ class Citizen(CitizenAPI):
for info in data.values():
self.reporter.report_action("NEW_MEDAL", info)
levelup = re.search(r"<p>Congratulations, you have reached experience <strong>level (\d+)</strong></p>", html)
if levelup:
level = levelup.group(1)
msg = f"Level up! Current level {level}"
self.write_log(msg)
self.telegram.report_medal(f"Level *{level}*")
self.reporter.report_action("LEVEL_UP", value=level)
# levelup = re.search(r"<p>Congratulations, you have reached experience <strong>level (\d+)</strong></p>", html)
# if levelup:
# level = levelup.group(1)
# msg = f"Level up! Current level {level}"
# self.write_log(msg)
# self.telegram.report_medal(f"Level *{level}*")
# self.reporter.report_action("LEVEL_UP", value=level)
def update_all(self, force_update=False):
# Do full update max every 5 min
@ -324,12 +319,14 @@ class Citizen(CitizenAPI):
self.update_money()
self.update_weekly_challenge()
self.send_state_update()
self.check_for_new_medals()
def update_citizen_info(self, html: str = None):
"""
Gets main page and updates most information about player
"""
if html is None:
self.check_for_new_medals()
self._get_main()
return
ugly_js = re.search(r'"promotions":\s*(\[{?.*?}?])', html).group(1)

View File

@ -524,9 +524,12 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
def _get_main_citizen_profile_json(self, player_id: int) -> Response:
return self.get("{}/main/citizen-profile-json/{}".format(self.url, player_id))
def _get_main_citizen_daily_assistant(self) -> Response:
def _get_main_citizen_notifications(self) -> Response:
return self.get("{}/main/citizenDailyAssistant".format(self.url))
def _get_main_citizen_daily_assistant(self) -> Response:
return self.get("{}/main/citizenNotifications".format(self.url))
def _get_main_city_data_residents(self, city: int, page: int = 1, params: Mapping[str, Any] = None) -> Response:
if params is None:
params = {}
@ -673,6 +676,10 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
data = dict(_token=self.token, articleId=article_id, amount=amount)
return self.post("{}/main/donate-article".format(self.url), data=data)
def _post_main_global_alerts_close(self, alert_id: int) -> Response:
data = dict(_token=self.token, alert_id=alert_id)
return self.post("{}/main/global-alerts/close".format(self.url), data=data)
def _post_delete_message(self, msg_id: list) -> Response:
data = {"_token": self.token, "delete_message[]": msg_id}
return self.post("{}/main/messages-delete".format(self.url), data)