Skip to content

Commit

Permalink
Merge pull request #5645 from PokemonGoF/dev
Browse files Browse the repository at this point in the history
Dev Merge to Master
  • Loading branch information
Gobberwart authored Sep 24, 2016
2 parents 90b35ea + e262b95 commit fd49544
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
20 changes: 18 additions & 2 deletions pokemongo_bot/cell_workers/sniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ def fetch_raw(self):
results = response.json()

# If the results is a dict, retrieve the list from it by the given key. This will return a list afterall.
return results.get(self.key, []) if isinstance(results, dict) else results
if isinstance(results, dict):
results = results.get(self.key, [])

# If results is STILL a dict (eg. each pokemon is its own dict), need to build data from nested json (example whereispokemon.net)
while isinstance(results,dict):
tmpResults = []
for key, value in results.iteritems():
tmpResults.append(value)
results = tmpResults

return results

def fetch(self):
pokemons = []
Expand All @@ -46,7 +56,7 @@ def fetch(self):
for result in results:
iv = result.get(self.mappings.iv.param)
id = result.get(self.mappings.id.param)
name = result.get(self.mappings.name.param)
name = self._fixname(result.get(self.mappings.name.param))
latitude = result.get(self.mappings.latitude.param)
longitude = result.get(self.mappings.longitude.param)
expiration = result.get(self.mappings.expiration.param)
Expand Down Expand Up @@ -140,6 +150,12 @@ def validate(self):
raise ValueError("Source not available")
except:
raise

def _fixname(self,name):
name = name.replace("mr-mime","mr. mime")
name = name.replace("farfetchd","farfetch'd")
return name


# Represents the JSON params mappings
class SniperSourceMapping(object):
Expand Down
26 changes: 10 additions & 16 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, bot, pokemons, config):
self.config = config
self.chat_handler = ChatHandler(self.bot, pokemons)
self.master = self.config.get('master')
self.logged_in = False
with self.bot.database as conn:
# initialize the DB table if it does not exist yet
initiator = TelegramDBInit(bot.database)
Expand Down Expand Up @@ -57,7 +56,7 @@ def grab_uid(self, update):
conn.execute("replace into telegram_uids (uid, username) values (?, ?)",
(update.message.chat_id, update.message.from_user.username))
conn.commit()
self.master = update.message.chat_id
if self.master: self.master = update.message.chat_id

def isMasterFromConfigFile(self, chat_id):
if not hasattr(self, "master") or not self.master:
Expand All @@ -76,21 +75,19 @@ def isMasterFromActiveLogins(self, chat_id):
cur = conn.cursor()
cur.execute("select count(1) from telegram_logins where uid = ?", [chat_id])
res = cur.fetchone()
if res[0] == 1:
return True
else:
return False
if res[0] == 1: return True
return False

def isAuthenticated(self, chat_id):
return self.isMasterFromConfigFile(chat_id) or self.isMasterFromActiveLogins(chat_id)

def deauthenticate(self, update):
with self.bot.database as conn:
cur = conn.cursor()
cur.execute("delete from telegram_logins where uid = ?", [update.message.chat_id])
sql = "delete from telegram_logins where uid = {}".format(update.message.chat_id)
cur.execute(sql)
conn.commit()
self.chat_handler.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Logout completed")
self.logged_in = False
return

def authenticate(self, update):
Expand All @@ -106,12 +103,10 @@ def authenticate(self, update):
else:
with self.bot.database as conn:
cur = conn.cursor()
cur.execute("delete from telegram_logins where uid = ?", [update.message.chat_id])
cur.execute("insert into telegram_logins(uid) values(?)", [update.message.chat_id])
cur.execute("insert or replace into telegram_logins(uid) values(?)",[update.message.chat_id])
conn.commit()
self.chat_handler.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown',
text="Authentication successful, you can now use all commands")
self.logged_in = True
return

def run(self):
Expand Down Expand Up @@ -161,9 +156,8 @@ def run(self):
self.authenticate(update)
continue
if not self.isAuthenticated(update.message.from_user.id) and hasattr(self,
"master") and self.master and not unicode(
self.master).isnumeric() and unicode(self.master) == unicode(
update.message.from_user.username):
"master") and self.master and not unicode(self.master).isnumeric() and \
unicode(self.master) == unicode(update.message.from_user.username):
outMessage = "Telegram message received from correct user, but master is not numeric, updating datastore."
self.bot.logger.warn(outMessage)
# the "master" is not numeric, set self.master to update.message.chat_id and re-instantiate the handler
Expand Down Expand Up @@ -352,7 +346,7 @@ def handle_event(self, event, sender, level, formatted_msg, data):
for sub in subs:
if DEBUG_ON: self.bot.logger.info("Processing sub {}".format(sub))
(uid, params, event_type) = sub
if not self.tbot.logged_in:
if not self.tbot.isAuthenticated(uid): # UID has subs but not in auth list.
return
if event != 'pokemon_caught' or self.catch_notify(data["pokemon"], int(data["cp"]), float(data["iv"]),
params):
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
numpy==1.11.0
networkx==1.11
-e git+https://github.com/pogodevorg/pgoapi.git/@11877201a2a87db886a6d01325e365d9c9563528#egg=pgoapi
-e git+https://github.com/pogodevorg/pgoapi.git/@3a02e7416f6924b1bbcbcdde60c10bd247ba8e11#egg=pgoapi
geopy==1.11.0
geographiclib==1.46.3
protobuf==3.0.0b4
requests==2.10.0
requests-mock==1.0.0
s2sphere==0.2.4
gpsoauth==0.3.0
gpsoauth==0.4.0
six==1.9.0
protobuf-to-dict==0.1.0
googlemaps==2.4.4
Expand Down

0 comments on commit fd49544

Please sign in to comment.