Advertisement
Guest User

Pokelizer patch

a guest
Jul 27th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.30 KB | None | 0 0
  1. Index: pogom/utils.py
  2. ===================================================================
  3. --- pogom/utils.py  (revision 5c3b1bbc819097452f2ddc279bb16dbbc51000d9)
  4. +++ pogom/utils.py  (revision )
  5. @@ -31,6 +31,7 @@
  6.  
  7.      parser.add_argument('-d', '--debug', type=str.lower, help='Debug Level [info|debug]', default=None)
  8.      parser.add_argument('-c', '--pycurl', help='Use pycurl downloader (unstable)', action='store_true')
  9. +    parser.add_argument('--pokel-pass', help='Password for Pokelizer database')
  10.  
  11.      args = parser.parse_args()
  12.      if args.password is None:
  13. Index: pogom/postgres.py
  14. ===================================================================
  15. --- pogom/postgres.py   (revision )
  16. +++ pogom/postgres.py   (revision )
  17. @@ -0,0 +1,40 @@
  18. +from .utils import get_pokemon_name
  19. +from pogom.utils import get_args
  20. +from datetime import datetime
  21. +import psycopg2
  22. +import random
  23. +import logging
  24. +
  25. +args = get_args()
  26. +
  27. +conn = psycopg2.connect(dbname="pokemon_go", user="pokemon_go_role", password=args.pokel_pass, host="127.0.0.1")
  28. +conn.autocommit = True
  29. +cursor = conn.cursor()
  30. +
  31. +log = logging.getLogger(__name__)
  32. +
  33. +def logPokemonDb(p):
  34. +    pokemon_id = int(p['pokemon_data']['pokemon_id'])
  35. +    pokemon_name = get_pokemon_name(str(pokemon_id)).lower().encode('ascii','ignore')
  36. +
  37. +    last_modified_time = int(p['last_modified_timestamp_ms'])
  38. +    time_until_hidden_ms = int(p['time_till_hidden_ms'])
  39. +
  40. +    hidden_time_unix_s = int((p['last_modified_timestamp_ms'] + p['time_till_hidden_ms']) / 1000.0)
  41. +    hidden_time_utc = datetime.utcfromtimestamp(hidden_time_unix_s)
  42. +
  43. +    encounter_id = str(p['encounter_id'])
  44. +    spawnpoint_id = str(p['spawnpoint_id'])
  45. +
  46. +    longitude = float(p['longitude'])
  47. +    latitude = float(p['latitude'])
  48. +    longitude_jittered = longitude + (random.gauss(0, 0.3) - 0.5) * 0.0005
  49. +    latitude_jittered = latitude + (random.gauss(0, 0.3) - 0.5) * 0.0005
  50. +
  51. +    query =  "INSERT INTO spotted_pokemon (name, encounter_id, last_modified_time, time_until_hidden_ms, hidden_time_unix_s, hidden_time_utc, spawnpoint_id, longitude, latitude, pokemon_id, longitude_jittered, latitude_jittered) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
  52. +    data = (pokemon_name, encounter_id, last_modified_time, time_until_hidden_ms, hidden_time_unix_s, hidden_time_utc, spawnpoint_id, longitude, latitude, pokemon_id, longitude_jittered, latitude_jittered)
  53. +
  54. +    try:
  55. +        cursor.execute(query, data)
  56. +    except Exception,e:
  57. +        log.error('Postgresql error (%s)', str(e))
  58. Index: pogom/models.py
  59. ===================================================================
  60. --- pogom/models.py (revision 5c3b1bbc819097452f2ddc279bb16dbbc51000d9)
  61. +++ pogom/models.py (revision )
  62. @@ -8,6 +8,7 @@
  63.  from base64 import b64encode
  64.  
  65.  from .utils import get_pokemon_name
  66. +from .postgres import logPokemonDb
  67.  
  68.  db = SqliteDatabase('pogom.db', pragmas=(
  69.      ('journal_mode', 'WAL'),
  70. @@ -112,7 +113,9 @@
  71.          for p in cell.get('wild_pokemons', []):
  72.              if p['encounter_id'] in pokemons:
  73.                  continue  # prevent unnecessary parsing
  74. +
  75. +            logPokemonDb(p)
  76. -
  77. +            
  78.              pokemons[p['encounter_id']] = {
  79.                  'encounter_id': b64encode(str(p['encounter_id'])),
  80.                  'spawnpoint_id': p['spawnpoint_id'],
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement