From d6a0d5a704a475fcc7a2aa48988d7bcdb3065480 Mon Sep 17 00:00:00 2001 From: Eriks Karls Date: Thu, 27 Feb 2020 18:23:59 +0200 Subject: [PATCH] BaseCitizen should have email and password at initialization to be able to login --- erepublik/__init__.py | 2 +- erepublik/citizen.py | 101 ++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 50 deletions(-) diff --git a/erepublik/__init__.py b/erepublik/__init__.py index 4c09eaf..1c08c80 100644 --- a/erepublik/__init__.py +++ b/erepublik/__init__.py @@ -5,7 +5,7 @@ __author__ = """Eriks Karls""" __email__ = 'eriks@72.lv' __version__ = '0.20.0' -__commit_id__ = "772c09a" +__commit_id__ = "22dc18d" from erepublik import classes, utils from erepublik.citizen import Citizen diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 748e4e4..a267470 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -42,7 +42,7 @@ class BaseCitizen(CitizenAPI): logged_in: bool = False commit_id: str = "" - def __init__(self): + def __init__(self, email: str = "", password: str = ""): super().__init__() self.commit_id = utils.COMMIT_ID self.config = Config() @@ -54,6 +54,9 @@ class BaseCitizen(CitizenAPI): self.stop_threads = Event() self.telegram = TelegramBot(stop_event=self.stop_threads) + self.config.email = email + self.config.password = password + def get_csrf_token(self): """ get_csrf_token is the function which logs you in, and updates csrf tokens @@ -359,54 +362,6 @@ class BaseCitizen(CitizenAPI): self.food["total"] = sum([self.food[q] * utils.FOOD_ENERGY[q] for q in utils.FOOD_ENERGY]) return inventory - def _check_response_for_medals(self, html: str): - new_medals = re.findall(r'(
.*?
\s*
)', - 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"

New Achievement

.*?(.*?)

.*?" - r"achievement_recieved.*?(.*?).*?" - r"
", 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) - if self.config.telegram: - 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"

Congratulations, you have reached experience level (\d+)

", html) - if levelup: - level = levelup.group(1) - msg = f"Level up! Current level {level}" - self.write_log(msg) - if self.config.telegram: - self.telegram.report_medal(f"Level *{level}*") - self.reporter.report_action("LEVEL_UP", value=level) - def write_log(self, *args, **kwargs): if self.config.interactive: utils.write_interactive_log(*args, **kwargs) @@ -561,6 +516,54 @@ class BaseCitizen(CitizenAPI): """ return utils.now() + def _check_response_for_medals(self, html: str): + new_medals = re.findall(r'(
.*?
\s*
)', + 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"

New Achievement

.*?(.*?)

.*?" + r"achievement_recieved.*?(.*?).*?" + r"
", 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) + if self.config.telegram: + 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"

Congratulations, you have reached experience level (\d+)

", html) + if levelup: + level = levelup.group(1) + msg = f"Level up! Current level {level}" + self.write_log(msg) + if self.config.telegram: + self.telegram.report_medal(f"Level *{level}*") + self.reporter.report_action("LEVEL_UP", value=level) + def _travel(self, country_id: int, region_id: int = 0) -> Response: data = { "toCountryId": country_id,