Some notifications are still being displayed the old way

This commit is contained in:
Eriks Karls 2020-02-19 18:52:40 +02:00
parent f73f2b7b9f
commit fd1880c50f
3 changed files with 62 additions and 17 deletions

View File

@ -144,6 +144,7 @@ class Citizen(CitizenAPI):
return
html = resp.text
self.check_for_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:
@ -218,6 +219,8 @@ class Citizen(CitizenAPI):
if self._errors_in_response(response):
self.get_csrf_token()
self.get(url, **kwargs)
else:
self.check_for_medals(response.text)
self.r = response
return response
@ -255,11 +258,60 @@ class Citizen(CitizenAPI):
elif json:
json.update({"_token": self.token})
response = self.post(url, data=data, json=json, **kwargs)
else:
self.check_for_medals(response.text)
self.r = response
return response
def check_for_new_medals(self):
def check_for_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)
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]
if (title, reward) not in data:
data[(title, reward)] = {'about': about, 'kind': title, 'reward': reward, "count": 1,
"currency": currency}
else:
data[(title, reward)]['count'] += 1
except AttributeError:
continue
if data:
msgs = ["{count} x {kind}, totaling {} {currency}\n"
"{about}".format(d["count"] * d["reward"], **d) for d in data.values()]
msgs = "\n".join(msgs)
self.telegram.report_medal(msgs)
self.write_log(f"Found awards:\n{msgs}")
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)
def check_for_notification_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 notifications.get('notifications', []):
@ -288,9 +340,8 @@ class Citizen(CitizenAPI):
data[(title, reward)]['count'] += 1
self._post_main_global_alerts_close(medal.get('id'))
if data:
msgs = ["{count} x {kind}, totaling {} {currency}\n"
"{about}".format(d["count"] * d["reward"], **d) for d in data.values()]
msgs = ["{count} x {kind},"
" totaling {} {currency}".format(d["count"] * d["reward"], **d) for d in data.values()]
msgs = "\n".join(msgs)
self.telegram.report_medal(msgs)
@ -298,14 +349,6 @@ 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)
def update_all(self, force_update=False):
# Do full update max every 5 min
if good_timedelta(self.__last_full_update, timedelta(minutes=5)) > self.now and not force_update:
@ -319,14 +362,14 @@ class Citizen(CitizenAPI):
self.update_money()
self.update_weekly_challenge()
self.send_state_update()
self.check_for_new_medals()
self.check_for_notification_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.check_for_notification_medals()
self._get_main()
return
ugly_js = re.search(r'"promotions":\s*(\[{?.*?}?])', html).group(1)

View File

@ -1285,6 +1285,8 @@ class TelegramBot:
def send_message(self, message: str) -> bool:
self.__queue.append(message)
if not self.__initialized:
if self._last_time < utils.now():
self.__queue.clear()
return True
self._threads = [t for t in self._threads if t.is_alive()]
self._next_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=1))

View File

@ -399,7 +399,7 @@ def slugify(value, allow_unicode=False) -> str:
def calculate_hit(strength: float, rang: int, tp: bool, elite: bool, ne: bool, booster: int = 0,
weapon: int = 200, is_deploy: bool = False) -> Decimal:
weapon: int = 200, is_deploy: bool = False) -> float:
dec = 3 if is_deploy else 0
base_str = (1 + Decimal(str(round(strength, 3))) / 400)
base_rnk = (1 + Decimal(str(rang / 5)))
@ -420,7 +420,7 @@ def calculate_hit(strength: float, rang: int, tp: bool, elite: bool, ne: bool, b
def get_ground_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_patriot: bool = False,
booster: int = 0, weapon_power: int = 200) -> Decimal:
booster: int = 0, weapon_power: int = 200) -> float:
r = requests.get(f"https://www.erepublik.com/en/main/citizen-profile-json/{citizen_id}").json()
rang = r['military']['militaryData']['ground']['rankNumber']
strength = r['military']['militaryData']['ground']['strength']
@ -432,7 +432,7 @@ def get_ground_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_
def get_air_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_patriot: bool = False, booster: int = 0,
weapon_power: int = 0) -> Decimal:
weapon_power: int = 0) -> float:
r = requests.get(f"https://www.erepublik.com/en/main/citizen-profile-json/{citizen_id}").json()
rang = r['military']['militaryData']['aircraft']['rankNumber']
elite = r['citizenAttributes']['level'] > 100