feat: SQLite Database for Pokemon

main
Khiem Ton 2 years ago
parent a3964db328
commit 7a0ad476eb
Signed by: th4tkh13m
GPG Key ID: 4D9CF147DCADD05D

2
.gitignore vendored

@ -2,3 +2,5 @@ __pycache__
Pipfile Pipfile
Pipfile.lock Pipfile.lock
DeveloperDiskImage* DeveloperDiskImage*
logs
data.db

@ -1,10 +1,26 @@
from math import radians, sin, cos, asin, sqrt from math import radians, sin, cos, asin, sqrt
from sqlalchemy.types import TypeDecorator, String
class Location(): class Location(TypeDecorator):
def __init__(self, latitude, longitude): impl = String
cache_ok = True
def __init__(self, latitude=None, longitude=None):
self.latitude = latitude self.latitude = latitude
self.longitude = longitude self.longitude = longitude
def process_bind_param(self, value, dialect):
if value:
return str(value)
def process_result_value(self, value, dialect):
if value:
return Location(* map(float, value.split(",")))
def load_dialect_impl(self, dialect):
# Provide the implementation for the specified dialect
return dialect.type_descriptor(String)
def distance(self, other): def distance(self, other):
R = 6371 R = 6371
@ -75,6 +91,8 @@ class Location():
else: else:
return 120 return 120
def __str__(self):
return str(self.latitude) + "," + str(self.longitude)
def __repr__(self): def __repr__(self):
return str(self.latitude) + "," + str(self.longitude) return str(self.latitude) + "," + str(self.longitude)

@ -1,13 +1,28 @@
from ..objects.pokemon import Pokemon from ..objects.pokemon import Pokemon
from ..core.location import Location from ..core.location import Location
from thefuzz.fuzz import partial_ratio from thefuzz.fuzz import partial_ratio
from sqlalchemy.orm import Session
from sqlalchemy.engine import Engine
from typing import List
from sqlalchemy import desc
from datetime import datetime, timedelta
class PokemonList(): class PokemonList():
def __init__(self, *args: Pokemon): def __init__(self, engine: Engine):
self.pokemons = [*args] self.pokemons = None
self.engine = engine
self.timedelta = timedelta(minutes=2)
def sort_by_name(self, reverse=False): def sort_by_name(self, reverse=False):
self.pokemons.sort(key=lambda pokemon: pokemon.name, reverse=reverse) with Session(self.engine) as session:
if reverse:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\
.order_by(desc(Pokemon.name))
else:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\
.order_by(Pokemon.name)
session.commit()
return result.all()
def sort_by_cp(self, reverse=False): def sort_by_cp(self, reverse=False):
self.pokemons.sort(key=lambda pokemon: pokemon.cp, reverse=reverse) self.pokemons.sort(key=lambda pokemon: pokemon.cp, reverse=reverse)
@ -24,3 +39,8 @@ class PokemonList():
def search_by_name(self, query:str): def search_by_name(self, query:str):
return PokemonList(*filter(lambda pokemon: partial_ratio(query.lower(), pokemon.name) >= 80, self.pokemons)) return PokemonList(*filter(lambda pokemon: partial_ratio(query.lower(), pokemon.name) >= 80, self.pokemons))
def insert_to_database(self, pokemons: List[Pokemon]):
with Session(self.engine) as session:
session.add_all(pokemons)
session.commit()

@ -0,0 +1,3 @@
from .player import Player
from .pokemon import Pokemon
from .raid import Raid

@ -9,9 +9,9 @@ class Player(Object):
def set_location_with_query(self, query): def set_location_with_query(self, query):
loc = Nominatim(user_agent="GetLoc") loc = Nominatim(user_agent="GetLoc")
getLoc = loc.geocode(query) get_loc = loc.geocode(query)
self.location = Location(getLoc.latitude, getLoc.longitude) self.location = Location(get_loc.latitude, get_loc.longitude)
def set_location(self, location, did_activity): def set_location(self, location, did_activity):
next_cd = self.cooldown_to(location) next_cd = self.cooldown_to(location)

@ -1,11 +1,31 @@
from datetime import datetime from datetime import datetime
from .object import Object from sqlalchemy import Column, String, Integer, Boolean, DateTime, PrimaryKeyConstraint
from ..core.location import Location
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class Pokemon(Base):
__tablename__ = "pokemon"
name = Column(String(30))
number = Column(Integer)
location = Column(Location)
cp = Column(Integer)
level = Column(Integer)
attack = Column(Integer)
defense = Column(Integer)
hp = Column(Integer)
iv = Column(Integer)
shiny = Column(Boolean)
start_time = Column(DateTime)
end_time = Column(DateTime)
country = Column(String(2))
PrimaryKeyConstraint(name, location, start_time, end_time, name="pokemon_pk", sqlite_on_conflict='IGNORE')
class Pokemon(Object):
def __init__(self, name, number, location, def __init__(self, name, number, location,
cp, level, attack, defense, hp, cp, level, attack, defense, hp,
iv, shiny, start_time, end_time, iv, shiny, start_time, end_time,
country): country):
self.name = name self.name = name
self.number = number self.number = number
self.location = location self.location = location
@ -20,8 +40,10 @@ class Pokemon(Object):
self.end_time = end_time self.end_time = end_time
self.country = country self.country = country
super(Pokemon, self).__init__()
def is_dispawned(self): def is_dispawned(self):
if datetime().now() > end_time: if datetime().now() > self.end_time:
return True return True
return False return False

