2024-12-06 16:16:47 -05:00
import os
import sqlite3
import requests
2024-12-06 16:52:35 -05:00
import datetime
2024-12-06 16:16:47 -05:00
2024-12-09 12:33:05 -05:00
def read_music_database ( ) :
2024-12-06 16:40:53 -05:00
conn = sqlite3 . connect ( " assets/localwrapped/music.db " )
2024-12-06 16:16:47 -05:00
c = conn . cursor ( )
c . execute ( ' SELECT * FROM music ' )
music_list = c . fetchall ( )
conn . close ( )
return music_list
2024-12-09 13:17:07 -05:00
def read_count_database ( year : int ) :
2024-12-09 12:33:05 -05:00
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
2024-12-06 16:16:47 -05:00
2024-12-09 12:33:05 -05:00
def total_play_time ( year : int ) :
2024-12-06 16:16:47 -05:00
total_time = 0
2024-12-09 13:17:07 -05:00
for countvar in read_count_database ( year ) :
for musicvar in read_music_database ( ) :
2024-12-06 16:16:47 -05:00
if countvar [ 0 ] == musicvar [ 0 ] :
total_time + = float ( musicvar [ - 1 ] ) * countvar [ 1 ]
return total_time
2024-12-09 13:17:07 -05:00
def generatehtmlcode ( year : int = datetime . datetime . now ( ) . year ) :
2024-12-06 16:16:47 -05:00
html = ' <DOCTYPE html> \n <html> \n '
html + = ' <head> \n <meta charset= " UTF-8 " > \n <link href= " https://fonts.googleapis.com/css2?family=Jetbrains+Mono:wght@400;700&display=swap " rel= " stylesheet " > \n </head> \n '
html + = ' <body style= " background-color: #1f1f1f; color: #ffffff; font-family: \' Jetbrains Mono \' , monospace; " > \n <meta name= " viewport " content= " width=device-width, initial-scale=1.0 " > \n '
html + = ' <title>Halhadus \' Local Wrapped</title> \n <link rel= " icon " type= " image/png " href= " assets/favicon.png " > \n '
html + = ' <meta name= " description " content= " Halhadus \' Local Wrapped " > \n <meta property= " og:title " content= " Halhadus \' Local Wrapped " > \n '
html + = ' <meta property= " og:description " content= " Halhadus \' Local Wrapped " > \n <meta property= " og:image " content= " assets/favicon.png " > \n '
html + = ' <meta property= " og:url " content= " https://halhadus.rocks/localwrapped.html " > \n '
html + = ' <h1>Halhadus \' Local Wrapped</h1> \n '
2024-12-06 17:05:37 -05:00
html + = ' <a href= " https://git.halhadus.rocks/Halhadus/my-local-wrapped " style= " color: #ffffff; " >Source Code</a> \n '
2024-12-09 12:33:05 -05:00
html + = ' <h3>Hi guys. I am too lazy to add links to song names. To find which song is it, check <a href= " musiclist.html " style= " color: #ffffff; " >this link</a>.</h3> \n '
html + = ' <h2> ' + str ( year ) + ' </h2> \n '
2024-12-06 17:05:37 -05:00
html + = ' <form action= " index.html " > \n <input type= " submit " value= " Back to main page " style= " background-color: #1f1f1f; color: #ffffff; border: 2px solid #ffffff; border-radius: 5px; padding: 10px 20px; margin: 10px 0px; " > \n </form> \n '
2024-12-09 13:17:07 -05:00
html + = ' <h2>Total Play Time: ' + str ( int ( total_play_time ( datetime . datetime . now ( ) . year ) / 60 ) ) + ' minutes</h2> \n '
2024-12-09 12:33:05 -05:00
if year == datetime . datetime . now ( ) . year :
2024-12-09 12:59:39 -05:00
html + = ' <h2>Last Updated: ' + datetime . datetime . utcfromtimestamp ( os . path . getmtime ( ' assets/localwrapped/count.db ' ) ) . strftime ( ' % Y- % m- %d % H: % M: % S ' ) + ' (UTC +0, I hope.)</h2> \n ' ;
2024-12-09 12:33:05 -05:00
else :
2024-12-09 12:59:39 -05:00
html + = ' <h2>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.)</h2> \n '
2024-12-06 16:16:47 -05:00
html + = ' <h3>Most Played Musics</h3> \n '
html + = ' <ol> \n '
count_list_formatted = [ ]
2024-12-09 13:17:07 -05:00
for music in read_count_database ( year ) :
2024-12-06 16:16:47 -05:00
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 + = ' <li> ' + music [ 0 ] + ' | ' + str ( music [ 1 ] ) + ' times</li> \n '
html + = ' </ol> \n '
html + = ' </body> \n </html> '
return html