Added global song archive

This commit is contained in:
yiannisha 2021-11-08 20:35:32 +02:00
parent 6dbe5e8940
commit a1e54e9781
3 changed files with 39 additions and 3 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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 """