mirror of
https://github.com/agresdominik/predictify.git
synced 2026-04-21 17:55:49 +00:00
Big commit
This commit is contained in:
@@ -71,10 +71,13 @@ class Database:
|
||||
|
||||
def add_row(self, table: Table, values):
|
||||
"""Add a new row into the specified table"""
|
||||
try:
|
||||
placeholders = ', '.join(['?'] * len(values))
|
||||
query = f"INSERT INTO {table.value} VALUES ({placeholders})"
|
||||
self.cursor.execute(query, values)
|
||||
self.conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
def read_all_rows(self, table: Table, column: str = "*"):
|
||||
"""Read all rows from the specified table"""
|
||||
@@ -85,3 +88,42 @@ class Database:
|
||||
def close(self):
|
||||
"""Close the database connection"""
|
||||
self.conn.close()
|
||||
|
||||
def get_total_overview(self) -> list:
|
||||
"""Retrieve a total overview of all recently played songs with full details"""
|
||||
try:
|
||||
# Join recently_played with track_information, artist_information, and album_information
|
||||
query = f'''
|
||||
SELECT rp.played_at,
|
||||
ti.track_id,
|
||||
ti.title,
|
||||
ai.artist_id,
|
||||
ai.artist_name,
|
||||
al.album_id,
|
||||
al.album_name
|
||||
FROM {Table.RECENTLY_PLAYED.value} rp
|
||||
JOIN {Table.TRACK_INFORMATION.value} ti ON rp.track_id = ti.track_id
|
||||
JOIN {Table.ARTIST_INFORMATION.value} ai ON rp.artist_id = ai.artist_id
|
||||
JOIN {Table.ALBUM_INFORMATION.value} al ON rp.album_id = al.album_id
|
||||
ORDER BY rp.played_at DESC
|
||||
'''
|
||||
self.cursor.execute(query)
|
||||
rows = self.cursor.fetchall()
|
||||
return rows
|
||||
except Exception as e:
|
||||
print(f"Error retrieving total overview: {e}")
|
||||
return []
|
||||
|
||||
"""
|
||||
print(rows)
|
||||
|
||||
if rows:
|
||||
print(f"{'Played At':<20} {'Track ID':<20} {'Track Title':<50} {'Artist ID':<20} {'Artist Name':<50} {'Album ID':<20} {'Album Name':<50}")
|
||||
print("-" * 160)
|
||||
for row in rows:
|
||||
played_at, track_id, title, artist_id, artist_name, album_id, album_name = row
|
||||
print(f"{played_at:<20} {track_id:<20} {title:<50} {artist_id:<20} {artist_name:<50} {album_id:<20} {album_name:<50}")
|
||||
else:
|
||||
print("No recently played songs found.")
|
||||
except Exception as e:
|
||||
print(f"Error retrieving total overview: {e}")"""
|
||||
|
||||
+59
-5
@@ -18,12 +18,28 @@ def main():
|
||||
# Once each 30 mins
|
||||
_read_recently_played_page_and_add_to_db(bearer_token=bearer_token)
|
||||
|
||||
bearer_token_simple = simple_authenticate()
|
||||
# Once a day
|
||||
all_track_ids = db.read_all_rows(Table.RECENTLY_PLAYED, 'track_id')
|
||||
bearer_toke_simple = simple_authenticate()
|
||||
for track_id in all_track_ids:
|
||||
response = _get_track_information(track_id=track_id[0], bearer_token=bearer_toke_simple)
|
||||
print(response)
|
||||
all_track_ids_recently_played = db.read_all_rows(Table.RECENTLY_PLAYED, 'track_id')
|
||||
all_track_ids_saved = db.read_all_rows(Table.TRACK_INFORMATION, 'track_id')
|
||||
all_track_ids_missing = list(set(all_track_ids_recently_played) - set(all_track_ids_saved))
|
||||
for track_id in all_track_ids_missing:
|
||||
response = _get_track_information(track_id=track_id[0], bearer_token=bearer_token_simple)
|
||||
db.add_row(Table.TRACK_INFORMATION, (response['id'], response['name']))
|
||||
# Once a day
|
||||
all_album_ids_recently_played = db.read_all_rows(Table.RECENTLY_PLAYED, 'album_id')
|
||||
all_album_ids_saved = db.read_all_rows(Table.ALBUM_INFORMATION, 'album_id')
|
||||
all_album_ids_missing = list(set(all_album_ids_recently_played) - set(all_album_ids_saved))
|
||||
for album_id in all_album_ids_missing:
|
||||
response = _get_album_information(album_id=album_id[0], bearer_token=bearer_token_simple)
|
||||
db.add_row(Table.ALBUM_INFORMATION, (response['id'], response['name']))
|
||||
# Once a day
|
||||
all_artist_ids_recently_played = db.read_all_rows(Table.RECENTLY_PLAYED, 'artist_id')
|
||||
all_artist_ids_saved = db.read_all_rows(Table.ARTIST_INFORMATION, 'artist_id')
|
||||
all_artist_ids_missing = list(set(all_artist_ids_recently_played) - set(all_artist_ids_saved))
|
||||
for artist_id in all_artist_ids_missing:
|
||||
response = _get_artist_information(artist_id=artist_id[0], bearer_token=bearer_token_simple)
|
||||
db.add_row(Table.ARTIST_INFORMATION, (response['id'], response['name']))
|
||||
|
||||
# Close the database connection
|
||||
db.close()
|
||||
@@ -81,5 +97,43 @@ def _get_track_information(track_id: str, bearer_token: str) -> dict:
|
||||
return response_json
|
||||
|
||||
|
||||
def _get_artist_information(artist_id: str, bearer_token: str) -> dict:
|
||||
"""
|
||||
This function returns the artist information based on the artist id
|
||||
|
||||
:param artist_id: str
|
||||
:param bearer_token: str
|
||||
:return: dict
|
||||
"""
|
||||
|
||||
url = f"https://api.spotify.com/v1/artists/{artist_id}"
|
||||
header = {
|
||||
'Authorization': f'Bearer {bearer_token}'
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=header)
|
||||
response_json = response.json()
|
||||
return response_json
|
||||
|
||||
|
||||
def _get_album_information(album_id: str, bearer_token: str) -> dict:
|
||||
"""
|
||||
This function returns the album information based on the album id
|
||||
|
||||
:param album_id: str
|
||||
:param bearer_token: str
|
||||
:return: dict
|
||||
"""
|
||||
|
||||
url = f"https://api.spotify.com/v1/albums/{album_id}"
|
||||
header = {
|
||||
'Authorization': f'Bearer {bearer_token}'
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=header)
|
||||
response_json = response.json()
|
||||
return response_json
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user