@ -1,15 +1,25 @@
from .object import Object from .object import Object
from datetime import datetime
from sqlalchemy import Column, String, Integer, Boolean, Time, PrimaryKeyConstraint
from sqlalchemy.orm import declarative_base
class Raid(Object): Base = declarative_base()
def __init__(self, name, number, level, location, start_time, end_time, country): class Raid(Object, Base):
super().__init__() __tablename__ = "raid"
self.name = name
self.number = number name = Column(String(30))
self.level = level number = Column(Integer)
self.location = location location = Column(String(50))
self.start_time = start_time level = Column(Integer)
self.end_time = end_time start_time = Column(Time)
self.country = country end_time = Column(Time)
country = Column(String(2))
PrimaryKeyConstraint(name, location, start_time, end_time, name="raid_pk")
def is_dispawned(self):
if datetime().now() > self.end_time:
return True
return False
def __repr__(self): def __repr__(self):
return f"(Raid: {self.name} - {self.level}-star - {self.location} - Start: {self.start_time} - End: {self.end_time})" return f"(Raid: {self.name} - {self.level}-star - {self.location} - Start: {self.start_time} - End: {self.end_time})"

@ -19,7 +19,7 @@ class Scraper():
for row in soup.find_all("tr")[1:]: for row in soup.find_all("tr")[1:]:
data = row.find_all("td")[1:] data = row.find_all("td")[1:]
name = data[0].text.strip() name = data[0].text.strip().strip("*")
number = int(data[1].text) number = int(data[1].text)
coordinates = map(float, data[2].text.split(",")) coordinates = map(float, data[2].text.split(","))
@ -36,7 +36,9 @@ class Scraper():
end_time = datetime.fromisoformat(data[11].text) end_time = datetime.fromisoformat(data[11].text)
country = data[12].text country = data[12].text
pokemon = Pokemon(name, number, location, cp, level, attack, defense, hp, iv, shiny, start_time, end_time, country) 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,
country=country)
pokemon_lst.append(pokemon) pokemon_lst.append(pokemon)
return pokemon_lst return pokemon_lst

@ -1,7 +1,11 @@
from ispoof.core.objects.player import Player from ispoof.objects.player import Player
from ispoof.core.spoofer.scraper import Scraper from ispoof.spoofer.scraper import Scraper
from ispoof.core.spoofer.device import Device from ispoof.spoofer.device import Device
from pymobiledevice3.exceptions import AlreadyMountedError from pymobiledevice3.exceptions import AlreadyMountedError
from sqlalchemy import create_engine
from ispoof.lists.pokemonlist import PokemonList
from ispoof.data import initialize_database
from sqlalchemy.orm import Session
import traceback import traceback
if __name__ == "__main__": if __name__ == "__main__":
@ -11,21 +15,32 @@ if __name__ == "__main__":
try: try:
device.mount_image(image_path, signature_path) device.mount_image(image_path, signature_path)
except Exception as e: except Exception as e:
traceback.print_exc() # traceback.print_exc()
print("Already mounted. Continuing.") print("Already mounted. Continuing.")
player = Player() player = Player()
query = input("Enter your location: ") query = input("Enter your location: ")
player.set_location_with_query(query) player.set_location_with_query(query)
print(str(player.location))
scraper = Scraper() scraper = Scraper()
engine = create_engine("sqlite:///data.db")
initialize_database(engine=engine)
pokemon_lst = PokemonList(engine=engine)
while True: while True:
pokemons = scraper.get_hundos() pokemons = scraper.get_hundos()
pokemons.sort(key=lambda x: x.end_time, reverse=True) pokemon_lst.insert_to_database(pokemons)
print("Spoof to") print("Spoof to")
print(pokemons[10]) print("Pokemon List:")
location = pokemons[10].location pokemons = pokemon_lst.sort_by_name()
for i, pokemon in enumerate(pokemons):
print(i, pokemon)
i = int(input("Choose pokemon: "))
pokemon = pokemons[i]
location = pokemon.location
print(pokemon)
device.spoof_gps(location) device.spoof_gps(location)
did_activity = None did_activity = None
print(pokemon)
while True: while True:
activity = input("Did you do any cooldown activities? [Y/N] ").lower() activity = input("Did you do any cooldown activities? [Y/N] ").lower()
if activity in ("y", "n"): if activity in ("y", "n"):
@ -40,9 +55,5 @@ if __name__ == "__main__":
if continue_prompt == "n": if continue_prompt == "n":
device.stop_spoofing() device.stop_spoofing()
exit(0) exit(0)
else: break else:
break
# print(scraper.get_raids())

@ -4,3 +4,4 @@ lxml
requests requests
geopy geopy
thefuzz[speedup] thefuzz[speedup]
sqlalchemy
Loading…
Cancel
Save