def db_exists(user, twitch_id): ''' Returns whether the subscriber already exists in the database for this particular user :param user: The user who may own the particular subscriber :param twitch_id: The Twitch ID of the user ''' return SubModel.select().where(SubModel.user == user, # @UndefinedVariable SubModel.twitchid == twitch_id).exists() # @UndefinedVariable
def db_exists(user, twitch_id): ''' Returns whether the subscriber already exists in the database for this particular user :param user: The user who may own the particular subscriber :param twitch_id: The Twitch ID of the user ''' return SubModel.select().where( SubModel.user == user, # @UndefinedVariable SubModel.twitchid == twitch_id).exists() # @UndefinedVariable
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 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)))