python:deezer-album-tracker
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:deezer-album-tracker [2024/03/29 19:06] – Wulf Rajek | python:deezer-album-tracker [2024/04/21 20:05] (current) – Wulf Rajek | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| This script uses the deezer public api to provide a list of albums released the past half year of artists in the configuration file. Configuration file will be created if it doesn' | This script uses the deezer public api to provide a list of albums released the past half year of artists in the configuration file. Configuration file will be created if it doesn' | ||
| + | |||
| + | Depending on network speed and amount of albums per artist and due to deezer API rate limit of max 50 requests per 5 seconds, querying 180 days for 235 artists takes about 1:45 minutes. | ||
| Prerequisites for fuzzy search: | Prerequisites for fuzzy search: | ||
| Line 23: | Line 25: | ||
| Example output: | Example output: | ||
| < | < | ||
| - | $ ./dat.py | + | $ / |
| - | Albums released in the past 6 months: | + | Albums released in the past 7 days: |
| - | Release Date: 2024-01-12 | + | |
| - | Artist: | + | Release Date: 2024-04-19 |
| - | Album Name: Scars (feat. | + | Artist: |
| + | Album Name: Better Days (feat. | ||
| + | Link: https:// | ||
| + | |||
| + | Release Date: 2024-04-19 | ||
| + | Artist: Distilled Harmony | ||
| + | Album Name: Nova (3 tracks) | ||
| + | Link: https:// | ||
| - | Release Date: 2023-11-03 | + | Release Date: 2024-04-19 |
| - | Artist: | + | Artist: |
| - | Album Name: Counterfeit Countdown | + | Album Name: THE TORTURED POETS DEPARTMENT [EXPLICIT] (16 tracks) |
| + | Link: https:// | ||
| - | Release Date: 2023-10-31 | ||
| - | Artist: Papa Roach | ||
| - | Album Name: Leave a Light On (Talk Away The Dark) | ||
| </ | </ | ||
| Line 81: | Line 88: | ||
| import time | import time | ||
| import argparse | import argparse | ||
| - | from fuzzywuzzy import fuzz | + | from fuzzywuzzy import fuzz, process |
| - | from fuzzywuzzy import | + | |
| import os | import os | ||
| # Constants for file paths | # Constants for file paths | ||
| - | CONFIG_FILE | + | CONFIG_FILENAME |
| + | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
| + | CONFIG_FILE = os.path.join(SCRIPT_DIR, | ||
| def load_config(): | def load_config(): | ||
| Line 94: | Line 103: | ||
| " | " | ||
| " | " | ||
| - | }, | + | }, |
| " | " | ||
| " | " | ||
| Line 110: | Line 119: | ||
| with open(CONFIG_FILE, | with open(CONFIG_FILE, | ||
| return json.load(config_file) | return json.load(config_file) | ||
| + | |||
| def save_config(config): | def save_config(config): | ||
| Line 115: | Line 125: | ||
| json.dump(config, | json.dump(config, | ||
| - | def send_email(subject, | + | |
| + | def send_email(body): | ||
| config = load_config() | config = load_config() | ||
| email_config = config.get(' | email_config = config.get(' | ||
| Line 126: | Line 137: | ||
| sender_email = email_config.get(' | sender_email = email_config.get(' | ||
| sender_password = email_config.get(' | sender_password = email_config.get(' | ||
| + | email_subject = config.get(' | ||
| msg = MIMEMultipart() | msg = MIMEMultipart() | ||
| msg[' | msg[' | ||
| msg[' | msg[' | ||
| - | msg[' | + | msg[' |
| body = MIMEText(body) | body = MIMEText(body) | ||
| Line 144: | Line 156: | ||
| server.login(sender_email, | server.login(sender_email, | ||
| server.send_message(msg) | server.send_message(msg) | ||
| + | |||
| def get_artist_name(artist_id): | def get_artist_name(artist_id): | ||
| Line 152: | Line 165: | ||
| return data.get(' | return data.get(' | ||
| return '' | return '' | ||
| + | |||
| def get_artist_id(artist_name): | def get_artist_id(artist_name): | ||
| Line 163: | Line 177: | ||
| return None | return None | ||
| - | def get_albums(artist_ids, | + | |
| - | config = load_config() | + | def get_albums(artist_ids, |
| base_url = " | base_url = " | ||
| earliest_release = (datetime.now() - timedelta(days=lookupdays)).strftime(' | earliest_release = (datetime.now() - timedelta(days=lookupdays)).strftime(' | ||
| Line 170: | Line 184: | ||
| request_count = 0 | request_count = 0 | ||
| start_time = time.time() | start_time = time.time() | ||
| + | |||
| + | today = datetime.today().strftime(' | ||
| for artist_id in artist_ids: | for artist_id in artist_ids: | ||
| Line 180: | Line 196: | ||
| for album in data[' | for album in data[' | ||
| release_date = datetime.strptime(album[' | release_date = datetime.strptime(album[' | ||
| - | if release_date | + | if (datetime.strptime(earliest_release, |
| + | | ||
| + | trackresponse = requests.get(album[' | ||
| + | request_count += 1 | ||
| + | if trackresponse.status_code == 200: | ||
| + | tracklist = trackresponse.json() | ||
| + | trackamount = tracklist[' | ||
| + | else: | ||
| + | trackamount = 0 | ||
| albums.append({ | albums.append({ | ||
| ' | ' | ||
| ' | ' | ||
| - | ' | + | ' |
| + | ' | ||
| + | ' | ||
| + | ' | ||
| }) | }) | ||
| - | | + | |
| # 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 196: | Line 224: | ||
| request_count = 0 | request_count = 0 | ||
| start_time = time.time() | start_time = time.time() | ||
| - | | + | |
| return sorted(albums, | return sorted(albums, | ||
| + | |||
| def list_artists(): | def list_artists(): | ||
| Line 205: | Line 234: | ||
| for artist_id, artist_name in sorted_artists.items(): | for artist_id, artist_name in sorted_artists.items(): | ||
| print(f" | print(f" | ||
| + | |||
| def add_artist(artist_name): | def add_artist(artist_name): | ||
| Line 216: | Line 246: | ||
| else: | else: | ||
| print(" | print(" | ||
| + | |||
| def delete_artist(search_term): | def delete_artist(search_term): | ||
| Line 225: | Line 256: | ||
| for index, (artist_name, | for index, (artist_name, | ||
| print(f" | print(f" | ||
| - | | + | |
| - | 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" | + | del config[' |
| + | save_config(config) | ||
| + | print(f" | ||
| + | else: | ||
| + | print(" | ||
| else: | else: | ||
| - | print(" | + | print(" |
| def main(): | def main(): | ||
| Line 255: | Line 291: | ||
| artist_ids = config.get(' | artist_ids = config.get(' | ||
| if args.days: | if args.days: | ||
| - | lookupdays=int(args.days) | + | lookupdays = int(args.days) |
| else: | else: | ||
| - | lookupdays=config.get(' | + | lookupdays = config.get(' |
| - | albums = get_albums(artist_ids, | + | albums = get_albums(artist_ids, |
| - | | + | |
| for album in albums: | for album in albums: | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | print() | + | if album[' |
| + | output += " [EXPLICIT]" | ||
| + | if album[' | ||
| + | output += f" ({album[' | ||
| + | if album[' | ||
| + | output += " | ||
| + | output += ")" | ||
| + | | ||
| + | output += f" | ||
| + | output += " | ||
| + | | ||
| if args.email: | if args.email: | ||
| - | | + | |
| - | | + | |
| - | email_body = f" | + | |
| - | send_email(email_subject, | + | |
| if __name__ == " | if __name__ == " | ||
| main() | main() | ||
| + | |||
| </ | </ | ||
python/deezer-album-tracker.1711739190.txt.gz · Last modified: by Wulf Rajek