def getuserID(self, username): """Get the twitch id (numbers) from username.""" u_name = sanitizeUserName(username) if u_name in self.userNametoID: return self.userNametoID[u_name] else: logging.info( "User data not in cache when trying to access user ID. User tag {}" .format(username)) try: data = self.getuserTag(username) id = data["users"][0]["_id"] display_name = data["users"][0]["display_name"] # update cache as well self.updateCacheData(u_name, display_name, id) return id except (ValueError, KeyError) as e: logging.info( "Cannot get user info from API call, can't get user ID of {}" .format(username)) raise e except (IndexError, RequestException): logging.info("Seems no such user as {}".format(username)) raise UserNotFoundError( "No user with login id of {}".format(username))
def userState(self, prefix, tags): """Track user tags.""" # NOTE: params in PRIVMSG() are already processed and does not contains these data, # so we have to get them from lineReceived() -> manually called userState() to parse the tags. # Also I don't want to crash the original PRIVMSG() functions by modifing them twitch_user_tag = prefix.split("!")[0] # also known as login id twitch_user_id = tags["user-id"] display_name = tags["display-name"] name = sanitizeUserName(twitch_user_tag) self.updateCacheData(name, display_name, twitch_user_id) if 'subscriber' in tags: if tags['subscriber'] == '1': self.subs.add(name) elif name in self.subs: self.subs.discard(name) if 'user-type' in tags: # This also works #if tags['user-type'] == 'mod': if tags['mod'] == '1': self.mods.add(name) elif name in self.mods: self.mods.discard(name)
def run(self, bot, user, msg, tag_info): """Calculate rank of user. 0-19: Rank 25, 20-39: Rank 24,..., 480-499: Rank 1 >= LEGENDP: Legend """ self.responses = bot.responses["Rank"] if msg.startswith('!rank '): user = sanitizeUserName(msg.split(' ')[1]) if user in bot.displayNameToUserName: # force display name to login id ... if that user is in our cache user = bot.displayNameToUserName[user] # code may break in this case # if user input !rank XXXX, where XXXX is a display name, but that user # does not show up in chat so that we can't get his login_id try: points = bot.ranking.getPoints(user) var = { "<USER>": bot.displayName(user), "<RANK>": bot.ranking.getHSRank(points), "<POINTS>": points } bot.write( bot.replace_vars(self.responses["display_rank"]["msg"], var)) except UserNotFoundError: bot.write(self.responses["user_not_found"]["msg"])
def getUserDataFromID(self, user_id): """Get Twitch user data of a given id.""" url = USER_ID_API.format(user_id) data = self.getJSONObjectFromTwitchAPI(url) u_name = sanitizeUserName(data["name"]) display_name = data["display_name"] id = data["_id"] self.updateCacheData(u_name, display_name, id) return data
def displayName(self, username): """Get the proper capitalization of a twitch user.""" u_name = sanitizeUserName(username) if u_name in self.userNametoDisplayName: return self.userNametoDisplayName[u_name] else: try: logging.info( "User data not in cache when trying to access user display name, user tag is {}" .format(username)) name = self.getuserTag(u_name)["users"][0]["display_name"] # save the record as well self.userNametoDisplayName[username] = name return name except (RequestException, IndexError, KeyError): logging.info( "Cannot get user info from API call, have to return username directly" ) return username