Skip to content

Commit

Permalink
Adding initial REST Framework; Adding method to get user stats (#204)
Browse files Browse the repository at this point in the history
* Adding initial REST Framework; Adding method to get user stats

* Changing bot's get_player_info print statement to adhere to the standards
  • Loading branch information
bstascavage authored and jtdroste committed Jul 22, 2016
1 parent f4b0cd0 commit 81ea152
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
11 changes: 9 additions & 2 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ssl._create_default_https_context = ssl._create_unverified_context

from pokemongo_bot import PokemonGoBot
from server import PokemonGoServer

def init_config():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -96,8 +97,14 @@ def main():
bot = PokemonGoBot(config)
bot.start()

while(True):
bot.take_step()
pid = os.fork()

if pid == 0:
server = PokemonGoServer(bot, 5000)
server.start()
else:
while(True):
bot.take_step()

if __name__ == '__main__':
main()
21 changes: 15 additions & 6 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from stepper import Stepper
from geopy.geocoders import GoogleV3
from math import radians, sqrt, sin, cos, atan2
from collections import OrderedDict

class PokemonGoBot(object):

Expand Down Expand Up @@ -248,7 +249,10 @@ def get_inventory_count(self, what):
return itemcount
return '0'

def get_player_info(self):
def get_player_info(self, print_stats=True):
player_stats = stats = OrderedDict()

# Get contents of inventory
self.api.get_inventory()
response_dict = self.api.call()
if 'responses' in response_dict:
Expand All @@ -266,14 +270,19 @@ def get_player_info(self):
nextlvlxp = (int(playerdata['next_level_xp']) - int(playerdata['experience']))

if 'level' in playerdata:
print('[#] -- Level: {level}'.format(**playerdata))
player_stats['Level'] = playerdata['level']

if 'experience' in playerdata:
print('[#] -- Experience: {experience}'.format(**playerdata))
print('[#] -- Experience until next level: {}'.format(nextlvlxp))
player_stats['Experience'] = playerdata['experience']
player_stats['Experience until next level'] = nextlvlxp

if 'pokemons_captured' in playerdata:
print('[#] -- Pokemon Captured: {pokemons_captured}'.format(**playerdata))
player_stats['Pokemon Captured'] = playerdata['pokemons_captured']

if 'poke_stop_visits' in playerdata:
print('[#] -- Pokestops Visited: {poke_stop_visits}'.format(**playerdata))
player_stats['Pokestops Visited'] = playerdata['poke_stop_visits']

if print_stats:
for key in player_stats:
print('[#] -- {}: {}'.format(key, player_stats[key]))
return json.dumps(player_stats, indent=4)
30 changes: 30 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json

from flask import Flask
from flask.ext.classy import FlaskView, route
#from bot import PokemonGoBot

from pgoapi import PGoApi

app = Flask(__name__)

global global_bot

class PokemonGoServer(object):
def __init__(self, bot, port=5000):
api_view = ApiView()
api_view.set_bot(bot)
api_view.register(app)
self.port = port

def start(self):
app.run(host='0.0.0.0', port=self.port)

class ApiView(FlaskView):
def set_bot(self, bot):
global global_bot
global_bot = bot

@route("/player_info")
def get_player_info(self):
return global_bot.get_player_info(False)

0 comments on commit 81ea152

Please sign in to comment.