From 22c2a0ffd266fb164b65b59d61a5bbeea3f17445 Mon Sep 17 00:00:00 2001 From: Eriks K Date: Wed, 21 Oct 2020 14:45:29 +0300 Subject: [PATCH] New Features! Dump session to file! Load session from file! --- .gitignore | 1 + erepublik/citizen.py | 59 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e314fbb..7dbdb7b 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,4 @@ ENV/ debug/ log/ docs/ +*dump.json diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 399f662..64a1ae9 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -403,6 +403,41 @@ class BaseCitizen(access_points.CitizenAPI): return_set.add(constants.COUNTRIES[country_data['id']]) return return_set + def dump_instance(self): + filename = f"{self.__class__.__name__}__dump.json" + with open(filename, 'w') as f: + utils.json.dump(dict(email=self.config.email, password=self.config.password, + cookies=self._req.cookies.get_dict(), token=self.token, + user_agent=self._req.headers.get("User-Agent")), f, cls=classes.MyJSONEncoder) + self.write_log(f"Session saved to: '{filename}'") + + @classmethod + def load_from_dump(cls, dump_name: str): + with open(dump_name) as f: + data = utils.json.load(f) + player = cls(data['email'], data['password']) + player._req.cookies.update(data['cookies']) + player._req.headers.update({"User-Agent": data['user_agent']}) + player._resume_session() + return player + + def _resume_session(self): + resp = self._req.get(self.url) + re_name_id = re.search(r'', resp.text) + if re_name_id: + self.name = re_name_id.group(2) + self.details.citizen_id = re_name_id.group(1) + self.write_log(f"Resumed as: {self.name}") + if re.search('
', resp.text): + self.restricted_ip = True + self.report_error("eRepublik has blacklisted IP. Limited functionality!", True) + + self.logged_in = True + self.get_csrf_token() + else: + self._login() + def __str__(self) -> str: return f"Citizen {self.name}" @@ -1347,8 +1382,8 @@ class CitizenMilitary(CitizenTravel): all_battles[battle_data.get('id')] = classes.Battle(battle_data) old_all_battles = self.all_battles self.all_battles = all_battles - for battle in old_all_battles.values(): - utils._clear_up_battle_memory(battle) + # for battle in old_all_battles.values(): + # utils._clear_up_battle_memory(battle) def get_battle_for_war(self, war_id: int) -> Optional[classes.Battle]: self.update_war_info() @@ -2003,16 +2038,16 @@ class CitizenPolitics(BaseCitizen): self._report_action('POLITIC_PARTY_PRESIDENT', 'Applied for party president elections') return self._get_candidate_party(self.politics.party_slug) - def get_country_president_election_result(self, country: constants.Country, year: int, month: int) -> Dict[ - str, int]: + def get_country_president_election_result( + self, country: constants.Country, year: int, month: int + ) -> Dict[str, int]: timestamp = int(constants.erep_tz.localize(datetime(year, month, 5)).timestamp()) resp = self._get_presidential_elections(country.id, timestamp) candidates = re.findall(r'class="candidate_info">(.*?)', resp.text, re.S | re.M) ret = {} for candidate in candidates: - name = re.search( - r'', - candidate) + name = re.search(r'', candidate) name = name.group(1) votes = re.search(r'(\d+) votes', candidate).group(1) ret.update({name: int(votes)}) @@ -2272,6 +2307,16 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade if auto_login: self.login() + @classmethod + def load_from_dump(cls, *args, **kwargs): + with open(f"{cls.__name__}__dump.json") as f: + data = utils.json.load(f) + player = cls(data['email'], data['password'], False) + player._req.cookies.update(data['cookies']) + player._req.headers.update({"User-Agent": data['user_agent']}) + player._resume_session() + return player + def config_setup(self, **kwargs): self.config.reset() for key, value in kwargs.items():