import os import sqlite3 import requests import datetime def read_music_database(): conn = sqlite3.connect("assets/localwrapped/music.db") c = conn.cursor() c.execute('SELECT * FROM music') music_list = c.fetchall() conn.close() return music_list def read_count_database(year: int = datetime.datetime.now().year): if year == datetime.datetime.now().year: conn = sqlite3.connect(f'assets/localwrapped/count.db') c = conn.cursor() c.execute('SELECT * FROM count') count_list = c.fetchall() conn.close() return count_list else: conn = sqlite3.connect(f'assets/localwrapped/count-{year}.db') c = conn.cursor() c.execute('SELECT * FROM count') count_list = c.fetchall() conn.close() return count_list def total_play_time(year: int): total_time = 0 for countvar in read_count_database(): for musicvar in read_music_database(year): if countvar[0] == musicvar[0]: total_time += float(musicvar[-1]) * countvar[1] return total_time def generatehtmlcode(year: int): html = '\n\n' html += '\n\n\n\n' html += '\n\n' html += 'Halhadus\' Local Wrapped\n\n' html += '\n\n' html += '\n\n' html += '\n' html += '

Halhadus\' Local Wrapped

\n' html += 'Source Code\n' html += '

Hi guys. I am too lazy to add links to song names. To find which song is it, check this link.

\n' html += '

' + str(year) + '

\n' html += '
\n\n
\n' html += '

Total Play Time: ' + str(int(total_play_time(url)/60)) + ' minutes

\n' if year == datetime.datetime.now().year: html += '

Last Updated: ' + datetime.datetime.utcfromtimestamp(os.path.getmtime('assets/localwrapped/count.db').strftime('%Y-%m-%d %H:%M:%S') + ' (UTC +0, I hope.)

\n' else: html += '

Last Updated: ' + datetime.datetime.utcfromtimestamp(os.path.getmtime(f'assets/localwrapped/count-{year}.db').strftime('%Y-%m-%d %H:%M:%S') + ' (UTC +0, I hope.)

\n' html += '

Most Played Musics

\n' html += '
    \n' count_list_formatted = [] for music in read_count_database(): music_name = music[0] count = music[1] count_list_formatted.append([music_name, count]) count_list_formatted.sort(key=lambda x: x[1], reverse=True) for music in count_list_formatted: html += '
  1. ' + music[0] + ' | ' + str(music[1]) + ' times
  2. \n' html += '
\n' html += '\n' return html