diff --git a/zspotify/const.py b/zspotify/const.py index a76231db..317d1e28 100644 --- a/zspotify/const.py +++ b/zspotify/const.py @@ -88,6 +88,8 @@ ROOT_PODCAST_PATH = 'ROOT_PODCAST_PATH' SKIP_EXISTING_FILES = 'SKIP_EXISTING_FILES' +SKIP_PREVIOUSLY_DOWNLOADED = 'SKIP_PREVIOUSLY_DOWNLOADED' + DOWNLOAD_FORMAT = 'DOWNLOAD_FORMAT' FORCE_PREMIUM = 'FORCE_PREMIUM' @@ -128,6 +130,7 @@ CONFIG_DEFAULT_SETTINGS = { 'ROOT_PATH': '../ZSpotify Music/', 'ROOT_PODCAST_PATH': '../ZSpotify Podcasts/', 'SKIP_EXISTING_FILES': True, + 'SKIP_PREVIOUSLY_DOWNLOADED': False, 'DOWNLOAD_FORMAT': 'ogg', 'FORCE_PREMIUM': False, 'ANTI_BAN_WAIT_TIME': 1, diff --git a/zspotify/track.py b/zspotify/track.py index 2cab1c16..9eb214f2 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -11,9 +11,10 @@ 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, TRACK_STATS_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 + CHUNK_SIZE, SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME, \ + SKIP_PREVIOUSLY_DOWNLOADED from utils import fix_filename, set_audio_tags, set_music_thumbnail, create_download_directory, \ - get_directory_song_ids, add_to_directory_song_ids + get_directory_song_ids, add_to_directory_song_ids, get_previously_downloaded, add_to_archive from zspotify import ZSpotify @@ -91,8 +92,10 @@ 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).lower())}') + archive_directory = os.path.join(os.path.dirname(__file__), ZSpotify.get_config(ROOT_PATH)) check_name = os.path.isfile(filename) and os.path.getsize(filename) check_id = scraped_song_id in get_directory_song_ids(download_directory) + check_all_time = scraped_song_id in get_previously_downloaded(scraped_song_id, archive_directory) # a song with the same name is installed if not check_id and check_name: @@ -115,6 +118,11 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', if check_id and check_name and ZSpotify.get_config(SKIP_EXISTING_FILES): print('\n### SKIPPING:', song_name, '(SONG ALREADY EXISTS) ###') + + elif check_all_time and ZSpotify.get_config(SKIP_PREVIOUSLY_DOWNLOADED): + print('\n### SKIPPING:', song_name, + '(SONG ALREADY DOWNLOADED ONCE) ###') + else: if track_id != scraped_song_id: track_id = scraped_song_id @@ -146,6 +154,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 archive file + if ZSpotify.get_config(SKIP_PREVIOUSLY_DOWNLOADED): + add_to_archive(scraped_song_id, archive_directory) # add song id to download directory's .song_ids file if not check_id: add_to_directory_song_ids(download_directory, scraped_song_id) diff --git a/zspotify/utils.py b/zspotify/utils.py index d0dc8738..6c67c6b9 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -12,7 +12,6 @@ import requests from const import ARTIST, TRACKTITLE, ALBUM, YEAR, DISCNUMBER, TRACKNUMBER, ARTWORK, \ WINDOWS_SYSTEM, ALBUMARTIST - class MusicFormat(str, Enum): MP3 = 'mp3', OGG = 'ogg', @@ -28,6 +27,30 @@ def create_download_directory(download_path: str) -> None: with open(hidden_file_path, 'w', encoding='utf-8') as f: pass +def get_previously_downloaded(song_id: str, archive_directory: str) -> List[str]: + """ Returns list of all time downloaded songs """ + + ids = [] + archive_path = os.path.join(archive_directory, '.song_archive') + + if os.path.exists(archive_path): + with open(archive_path, 'r', encoding='utf-8') as f: + ids = [line.strip() for line in f.readlines()] + + return ids + +def add_to_archive(song_id: str, archive_directory: str) -> None: + """ Adds song id to all time installed songs archive """ + + archive_path = os.path.join(archive_directory, '.song_archive') + + if os.path.exists(archive_path): + with open(archive_path, 'a', encoding='utf-8') as f: + f.write(f'{song_id}\n') + else: + with open(archive_path, 'w', encoding='utf-8') as f: + f.write(f'{song_id}\n') + def get_directory_song_ids(download_path: str) -> List[str]: """ Gets song ids of songs in directory """