def summoner_changed_presence(self, summoner_id, presence): ''' Responds to the event of a summoner changing presence :param summoner_id: The summoner ID of the user that changed presence :param presence: The new presence of the summoner ''' user = self.get_user(summoner_id) champion_regex = '<skinname>([\w ]+)<\/skinname>' lol_presence = presence['status'] search = re.search(champion_regex, lol_presence) # Able to see a skin name; therefore in game if (search != None): groups = search.groups() if '<gameStatus>inGame</gameStatus>' in lol_presence \ and len(groups) > 0: if user.champion != groups[0]: champion = groups[0] Logger().get().info( 'Summoner Started Game: {} ({}) [{}]'.format( user.summoner.summonername, user.summoner.user.twitchusername, champion)) AlerterRedis().summoner_ingame(summoner_id, champion) user.champion = champion # Unable to see a skin name, established out of game else: AlerterRedis().summoner_outgame(summoner_id) user.champion = ''
def run_method(self): ''' The method on which the thread will run ''' first = True while (self.alive): # Get the latest few donations redis = AlerterRedis() latest = Donation.fetch(5, self.user.twitchalertskey) for new in latest: donate_id = new['id'] # Store the overnight donations in the redis server if first: redis.new_donation(self.user, donate_id) continue if (not redis.has_donation(self.user, donate_id)): redis.new_donation(self.user, donate_id) if (self.user.minimumdonation != None and float(new['amount']) < self.user.minimumdonation): continue self.active_user.new_donation(new['donator']['name'], new['amount'], new['message']) first = False # Minimum cache time by TwitchAlerts time.sleep(20)
def __init__(self): Logger().get().info("Started LoLAlerter") AlerterRedis.configure(Settings.redis()) AlerterRedis().clear() self.chat = TwitchChat() unsub = UnsubTracker() unsub.start() dbtracker = DBTracker(self) dbtracker.start()
def update_subs(self, user): ''' Updates the subscribers for a particular user :param user: The ActiveUser model object to update for ''' stats = (0, 0) twitch_current = Subscriber.fetch_all(user.twitchusername, user.twitchtoken) if twitch_current == None: return twitch_ids = [x['user']['_id'] for x in twitch_current] current_models = SubModel.select(SubModel.twitchid)\ .where(SubModel.user == user, SubModel.active) db_current = [x.twitchid for x in current_models] new_subs = [ x for x in twitch_current if x['user']['_id'] not in db_current ] un_subs = [x for x in db_current if x not in twitch_ids] for sub in new_subs: new_sub = SubTracker.create_sub_model(user, sub['user']) new_sub.active = 1 new_sub.save() redis = AlerterRedis() if un_subs: SubModel.update(active=False, unsubdate=datetime.now())\ .where(SubModel.twitchid << un_subs, SubModel.user == user)\ .execute() for unsub_user in SubModel.select(SubModel.username)\ .where(SubModel.twitchid << un_subs, SubModel.user == user): redis.del_subscriber(user, unsub_user.username) stats = (len(new_subs), len(un_subs)) self.stats = tuple(map(sum, zip(self.stats, stats)))
def user_online(self, summoner_id, retry=False): ''' Handles a new user connecting to the chat :param summoner_id: The summoner ID of the new user :param retry: A boolean of whether the user has retried the login ''' try: summoner = (ActiveSummoner.select( ActiveSummoner, User).join(User).where( ActiveSummoner.summonerid == summoner_id).get()) Logger().get().info("User Online: {} ({} - {})".format( summoner.user.twitchusername, summoner.summonername, summoner.summonerid)) if (retry): self.chat.new_message(summoner_id, Constants.INACTIVE_NOW_ACTIVE) # Summoner is not an active user except ActiveSummoner.DoesNotExist: self.chat.new_message(summoner_id, Constants.INACTIVE_RESTART) Logger().get().info("Inactive User: {} ({} - {})".format( summoner.user.twitchusername, summoner.summonername, summoner.summonerid)) try: self.setup_user(summoner) AlerterRedis().user_online(summoner) except Exception as err: import random reference = ('%05x' % random.randrange(16**5)).upper() message = str(Constants.ERROR_MESSAGE).format(ref=reference) self.chat.new_message(summoner_id, message) Logger().get().info('Exception reference: {}'.format(reference)) Logger().get().exception(err)
def user_offline(self, summoner_id): ''' Handles a user disconnecting from the chat :param summoner_id: The summoner ID of the leaving user ''' try: summoner = (ActiveSummoner.select( ActiveSummoner, User).join(User).where( ActiveSummoner.summonerid == summoner_id).get()) Logger().get().info("User Offline: {} ({} - {})".format( summoner.user.twitchusername, summoner.summonername, summoner.summonerid)) # Summoner is not an active user except ActiveSummoner.DoesNotExist: return for running in [ x for x in self.users if x.summoner.user.id == summoner.user.id ]: running.stop() self.users.remove(running) AlerterRedis().user_offline(summoner.user)
def run_method(self): ''' The method on which the thread will run ''' first = True while (self.alive): # Get the latest few subscribers redis = AlerterRedis() latest = Subscriber.fetch(5, self) for new in latest: username = new['user']['display_name'] twitch_id = new['user']['_id'] # Store the overnight subscribers in the redis server if first: redis.new_subscriber(self.user, username) continue if (not redis.has_subscriber(self.user, username)): redis.new_subscriber(self.user, username) self.active_user.new_subscriber( username, SubTracker.db_exists(self.user, twitch_id)) # Update database after notification, otherwise it will # invalidate the resub ability new_sub = SubTracker.create_sub_model( self.user, new['user']) new_sub.active = 1 new_sub.save() first = False time.sleep(5)