def checkInvLoop(self, bot, connection, event): if not self.invLoop: return print("got to checkinvloop") try: parsed_data = urlopen(Request("http://toonhq.org/api/v1/invasion/", headers={'User-Agent': 'Mozilla/5'}), timeout=5).read().decode('utf-8') except (timeout, HTTPError, URLError): bot.send(connection, event.target, "Sorry %s, I could not get a response from the server!" % event.source.nick) return raw_json = json.loads(parsed_data) cogTypes = [] invasions = [] for inv in raw_json['invasions']: cogTypes.append(inv['cog'].replace('\x03', '')) #print(cogTypes) #debug if any(cog in cogTypes for cog in self.cogs_to_watch): print("got some matching cogs") InvasionData = [] for inv in raw_json['invasions']: InvasionData = {inv['district']:{'cog' : inv['cog'], 'total' : inv['total'], 'defeated' : inv['defeated'], 'defeat_rate' : inv['defeat_rate']}} # print("invasionData = ", InvasionData) #debug for districts in InvasionData: if any(cog in InvasionData[districts]['cog'].replace('\x03', '') for cog in self.cogs_to_watch): total = (InvasionData[districts]['total']) # print("total = ", total) #debug defeated = (InvasionData[districts]['defeated']) # print("defeated = ", defeated) #debug defeat_rate = (InvasionData[districts]['defeat_rate']) # print("defeat_rate = ", defeat_rate) district = (inv['district']) # print("district = ", district) #debug cog = (InvasionData[districts]['cog']) # print("cog = ", cog) #debug num_remaining = total - defeated # print("remaining = ", num_remaining) #debug time_remaining = num_remaining / defeat_rate / 60 # print("time left = ", time_remaining) #debug bot.send(connection, event.target, "It appears that %s have taken over %s for approximately %s minutes. (%s cogs left @~%s cogs/sec!)" %(f.bold(cog + 's'), f.bold(district), f.bold(str(int(time_remaining))), f.bold(str(int(num_remaining))), f.bold(str(defeat_rate)[:5]))) bot.send(connection, event.target, "You are seeing this notification because '%s' is in the Watchlist!" % f.bold(cog), event) else: print("couldnt find matching cogs") bot.execute_delayed(bot.connection, 30, self.checkInvLoop, (bot, connection, event))
def returnProgress(self, requestedType, bot, connection, event): requestedType = requestedType.title() try: parsed_data = urlopen(Request("http://toonhq.org/api/v1/invasion/", headers={'User-Agent': 'Mozilla/5'}), timeout=5).read().decode('utf-8') except (timeout, HTTPError, URLError): bot.send(connection, event.target, "Sorry %s, I could not get a response from the server!" % event.source.nick) return raw_json = json.loads(parsed_data) cogTypes = [] for inv in raw_json['invasions']: cogTypes.append(inv['cog'].replace('\x03', '')) if requestedType in cogTypes: #print("yay!", cogTypes) #debug InvasionData = [] for inv in raw_json['invasions']: InvasionData = {inv['district']:{'cog' : inv['cog'], 'total' : inv['total'], 'defeated' : inv['defeated'], 'defeat_rate' : inv['defeat_rate']}} # print("invasionData = ", InvasionData) #debug for districts in InvasionData: if requestedType in InvasionData[districts]['cog'].replace('\x03', ''): total = (InvasionData[districts]['total']) # print("total = ", total) #debug defeated = (InvasionData[districts]['defeated']) # print("defeated = ", defeated) #debug defeat_rate = (InvasionData[districts]['defeat_rate']) # print("defeat_rate = ", defeat_rate) num_remaining = total - defeated #print("remaining = ", num_remaining) #debug time_remaining = num_remaining / defeat_rate / 60 #print("time left = ", time_remaining) #debug bot.send(connection, event.target, "%s, there's about %s minutes left in the %s invasion. (%s cogs left @~%s cogs/sec!)" %(event.source.nick, f.bold(str(int(time_remaining))), f.bold(requestedType), f.bold(str(int(num_remaining))), f.bold(str(defeat_rate)[:5]))) #bot.send(connection, event.target, "%s, in the %s invasion, there are %s cogs remaining. (about %s minutes left at ~%s cogs/min!)" % (event.source.nick, f.bold(requestedType), f.bold(str(int(num_remaining))), f.bold(str(int(time_remaining))), f.bold(str(defeat_rate)[:5])), event) else: bot.send(connection, event.target, "Sorry %s, I could not find an invasion for '%s'." % (event.source.nick, requestedType), event)