User Tools

Site Tools


python:deezer-album-tracker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:deezer-album-tracker [2024/03/30 12:08] Wulf Rajekpython:deezer-album-tracker [2024/04/21 20:05] (current) Wulf Rajek
Line 25: Line 25:
 Example output: Example output:
 <code> <code>
-./dat.py --days 7+$ /dat.py --days 7
 Albums released in the past 7 days: Albums released in the past 7 days:
-Release Date: 2024-03-29 
-Artist: Beyoncé 
-Album Name: COWBOY CARTER 
-Link: https://www.deezer.com/album/565889181 
  
-Release Date: 2024-03-29 +Release Date: 2024-04-19 
-Artist: KALEO +Artist: Staind 
-Album Name: Lonely Cowboy +Album Name: Better Days (feat. Dorothy) (1 track) 
-Link: https://www.deezer.com/album/560928422+Link: https://www.deezer.com/album/571491071
  
-Release Date: 2024-03-29 +Release Date: 2024-04-19 
-Artist: Lauren Daigle +Artist: Distilled Harmony 
-Album Name: Be Okay +Album Name: Nova (3 tracks) 
-Link: https://www.deezer.com/album/563282112+Link: https://www.deezer.com/album/572632871 
 + 
 +Release Date: 2024-04-19 
 +Artist: Taylor Swift 
 +Album Name: THE TORTURED POETS DEPARTMENT [EXPLICIT] (16 tracks) 
 +Link: https://www.deezer.com/album/574109801
  
 </code> </code>
Line 87: Line 88:
 import time import time
 import argparse import argparse
-from fuzzywuzzy import fuzz +from fuzzywuzzy import fuzzprocess
-from fuzzywuzzy import process+
 import os import os
  
 # Constants for file paths # Constants for file paths
-CONFIG_FILE = "config.json"+CONFIG_FILENAME = "config.json" 
 +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 
 +CONFIG_FILE = os.path.join(SCRIPT_DIR, CONFIG_FILENAME) 
  
 def load_config(): def load_config():
