From a1e54e9781f41a5f8a1b3d7a44565dede095f6e7 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Mon, 8 Nov 2021 20:35:32 +0200 Subject: [PATCH 1/3] Added global song archive --- zspotify/const.py | 3 +++ zspotify/track.py | 14 ++++++++++++-- zspotify/utils.py | 25 ++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/zspotify/const.py b/zspotify/const.py index a76231db..cb34ae87 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_ALL_TIME_INSTALLED = 'SKIP_ALL_TIME_INSTALLED' + 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_ALL_TIME_INSTALLED': False, 'DOWNLOAD_FORMAT': 'ogg', 'FORCE_PREMIUM': False, 'ANTI_BAN_WAIT_TIME': 1, diff --git a/zspotify/track.py b/zspotify/track.py index 2cab1c16..1f112191 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_ALL_TIME_INSTALLED 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_all_time_installed, add_to_archive from zspotify import ZSpotify @@ -93,6 +94,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', 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_all_time_installed(scraped_song_id, ZSpotify.get_config(ROOT_PATH)) # a song with the same name is installed if not check_id and check_name: @@ -115,6 +117,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_ALL_TIME_INSTALLED): + print('\n### SKIPPING:', song_name, + '(SONG ALREADY INSTALLED ONCE) ###') + else: if track_id != scraped_song_id: track_id = scraped_song_id @@ -146,6 +153,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_ALL_TIME_INSTALLED): + add_to_archive(scraped_song_id, ZSpotify.get_config(ROOT_PATH)) # 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..e22700a3 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_all_time_installed(song_id: str, archive_directory: str) -> List[str]: + """ Returns list of all time installed 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 """ From 1c419c0873535c8a441c84a12f3a19c214ea40f8 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Tue, 9 Nov 2021 18:46:28 +0200 Subject: [PATCH 2/3] Name changes --- zspotify/const.py | 4 ++-- zspotify/track.py | 8 ++++---- zspotify/utils.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/zspotify/const.py b/zspotify/const.py index cb34ae87..317d1e28 100644 --- a/zspotify/const.py +++ b/zspotify/const.py @@ -88,7 +88,7 @@ ROOT_PODCAST_PATH = 'ROOT_PODCAST_PATH' SKIP_EXISTING_FILES = 'SKIP_EXISTING_FILES' -SKIP_ALL_TIME_INSTALLED = 'SKIP_ALL_TIME_INSTALLED' +SKIP_PREVIOUSLY_DOWNLOADED = 'SKIP_PREVIOUSLY_DOWNLOADED' DOWNLOAD_FORMAT = 'DOWNLOAD_FORMAT' @@ -130,7 +130,7 @@ CONFIG_DEFAULT_SETTINGS = { 'ROOT_PATH': '../ZSpotify Music/', 'ROOT_PODCAST_PATH': '../ZSpotify Podcasts/', 'SKIP_EXISTING_FILES': True, - 'SKIP_ALL_TIME_INSTALLED': False, + '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 1f112191..35f7bd6b 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -12,9 +12,9 @@ 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, \ - SKIP_ALL_TIME_INSTALLED + 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_all_time_installed, add_to_archive + get_directory_song_ids, add_to_directory_song_ids, get_previously_downloaded, add_to_archive from zspotify import ZSpotify @@ -94,7 +94,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', 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_all_time_installed(scraped_song_id, ZSpotify.get_config(ROOT_PATH)) + check_all_time = scraped_song_id in get_previously_downloaded(scraped_song_id, ZSpotify.get_config(ROOT_PATH)) # a song with the same name is installed if not check_id and check_name: @@ -118,7 +118,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', print('\n### SKIPPING:', song_name, '(SONG ALREADY EXISTS) ###') - elif check_all_time and ZSpotify.get_config(SKIP_ALL_TIME_INSTALLED): + elif check_all_time and ZSpotify.get_config(SKIP_PREVIOUSLY_DOWNLOADED): print('\n### SKIPPING:', song_name, '(SONG ALREADY INSTALLED ONCE) ###') diff --git a/zspotify/utils.py b/zspotify/utils.py index e22700a3..6c67c6b9 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -27,8 +27,8 @@ def create_download_directory(download_path: str) -> None: with open(hidden_file_path, 'w', encoding='utf-8') as f: pass -def get_all_time_installed(song_id: str, archive_directory: str) -> List[str]: - """ Returns list of all time installed songs """ +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') From 84978d8885244e30be8522d6511f7a25ae95e16c Mon Sep 17 00:00:00 2001 From: yiannisha Date: Tue, 9 Nov 2021 19:03:44 +0200 Subject: [PATCH 3/3] fixed path with missing directory --- zspotify/track.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zspotify/track.py b/zspotify/track.py index 35f7bd6b..9eb214f2 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -92,9 +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, ZSpotify.get_config(ROOT_PATH)) + 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: @@ -120,7 +121,7 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', elif check_all_time and ZSpotify.get_config(SKIP_PREVIOUSLY_DOWNLOADED): print('\n### SKIPPING:', song_name, - '(SONG ALREADY INSTALLED ONCE) ###') + '(SONG ALREADY DOWNLOADED ONCE) ###') else: if track_id != scraped_song_id: @@ -154,8 +155,8 @@ def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', set_music_thumbnail(filename, image_url) # add song id to archive file - if ZSpotify.get_config(SKIP_ALL_TIME_INSTALLED): - add_to_archive(scraped_song_id, ZSpotify.get_config(ROOT_PATH)) + 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)