Refactored to own logger

This commit is contained in:
agres
2025-03-23 23:19:12 +01:00
parent 87f84b250e
commit db983cfc8f
+28 -25
View File
@@ -1,15 +1,16 @@
import json import json
import logging as log
import os import os
from auth import simple_authenticate from auth import simple_authenticate
from database_handler import Database, Table from database_handler import Database, Table
from logger import LoggerWrapper
from spotify_api import get_multiple_field_information from spotify_api import get_multiple_field_information
# Define the absolute folder path to the folder containing the gdrp retrieved data # Define the absolute folder path to the folder containing the gdrp retrieved data
folder_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'data', 'gdpr_data') folder_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'data', 'gdpr_data')
# Define the db # Define the db
db = Database() db = Database()
log = LoggerWrapper()
def _read_gdrp_data() -> list: def _read_gdrp_data() -> list:
@@ -20,32 +21,34 @@ def _read_gdrp_data() -> list:
:return: all_songs_played: A dict with an items field containing all songs played for the user :return: all_songs_played: A dict with an items field containing all songs played for the user
""" """
all_songs_played = [] all_songs_played = []
try:
for filename in os.listdir(folder_path):
for filename in os.listdir(folder_path): if filename.endswith('.json'):
file_path = os.path.join(folder_path, filename)
if filename.endswith('.json'): with open(file_path, 'r') as file:
file_path = os.path.join(folder_path, filename) data = json.load(file)
with open(file_path, 'r') as file: for entry in data:
data = json.load(file) # This removes all podcasts from the list
if entry['spotify_track_uri'] is None:
for entry in data: continue
# This removes all podcasts from the list try:
if entry['spotify_track_uri'] is None: track = {
continue 'timestamp': entry['ts'],
try: 'id': _extract_id(entry['spotify_track_uri']),
track = { 'track_name': entry['master_metadata_track_name'],
'timestamp': entry['ts'], 'artist_name': entry['master_metadata_album_artist_name'],
'id': _extract_id(entry['spotify_track_uri']), 'album_name': entry['master_metadata_album_album_name'],
'track_name': entry['master_metadata_track_name'], 'conn_country': entry['conn_country'],
'artist_name': entry['master_metadata_album_artist_name'], 'ms_played': entry['ms_played']
'album_name': entry['master_metadata_album_album_name'], }
'conn_country': entry['conn_country'], all_songs_played.append(track)
'ms_played': entry['ms_played'] except Exception as e:
} log.warning(f'Missing field from gdpr data: {e}')
all_songs_played.append(track) except Exception as e:
except Exception as e: log.error(f'Failed to read gdpr data: {e}')
print(f'Missing field: {e}')
all_songs_played = sorted(all_songs_played, key=lambda x: x['timestamp']) all_songs_played = sorted(all_songs_played, key=lambda x: x['timestamp'])
return all_songs_played return all_songs_played
@@ -139,7 +142,7 @@ def _insert_data_into_db(all_songs_played: list):
log.error(f'Failed adding {entry} to database, error {e}') log.error(f'Failed adding {entry} to database, error {e}')
def export_gdpr_data(n_limit: int = 100): def export_gdpr_data(n_limit: int = 100) -> None:
all_songs_played = _read_gdrp_data() all_songs_played = _read_gdrp_data()
all_songs_played = all_songs_played[-n_limit:] all_songs_played = all_songs_played[-n_limit:]
all_songs_catalogued = _populate_ids(all_songs_played) all_songs_catalogued = _populate_ids(all_songs_played)