Line 100: Line 103:
             "global": {             "global": {
                 "days": "180"                 "days": "180"
-            },            +            },
             "email": {             "email": {
                 "smtp_server": "smtp.example.com",                 "smtp_server": "smtp.example.com",
Line 116: Line 119:
     with open(CONFIG_FILE, "r") as config_file:     with open(CONFIG_FILE, "r") as config_file:
         return json.load(config_file)         return json.load(config_file)
 +
  
 def save_config(config): def save_config(config):
     with open(CONFIG_FILE, "w") as config_file:     with open(CONFIG_FILE, "w") as config_file:
         json.dump(config, config_file, indent=4)         json.dump(config, config_file, indent=4)
 +
  
 def send_email(body): def send_email(body):
Line 151: Line 156:
         server.login(sender_email, sender_password)         server.login(sender_email, sender_password)
         server.send_message(msg)         server.send_message(msg)
 +
  
 def get_artist_name(artist_id): def get_artist_name(artist_id):
Line 159: Line 165:
         return data.get('name', '')         return data.get('name', '')
     return ''     return ''
 +
  
 def get_artist_id(artist_name): def get_artist_id(artist_name):
Line 169: Line 176:
                 return artist['id']                 return artist['id']
     return None     return None
 +
  
 def get_albums(artist_ids, lookupdays): def get_albums(artist_ids, lookupdays):
Line 178: Line 186:
  
     today = datetime.today().strftime('%Y-%m-%d')     today = datetime.today().strftime('%Y-%m-%d')
-    +
     for artist_id in artist_ids:     for artist_id in artist_ids:
         url = base_url.format(artist_id)         url = base_url.format(artist_id)
Line 188: Line 196:
             for album in data['data']:             for album in data['data']:
                 release_date = datetime.strptime(album['release_date'], '%Y-%m-%d')                 release_date = datetime.strptime(album['release_date'], '%Y-%m-%d')
-                if datetime.strptime(earliest_release, '%Y-%m-%d') <= release_date <= datetime.strptime(today, '%Y-%m-%d'):+                if (datetime.strptime(earliest_release, '%Y-%m-%d') <= release_date <= 
 +                        datetime.strptime(today, '%Y-%m-%d')): 
 +                    trackresponse = requests.get(album['tracklist']) 
 +                    request_count += 1 
 +                    if trackresponse.status_code == 200: 
 +                        tracklist = trackresponse.json() 
 +                        trackamount = tracklist['total'
 +                    else: 
 +                        trackamount = 0 
                     albums.append({                     albums.append({
                         'artist': artist_name,                         'artist': artist_name,
                         'album_name': album['title'],                         'album_name': album['title'],
                         'release_date': album['release_date'],                         'release_date': album['release_date'],
 +                        'trackamount': trackamount,
                         'explicit_lyrics': album['explicit_lyrics'],                         'explicit_lyrics': album['explicit_lyrics'],
-                        'link': album['link']+                        'link': album['link']
-                        'cover_small': album['cover_small'], +
-                        'cover_medium': album['cover_medium'],+
                     })                     })
-                    +
         # Deezer rate limit is 50 requests / 5 seconds. Limiting to 40/5 here:         # Deezer rate limit is 50 requests / 5 seconds. Limiting to 40/5 here:
         # Check if 40 requests have been made in less than 5 seconds         # Check if 40 requests have been made in less than 5 seconds
Line 208: Line 224:
             request_count = 0             request_count = 0
             start_time = time.time()             start_time = time.time()
-                        +
     return sorted(albums, key=lambda x: x['release_date'], reverse=True)     return sorted(albums, key=lambda x: x['release_date'], reverse=True)
 +
  
 def list_artists(): def list_artists():
Line 217: Line 234:
     for artist_id, artist_name in sorted_artists.items():     for artist_id, artist_name in sorted_artists.items():
         print(f"{artist_name}  ({artist_id})")         print(f"{artist_name}  ({artist_id})")
 +
  
 def add_artist(artist_name): def add_artist(artist_name):
Line 228: Line 246:
     else:     else:
         print("Artist not found.")         print("Artist not found.")
 +
  
 def delete_artist(search_term): def delete_artist(search_term):
Line 237: Line 256:
     for index, (artist_name, score) in enumerate(choices):     for index, (artist_name, score) in enumerate(choices):
         print(f"{index + 1}. {artist_name} ({score})")         print(f"{index + 1}. {artist_name} ({score})")
-    choice_index int(input("Enter the number of the artist to delete: ")) - 1 +    choice_input = input("Enter the number of the artist to delete: ") 
-    if 0 <= choice_index < len(choices): +    if choice_input.isnumeric(): 
-        artist_name = choices[choice_index][0] +        choice_index = int(choice_input) - 1 
-        artist_id = [key for key, value in subscribed_artists.items() if value == artist_name][0] +        if 0 <= choice_index < len(choices): 
-        del config[artist_id] +            artist_name = choices[choice_index][0] 
-        save_config(config) +            artist_id = [key for key, value in subscribed_artists.items() if value == artist_name][0] 
-        print(f"Artist '{artist_name}' deleted successfully.")+            del config['artist_ids'][artist_id] 
 +            save_config(config) 
 +            print(f"Artist '{artist_name}' deleted successfully.") 
 +        else: 
 +            print("Invalid choice.")
     else:     else:
-        print("Invalid choice.")+        print("No number entered.") 
  
 def main(): def main():
Line 272: Line 296:
         albums = get_albums(artist_ids, lookupdays)         albums = get_albums(artist_ids, lookupdays)
  
-        print(f"Albums released in the past {lookupdays} days:")+        output = f"Albums released in the past {lookupdays} days:\n\n"
         for album in albums:         for album in albums:
-            print("Release Date:", album['release_date']) +            output += f"Release Date: {album['release_date']}\n" 
-            print("Artist:", album['artist']) +            output += f"Artist: {album['artist']}\n" 
-            if album['explicit_lyrics'== 'true': +            output +f"Album Name: {album['album_name']}" 
-                print("Album Name:", album['album_name']" [EXPLICIT]") +            if album['explicit_lyrics'] is True: 
-            else+                output += " [EXPLICIT]" 
-                print("Album Name:", album['album_name']) +            if album['trackamount'] > 0
-            print("Link:", album['link']) +                output += f" ({album['trackamount']} track" 
-            print()+                if album['trackamount'> 1: 
 +                    output += "s" 
 +                output += ")" 
 +            output += "\n" 
 +            output += f"Link: {album['link']}\n" 
 +            output += "\n" 
 +        print(output)
  
         if args.email:         if args.email:
-            email_body = f"Albums released in the past {lookupdays} days:\n\n"+"\n".join([f"Release Date: {album['release_date']}\nArtist: {album['artist']}\nAlbum Name: {album['album_name']}\nLink: {album['link']}\n" for album in albums]+            send_email(output
-            send_email(email_body)+
  
 if __name__ == "__main__": if __name__ == "__main__":
     main()     main()
 +
 </code> </code>
  
python/deezer-album-tracker.1711800538.txt.gz · Last modified: 2024/03/30 12:08 by Wulf Rajek