def random_gg_image(): random_idol_id = get_random_idol_id_with_photo() c.execute( "SELECT fullname, stagename FROM groupmembers.member WHERE id = %s", (random_idol_id, )) info = c.fetchone() full_name = info[0] stage_name = info[1] c.execute( "SELECT alias FROM groupmembers.aliases WHERE objectid = %s AND isgroup = 0 AND serverid IS NULL", (random_idol_id, )) aliases = c.fetchall() aliases = [alias[0] for alias in aliases] photo_link = ".mp4" # confirm the client does not receive a video. while ".mp4" in photo_link or ".webm" in photo_link: photo_link = get_idol_photo(random_idol_id, redirect_user=False, auth=True, guessing_game=True) idol_info_json = { 'id': random_idol_id, 'full_name': full_name, 'stage_name': stage_name, 'image_url': photo_link, 'aliases': aliases } return idol_info_json
def log_info(): """Log minimal request information to know amount of calls along with key usage.""" try: key = request.headers.get('Authorization') or "None" # keys are always appended to the end in order, so we can use the index to differentiate between keys. try: index = private_keys.index(key) except: index = -1 # c.execute("INSERT INTO ") c.execute( "SELECT called FROM stats.apiusage WHERE endpoint = %s AND keyused = %s", (request.base_url, index)) called_amount = c.fetchone() if called_amount: c.execute( "UPDATE stats.apiusage SET called = %s WHERE endpoint = %s AND keyused = %s", (called_amount[0] + 1, request.base_url, index)) else: c.execute( "INSERT INTO stats.apiusage(endpoint, keyused, called) VALUES(%s, %s, %s)", (request.base_url, index, 1)) db_conn.commit() except Exception as e: db_conn.commit( ) # will cause a transaction rollback / abort the current transaction. print(f"{e} - log_info")
def get_group(group_id): """Get group name by group id""" c.execute("SELECT groupname FROM groupmembers.groups WHERE groupid=%s", (group_id, )) group = c.fetchone() try: return {group_id: group[0]} except: return {}
def get_bot_info(): """Sends a list of bot information such as Server Count, User Count, Total commands used, Amount of Idol Photos """ c.execute("SELECT totalused FROM stats.sessions ORDER BY totalused DESC") total_commands_used = c.fetchone()[0] c.execute("SELECT COUNT(*) FROM stats.guilds") server_count = c.fetchone()[0] c.execute("SELECT SUM(membercount) FROM stats.guilds") member_count = c.fetchone()[0] c.execute("SELECT COUNT(*) FROM groupmembers.imagelinks") idol_photo_count = c.fetchone()[0] return { 'total_commands_used': total_commands_used, 'server_count': server_count, 'member_count': member_count, 'idol_photo_count': idol_photo_count }, 200
def get_image(image_id): # check authorization if not check_auth_key(request.headers.get('Authorization')): # Invalid API Key return Response(status=403) try: c.execute("SELECT link FROM groupmembers.imagelinks where id = %s", (image_id, )) link = c.fetchone() if not link: return Response(status=404) return process_image([image_id, link[0]]) except Exception as e: print(e) return Response(status=500)
def get_idol_commands_used(): """Get the Amount of Idol Photo Commands Used.""" c.execute( "SELECT SUM(count) FROM stats.commands WHERE commandname LIKE 'Idol %' OR commandname LIKE 'randomidol'" ) return {'idol_commands_used': c.fetchone()[0]}, 200