def serverInfo(guildId, channelId): # Auth and session token = session.get('oauth2_token') (user, usertoken) = get_user_cache(token, cache) check_auth(user, usertoken) channelId = channelId if (channelId != None) else guildId mysqldb = MySQL(current_app.config['DB_HOST'], current_app.config['DB_USER'], current_app.config['DB_PASS']) mysqldb.connect() messages = mysqldb.getMessages(channelId) nbMessages = len(messages) # API calls headers = {'authorization': usertoken} channel = requests.get(API_BASE_URL + '/channels/' + channelId, headers=headers).json() guild = requests.get(API_BASE_URL + '/guilds/' + guildId, headers=headers).json() channels = requests.get(API_BASE_URL + '/guilds/' + guildId + '/channels', headers=headers).json() cache['guilds'][guildId] = guild guild = cache['guilds'][channel['guild_id']] data = { 'guildId': guildId, 'channelId': channelId, 'channels': channels } channelinfos = { 'nbmessages' : nbMessages, 'nbchannels' : 0, 'firstmessage' : mysqldb.getFirstMessage(channelId), 'lastmessage' : mysqldb.getLastMessage(channelId) } # Compute things # Render return render_template("layout.html", contentTemplate="server-info.html", user=user, server=guild, data=data, channelinfos=channelinfos)
def compute(guildId): # Auth and session token = session.get('oauth2_token') (user, usertoken) = get_user_cache(token, cache) # Logic way = request.args.get('time') channelId = request.args.get('channelId') mysqldb = MySQL(current_app.config['DB_HOST'], current_app.config['DB_USER'], current_app.config['DB_PASS']) mysqldb.connect() params = {} nbMessagesStr = request.args.get('number') if nbMessagesStr is not None: nbMessagesLeft = int(request.args.get('number')) else: nbMessagesLeft = 100 if nbMessagesLeft < 0: nbMessagesLeft = 100 if nbMessagesLeft > 1000: nbMessagesLeft = 1000 # API calls headers = {'authorization': usertoken} first = True while nbMessagesLeft > 0: if not first: time.sleep(1) first = False nbToTake = nbMessagesLeft if nbMessagesLeft > 100: nbToTake = 100 lastMessageId = mysqldb.getLastMessage(channelId) if lastMessageId is None: channel = requests.get(API_BASE_URL + '/channels/' + channelId, headers=headers).json() params = {'around': int(channel['last_message_id']), 'limit': nbToTake} elif way == 'after': lastMessageId = mysqldb.getLastMessage(channelId) params = {'after': int(lastMessageId['id']), 'limit': nbToTake} elif way == 'before': firstMessageId = mysqldb.getFirstMessage(channelId) params = {'before': int(firstMessageId['id']), 'limit': nbToTake} else: abort(412, "Incorrect 'time' parameter.") return messages = requests.get(API_BASE_URL + '/channels/' + channelId + '/messages', headers=headers, params=params).json() def take8(l): return [l[0], l[1][:8]] toInsert = [(str(message['id']), str(guildId), str(channelId), str(message['author']['id']), str(message['content']), str(' '.join(take8(message['timestamp'].split('T')))), str(message['author']['username']), message['author']['discriminator'], str(message['author']['avatar'])) for message in messages] # DB Fill returnValue = mysqldb.insertMessages(toInsert) nbMessagesLeft -= nbToTake # Render json = { 'result': 'ok', 'message': 'Successfully inserted messages' } return jsonify(json)