From 9f6a40c900827c2e6a64b55da44c51b4845d7b59 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Sat, 30 Oct 2021 15:35:01 +0300 Subject: [PATCH 1/3] Can now install different songs with the same name So basically now in each directory created by zspotify a .song_ids file will be created and appended to every a song is installed in that directory. Now, instead of checking if a file exists by name we're checking with song id. --- zspotify/track.py | 18 ++++++++++++++++-- zspotify/utils.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/zspotify/track.py b/zspotify/track.py index 57960496..2f4daaed 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -11,7 +11,8 @@ from tqdm import tqdm from const import TRACKS, ALBUM, NAME, ITEMS, DISC_NUMBER, TRACK_NUMBER, IS_PLAYABLE, ARTISTS, IMAGES, URL, \ RELEASE_DATE, ID, TRACKS_URL, SAVED_TRACKS_URL, SPLIT_ALBUM_DISCS, ROOT_PATH, DOWNLOAD_FORMAT, CHUNK_SIZE, \ SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME -from utils import sanitize_data, set_audio_tags, set_music_thumbnail, create_download_directory +from utils import sanitize_data, set_audio_tags, set_music_thumbnail, create_download_directory, \ + get_directory_song_ids, add_to_directory_song_ids from zspotify import ZSpotify @@ -74,6 +75,16 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', filename = os.path.join( download_directory, f'{song_name}.{EXT_MAP.get(ZSpotify.get_config(DOWNLOAD_FORMAT))}') + check_name = os.path.isfile(filename) and os.path.getsize(filename) + check_id = scraped_song_id in get_directory_song_ids(download_directory) + + # a song with the same name is installed + if not check_id and check_name: + # TODO: count songs with the same name but different id and add prefix based on that + filename = os.path.join( + download_directory, f'{song_name}_1.{EXT_MAP.get(ZSpotify.get_config(DOWNLOAD_FORMAT))}') + + except Exception as e: print('### SKIPPING SONG - FAILED TO QUERY METADATA ###') print(e) @@ -83,7 +94,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', print('\n### SKIPPING:', song_name, '(SONG IS UNAVAILABLE) ###') else: - if os.path.isfile(filename) and os.path.getsize(filename) and ZSpotify.get_config(SKIP_EXISTING_FILES): + if check_id and ZSpotify.get_config(SKIP_EXISTING_FILES): print('\n### SKIPPING:', song_name, '(SONG ALREADY EXISTS) ###') else: @@ -119,6 +130,9 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', release_year, disc_number, track_number) set_music_thumbnail(filename, image_url) + # add song id to download directory's .song_ids file + add_to_directory_song_ids(download_directory, scraped_song_id) + if not ZSpotify.get_config(OVERRIDE_AUTO_WAIT): time.sleep(ZSpotify.get_config(ANTI_BAN_WAIT_TIME)) except Exception as e: diff --git a/zspotify/utils.py b/zspotify/utils.py index b8188d95..e838bbb1 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -15,11 +15,38 @@ from const import SANITIZE, ARTIST, TRACKTITLE, ALBUM, YEAR, DISCNUMBER, TRACKNU class MusicFormat(str, Enum): MP3 = 'mp3', OGG = 'ogg', - + def create_download_directory(download_path: str) -> None: + """ Create directory and add a hidden file with song ids """ os.makedirs(download_path, exist_ok=True) + # add hidden file with song ids + hidden_file_path = os.path.join(download_path, '.song_ids') + if not os.path.isfile(hidden_file_path): + with open(hidden_file_path, 'w', encoding='utf-8') as f: + pass + +def get_directory_song_ids(download_path: str) -> list[str]: + """ Gets song ids of songs in directory """ + + song_ids = [] + + hidden_file_path = os.path.join(download_path, '.song_ids') + if os.path.isfile(hidden_file_path): + with open(hidden_file_path, 'r', encoding='utf-8') as file: + song_ids.extend([line.strip() for line in file.readlines()]) + + return song_ids + +def add_to_directory_song_ids(download_path: str, song_id: str) -> None: + """ Appends song_id to .song_ids file in directory """ + + hidden_file_path = os.path.join(download_path, '.song_ids') + # not checking if file exists because we need an exception + # to be raised if something is wrong + with open(hidden_file_path, 'a', encoding='utf-8') as file: + file.write(f'{song_id}\n') def wait(seconds: int = 3) -> None: """ Pause for a set number of seconds """ From 857b1d949a573a33141ea5197c4867a4a9cd50b5 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Sat, 30 Oct 2021 15:42:59 +0300 Subject: [PATCH 2/3] Fixed issue when deleting or moving file --- zspotify/track.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zspotify/track.py b/zspotify/track.py index 2f4daaed..0322f098 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -94,7 +94,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', print('\n### SKIPPING:', song_name, '(SONG IS UNAVAILABLE) ###') else: - if check_id and ZSpotify.get_config(SKIP_EXISTING_FILES): + if check_id and check_name and ZSpotify.get_config(SKIP_EXISTING_FILES): print('\n### SKIPPING:', song_name, '(SONG ALREADY EXISTS) ###') else: @@ -131,7 +131,8 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', set_music_thumbnail(filename, image_url) # add song id to download directory's .song_ids file - add_to_directory_song_ids(download_directory, scraped_song_id) + if not check_id: + add_to_directory_song_ids(download_directory, scraped_song_id) if not ZSpotify.get_config(OVERRIDE_AUTO_WAIT): time.sleep(ZSpotify.get_config(ANTI_BAN_WAIT_TIME)) From b340f92f293f7847c57b1a8e9465e49e5f99c7d7 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Sat, 30 Oct 2021 15:58:09 +0300 Subject: [PATCH 3/3] Added prefixes in case more than two songs have the same name --- zspotify/track.py | 7 +++++-- zspotify/utils.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zspotify/track.py b/zspotify/track.py index 0322f098..27aac958 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -1,4 +1,5 @@ import os +import re import time from typing import Any, Tuple, List @@ -80,9 +81,11 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', # a song with the same name is installed if not check_id and check_name: - # TODO: count songs with the same name but different id and add prefix based on that + c = len([file for file in os.listdir(download_directory) + if re.search(f'^{song_name}_', file)]) + 1 + filename = os.path.join( - download_directory, f'{song_name}_1.{EXT_MAP.get(ZSpotify.get_config(DOWNLOAD_FORMAT))}') + download_directory, f'{song_name}_{c}.{EXT_MAP.get(ZSpotify.get_config(DOWNLOAD_FORMAT))}') except Exception as e: diff --git a/zspotify/utils.py b/zspotify/utils.py index e838bbb1..5f9f5c25 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -27,7 +27,7 @@ def create_download_directory(download_path: str) -> None: with open(hidden_file_path, 'w', encoding='utf-8') as f: pass -def get_directory_song_ids(download_path: str) -> list[str]: +def get_directory_song_ids(download_path: str) -> List[str]: """ Gets song ids of songs in directory """ song_ids = []