Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """getPokestopDetails.py - get Name/Description/Image for Pokestops from DB."""
  5.  
  6. from pgoapi import PGoApi
  7. from pgoapi.utilities import f2i
  8. from pgoapi import utilities as util
  9. from pgoapi.exceptions import AuthException
  10. from pgoapi.exceptions import ServerSideRequestThrottlingException
  11. from pgoapi.exceptions import NotLoggedInException
  12. from peewee import *
  13. import time
  14. import sys
  15. import MySQLdb as mdb
  16. import MySQLdb.cursors
  17. import argparse
  18. import pprint
  19.  
  20.  
  21. #stolen from utils.py
  22. def parse_unicode(bytestring):
  23. decoded_string = bytestring.decode(sys.getfilesystemencoding())
  24. return decoded_string
  25.  
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument('-a', '--auth-service', type=str.lower, default='ptc',
  28. help='Auth Services: ptc or google. Defaults to ptc.')
  29. parser.add_argument('-u', '--username', default='',
  30. help='Username.', required=True)
  31. parser.add_argument('-p', '--password', default='',
  32. help='Password.', required=True)
  33. parser.add_argument('-l', '--location', type=parse_unicode,
  34. help='Location, can be an address or coordinates', required=True)
  35. parser.add_argument('-px', '--proxy',
  36. help='Proxy url (e.g. socks5://127.0.0.1:9050)')
  37. parser.add_argument('--db-type',
  38. help='Type of database to be used (default: mysql)', default='mysql', required=True)
  39. parser.add_argument('-D', '--db',
  40. help='Database filename', default='pogom.db')
  41. parser.add_argument('--db-name', help='Name of the database to be used')
  42. parser.add_argument('--db-user', help='Username for the database')
  43. parser.add_argument('--db-pass', help='Password for the database')
  44. parser.add_argument('--db-host', help='IP or hostname for the database', default='127.0.0.1')
  45. parser.add_argument('--db-port', help='Port for the database', type=int, default=3306)
  46. args = parser.parse_args()
  47.  
  48. position = util.get_pos_by_name(args.location)
  49. if not position:
  50. print('Location invalid.')
  51. quit()
  52.  
  53. if args.db_type == 'mysql':
  54. if not args.db_user or not args.db_pass or not args.db_name:
  55. print("Needed parameters for MySQL: --db-user, --db-pass, --db-name.")
  56. quit()
  57. print('Connecting to MySQL database on {}:{}'.format(args.db_host, args.db_port))
  58. db = MySQLDatabase(args.db_name, user=args.db_user, password=args.db_pass, host=args.db_host, port=args.db_port)
  59.  
  60. elif args.db_type == 'sqlite':
  61. if not args.db:
  62. print("You need to specify an SQLite file with -D / --db.")
  63. quit()
  64. db = SqliteDatabase(args.db)
  65. else:
  66. print("Unsupported database type.")
  67. quit()
  68.  
  69.  
  70. def run_scan(db, args, position):
  71.  
  72. db.connect()
  73. PokestopDetails.create_table(fail_silently=True)
  74.  
  75. pokestops = {}
  76. pokestops = (Pokestop.select(Pokestop.pokestop_id, Pokestop.latitude, Pokestop.longitude)
  77. .join(PokestopDetails, JOIN.LEFT_OUTER, on=(PokestopDetails.pokestop_id == Pokestop.pokestop_id))
  78. .where(
  79. (PokestopDetails.name >> None) |
  80. (PokestopDetails.name == '')
  81. )
  82. .order_by(Pokestop.latitude, Pokestop.longitude)
  83. .desc()
  84. )
  85.  
  86. api = PGoApi()
  87. api.set_position(*position)
  88.  
  89. if args.proxy:
  90. print("Using proxy {}".format(args.proxy))
  91. api.set_proxy({'http': args.proxy, 'https': args.proxy})
  92.  
  93. print("Logging in with user {}".format(args.username))
  94. if not api.login(args.auth_service, args.username, args.password, app_simulation = True):
  95. return
  96.  
  97. #test command
  98. api.get_player()
  99. time.sleep(1)
  100.  
  101.  
  102. for stop in pokestops:
  103. lat = stop.latitude
  104. lng = stop.longitude
  105. print("Scanning for Stop at {}, {}".format(lat,lng))
  106. position = util.get_pos_by_name("{}, {}".format(lat,lng))
  107. api.set_position(*position)
  108. cell_ids = util.get_cell_ids(lat, lng)
  109. timestamps = [0,] * len(cell_ids)
  110. response_dict = api.get_map_objects(latitude = lat, longitude = lng, since_timestamp_ms = timestamps, cell_id = cell_ids)
  111. stopinfo = api.fort_details(fort_id = stop.pokestop_id, latitude = lat, longitude = lng)
  112.  
  113. if not stopinfo['responses']:
  114. print("Got no information, continuing...")
  115. continue
  116.  
  117. image = stopinfo['responses']['FORT_DETAILS']['image_urls'][0]
  118. name = stopinfo['responses']['FORT_DETAILS']['name']
  119. desc = stopinfo['responses']['FORT_DETAILS'].get('description', '')
  120.  
  121. qry = PokestopDetails.insert(pokestop_id=stop.pokestop_id, name=name, image_url=image, description=desc).upsert(upsert=True)
  122. qry.execute()
  123.  
  124. time.sleep(2)
  125.  
  126.  
  127. #peewee stuff
  128. class PokestopDetails(Model):
  129. pokestop_id = CharField(primary_key=True, index=True, max_length=50)
  130. name = CharField(max_length=100)
  131. description = TextField(null=True, default="")
  132. image_url = TextField(null=True, default="")
  133. class Meta:
  134. database = db
  135.  
  136.  
  137. class Pokestop(Model):
  138. pokestop_id = CharField(primary_key=True, max_length=50)
  139. enabled = BooleanField()
  140. latitude = DoubleField()
  141. longitude = DoubleField()
  142. last_modified = DateTimeField(index=True)
  143. lure_expiration = DateTimeField(null=True, index=True)
  144. active_fort_modifier = CharField(max_length=50, null=True)
  145.  
  146. class Meta:
  147. indexes = ((('latitude', 'longitude'), False),)
  148. database = db
  149.  
  150.  
  151. run_scan(db, args, position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement