#!/usr/bin/env python """ pgoapi - Pokemon Go API Copyright (c) 2016 tjado Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Author: tjado """ import os import sys import json import time import pprint import logging import getpass import argparse import mysql.connector Connection = None Cursor = None # add directory of this file to PATH, so that the package will be found sys.path.append(os.path.dirname(os.path.realpath(__file__))) # import Pokemon Go API lib from pgoapi import pgoapi from pgoapi import utilities as util log = logging.getLogger(__name__) def dbconnect(): conn = mysql.connector.connect(host='MY_HOST', database='MY_DB', user='MY_USER', password='MY_PASSWORD') if conn.is_connected(): print('Connected to MySQL database') return conn def init_config(): parser = argparse.ArgumentParser() config_file = "config.json" # If config file exists, load variables from json load = {} if os.path.isfile(config_file): with open(config_file) as data: load.update(json.load(data)) # Read passed in Arguments required = lambda x: not x in load parser.add_argument("-a", "--auth_service", help="Auth Service ('ptc' or 'google')", required=required("auth_service")) parser.add_argument( '-id', '--scanerid', type=str.lower, help='ScanerID', required=True) parser.add_argument("-u", "--username", help="Username", required=required("username")) parser.add_argument("-p", "--password", help="Password") parser.add_argument("-d", "--debug", help="Debug Mode", action='store_true') parser.add_argument("-t", "--test", help="Only parse the specified location", action='store_true') parser.set_defaults(DEBUG=False, TEST=False) config = parser.parse_args() # Passed in arguments shoud trump for key in config.__dict__: if key in load and config.__dict__[key] == None: config.__dict__[key] = str(load[key]) if config.__dict__["password"] is None: log.info("Secure Password Input (if there is no password prompt, use --password ):") config.__dict__["password"] = getpass.getpass() if config.auth_service not in ['ptc', 'google']: log.error("Invalid Auth service specified! ('ptc' or 'google')") return None return config def makeSQL(request, content = None): global Connection global Cursor need_to_go = True while(need_to_go): try: Cursor.execute(request, content) need_to_go = False return Cursor except Exception: Connection = dbconnect() Cursor = Connection.cursor() def main(): try: # log settings # log format logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s') # log level for http request class logging.getLogger("requests").setLevel(logging.ERROR) # log level for main pgoapi class logging.getLogger("pgoapi").setLevel(logging.ERROR) # log level for internal pgoapi class logging.getLogger("rpc_api").setLevel(logging.ERROR) config = init_config() if not config: return if config.debug: logging.getLogger("requests").setLevel(logging.DEBUG) logging.getLogger("pgoapi").setLevel(logging.DEBUG) logging.getLogger("rpc_api").setLevel(logging.DEBUG) # instantiate pgoapi api = pgoapi.PGoApi() result = makeSQL("SELECT * FROM `PG_Scaners` WHERE `id`='"+config.scanerid+"' ORDER BY `timestamp` ASC LIMIT 1") position = (48.514700, 34.583425, 0.0) for (_, lat, lon, _ ) in result.fetchall(): position = (float(lat), float(lon), 20.0) # set player position on the earth api.set_position(*position) # new authentication initialitation api.set_authentication(provider = config.auth_service, username = config.username, password = config.password) # provide the path for your encrypt dll api.activate_signature("encrypt.dll") stepcount = 0 while True: # print get maps object stepcount = stepcount + 1 pokemons = 0 forts = 0 print "[I]Location: " + str(position) + " || Step#: " + str(stepcount) cell_ids = util.get_cell_ids(position[0], position[1], 1500) timestamps = [0,] * len(cell_ids) map_dict = api.get_map_objects(latitude = position[0], longitude = position[1], since_timestamp_ms = timestamps, cell_id = cell_ids) try: cells = map_dict['responses']['GET_MAP_OBJECTS']['map_cells'] except TypeError: log.error("Could't fetch data, is your account activated?") return False #print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(map_dict))) for cell in cells: for p in cell.get('wild_pokemons', []): pokemons = pokemons + 1 makeSQL("SOME_DB_REQUEST", (SOME_DATA)) makeSQL("SOME_DB_REQUEST ", (SOME_DATA)) for f in cell.get('forts', []): forts = forts + 1 makeSQL("SOME_DB_REQUEST", (SOME_DATA)) makeSQL("SOME_DB_REQUEST", (SOME_DATA)) result = makeSQL("SOME_DB_REQUEST", (SOME_DATA)) for (_, lat, lon, _ ) in result.fetchall(): position = (float(lat), float(lon), 20.0) print "[I]Forts: "+str(forts) print "[I]Pokemons: "+str(pokemons) time.sleep(1) # set player position on the earth api.set_position(*position) if stepcount%25 == 0: print "[!]Relog" # new authentication initialitation api.set_authentication(provider = config.auth_service, username = config.username, password = config.password) time.sleep(1) except Exception: print "Crah:" main() if __name__ == '__main__': while True: print "Go-go-go" main()