diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/ispoof/data.py b/ispoof/data.py new file mode 100644 index 0000000..76ecf20 --- /dev/null +++ b/ispoof/data.py @@ -0,0 +1,13 @@ +from ispoof.objects import Pokemon, Raid +from ispoof.lists.playerhistory import PlayerHistory +from sqlalchemy.engine import Engine +from sqlalchemy.exc import ArgumentError + + +def initialize_database(engine: Engine): + try: + Pokemon.metadata.create_all(engine) + Raid.metadata.create_all(engine) + PlayerHistory.metadata.create_all(engine) + except ArgumentError: + print("Exception") diff --git a/ispoof/lists/playerhistory.py b/ispoof/lists/playerhistory.py new file mode 100644 index 0000000..1145add --- /dev/null +++ b/ispoof/lists/playerhistory.py @@ -0,0 +1,39 @@ +import sqlalchemy.exc +from geopy.geocoders import Nominatim +from ..core.location import Location +from sqlalchemy.orm import declarative_base +from sqlalchemy import Column, DateTime, PrimaryKeyConstraint +from sqlalchemy.orm import Session +from datetime import datetime, timedelta +from math import ceil + +Base = declarative_base() + + +class PlayerHistory(Base): + __tablename__ = "player_history" + + time = Column(DateTime) + location = Column(Location) + PrimaryKeyConstraint(time, name="history_pk") + + def __init__(self, engine=None): + self.engine = engine + + def get_last_activity(self): + time, location = None, None + with Session(self.engine) as session: + result = session.query(PlayerHistory).order_by(PlayerHistory.time.desc()).first() + if result: + time, location = result.time, result.location + session.commit() + + return time, location + + def add_activity(self, location: Location): + with Session(self.engine) as session: + self.time = datetime.now() + self.location = location + session.add(self) + session.commit() + return True diff --git a/ispoof/lists/pokemonlist.py b/ispoof/lists/pokemonlist.py index ed837ae..7763182 100644 --- a/ispoof/lists/pokemonlist.py +++ b/ispoof/lists/pokemonlist.py @@ -7,7 +7,8 @@ from typing import List from sqlalchemy import desc from datetime import datetime, timedelta -class PokemonList(): + +class PokemonList: def __init__(self, engine: Engine): self.pokemons = None self.engine = engine diff --git a/ispoof/objects/object.py b/ispoof/objects/object.py deleted file mode 100644 index b17b829..0000000 --- a/ispoof/objects/object.py +++ /dev/null @@ -1,9 +0,0 @@ -class Object(): - def __init__(self, location=None): - self.location = location - - def distance_to(self, other): - return self.location.distance(other) - - def cooldown_to(self, other): - return self.location.get_cooldown(other) + 1 \ No newline at end of file diff --git a/ispoof/objects/player.py b/ispoof/objects/player.py index 3644b04..7eb047b 100644 --- a/ispoof/objects/player.py +++ b/ispoof/objects/player.py @@ -1,26 +1,61 @@ from geopy.geocoders import Nominatim from ..core.location import Location -from .object import Object +from ..lists.playerhistory import PlayerHistory +from sqlalchemy.orm import Session +from datetime import datetime, timedelta +from math import ceil -class Player(Object): - def __init__(self): - super().__init__() - self.current_cooldown = 0 + +class Player(): + + def __init__(self, engine, location=None, cooldown=0): + self.location = location + self.cooldown = cooldown + self.history = PlayerHistory(engine=engine) def set_location_with_query(self, query): loc = Nominatim(user_agent="GetLoc") get_loc = loc.geocode(query) self.location = Location(get_loc.latitude, get_loc.longitude) - - def set_location(self, location, did_activity): - next_cd = self.cooldown_to(location) - if did_activity: - if next_cd + self.current_cooldown >= 120: - self.current_cooldown = 120 - else: - self.current_cooldown += next_cd + + def get_last_activity(self): + return self.history.get_last_activity() + + def set_location(self, location: Location): self.location = location + def do_activity(self, location: Location): + return self.history.add_activity(location) + def get_location(self): return self.location + + def distance_to(self, other): + return self.location.distance(other) + + def get_cooldown(self, location): + time, history_location = self.get_last_activity() + if time and history_location: + cooldown = history_location.get_cooldown(location) + current_cooldown = ceil((time + timedelta(minutes=cooldown) - datetime.now()).total_seconds() / 60) + if current_cooldown < 0: + current_cooldown = 0 + else: + current_cooldown = 0 + return current_cooldown + + def get_current_cooldown(self): + self.cooldown = self.get_cooldown(self.location) + return self.cooldown + + def change_gps_by_query(self, query: str, do_activity=False): + self.set_location_with_query(query) + if do_activity: + self.do_activity(self.location) + + def change_gps_by_location(self, location: Location, do_activity=False): + self.set_location(location) + if do_activity: + self.do_activity(self.location) + diff --git a/ispoof/objects/raid.py b/ispoof/objects/raid.py index 05639b0..f8cbec5 100644 --- a/ispoof/objects/raid.py +++ b/ispoof/objects/raid.py @@ -1,10 +1,9 @@ -from .object import Object from datetime import datetime from sqlalchemy import Column, String, Integer, Boolean, Time, PrimaryKeyConstraint from sqlalchemy.orm import declarative_base Base = declarative_base() -class Raid(Object, Base): +class Raid(Base): __tablename__ = "raid" name = Column(String(30)) diff --git a/ispoof/spoofer/scraper.py b/ispoof/spoofer/scraper.py index b7b1dba..0186218 100644 --- a/ispoof/spoofer/scraper.py +++ b/ispoof/spoofer/scraper.py @@ -34,7 +34,7 @@ class Scraper(): shiny = data[9].text == "Yes" start_time = datetime.fromisoformat(data[10].text) end_time = datetime.fromisoformat(data[11].text) - country = data[12].text + country = data[12].text.strip() pokemon = Pokemon(name=name, number=number, location=location, cp=cp, level=level, attack=attack, defense=defense, hp=hp, iv=iv, shiny=shiny, start_time=start_time, end_time=end_time, diff --git a/main.py b/main.py index 797d7e8..857471b 100644 --- a/main.py +++ b/main.py @@ -17,16 +17,17 @@ if __name__ == "__main__": except Exception as e: # traceback.print_exc() print("Already mounted. Continuing.") - player = Player() + + engine = create_engine("sqlite:///data.db") + initialize_database(engine=engine) + player = Player(engine=engine) + query = input("Enter your location: ") player.set_location_with_query(query) - print(str(player.location)) + scraper = Scraper() - engine = create_engine("sqlite:///data.db") - initialize_database(engine=engine) pokemon_lst = PokemonList(engine=engine) while True: - pokemons = scraper.get_hundos() pokemon_lst.insert_to_database(pokemons) print("Spoof to") @@ -37,7 +38,6 @@ if __name__ == "__main__": i = int(input("Choose pokemon: ")) pokemon = pokemons[i] location = pokemon.location - print(pokemon) device.spoof_gps(location) did_activity = None print(pokemon) @@ -46,8 +46,8 @@ if __name__ == "__main__": if activity in ("y", "n"): did_activity = activity == "y" break - player.set_location(location, did_activity) - print(f"Current cooldown: {player.current_cooldown} min") + player.change_gps_by_location(location, did_activity) + print(f"Current cooldown: {player.get_current_cooldown()} min") while True: continue_prompt = input("Continue? [Y/N] ").lower()