2021-11-18 23:24:08 +01:00
|
|
|
import datetime
|
2021-11-22 11:41:38 +01:00
|
|
|
import math
|
2021-10-23 17:36:37 +02:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import re
|
2021-10-31 19:55:00 +01:00
|
|
|
import subprocess
|
2021-10-23 17:36:37 +02:00
|
|
|
from enum import Enum
|
2021-10-25 07:36:17 +02:00
|
|
|
from typing import List, Tuple
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
import music_tag
|
|
|
|
import requests
|
|
|
|
|
2021-11-30 14:45:06 +01:00
|
|
|
from const import ARTIST, GENRE, TRACKTITLE, ALBUM, YEAR, DISCNUMBER, TRACKNUMBER, ARTWORK, \
|
2021-11-05 16:50:55 +01:00
|
|
|
WINDOWS_SYSTEM, ALBUMARTIST
|
2021-11-18 23:24:08 +01:00
|
|
|
from zspotify import ZSpotify
|
|
|
|
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
class MusicFormat(str, Enum):
|
|
|
|
MP3 = 'mp3',
|
|
|
|
OGG = 'ogg',
|
2021-10-30 14:35:01 +02:00
|
|
|
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
def create_download_directory(download_path: str) -> None:
|
2021-10-30 14:35:01 +02:00
|
|
|
""" Create directory and add a hidden file with song ids """
|
2021-10-23 17:36:37 +02:00
|
|
|
os.makedirs(download_path, exist_ok=True)
|
|
|
|
|
2021-10-30 14:35:01 +02:00
|
|
|
# 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
|
|
|
|
|
2021-11-22 11:41:38 +01:00
|
|
|
|
2021-11-18 23:24:08 +01:00
|
|
|
def get_previously_downloaded() -> List[str]:
|
2021-11-09 17:46:28 +01:00
|
|
|
""" Returns list of all time downloaded songs """
|
2021-11-08 19:35:32 +01:00
|
|
|
|
|
|
|
ids = []
|
2021-11-24 14:40:14 +01:00
|
|
|
archive_path = ZSpotify.CONFIG.get_song_archive()
|
2021-11-08 19:35:32 +01:00
|
|
|
|
|
|
|
if os.path.exists(archive_path):
|
|
|
|
with open(archive_path, 'r', encoding='utf-8') as f:
|
2021-11-18 23:24:08 +01:00
|
|
|
ids = [line.strip().split('\t')[0] for line in f.readlines()]
|
2021-11-08 19:35:32 +01:00
|
|
|
|
|
|
|
return ids
|
|
|
|
|
2021-11-22 11:41:38 +01:00
|
|
|
|
2021-11-19 17:46:32 +01:00
|
|
|
def add_to_archive(song_id: str, filename: str, author_name: str, song_name: str) -> None:
|
2021-11-08 19:35:32 +01:00
|
|
|
""" Adds song id to all time installed songs archive """
|
|
|
|
|
2021-11-24 14:40:14 +01:00
|
|
|
archive_path = ZSpotify.CONFIG.get_song_archive()
|
2021-11-08 19:35:32 +01:00
|
|
|
|
|
|
|
if os.path.exists(archive_path):
|
2021-11-19 17:46:32 +01:00
|
|
|
with open(archive_path, 'a', encoding='utf-8') as file:
|
|
|
|
file.write(f'{song_id}\t{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\t{author_name}\t{song_name}\t{filename}\n')
|
2021-11-08 19:35:32 +01:00
|
|
|
else:
|
2021-11-19 17:46:32 +01:00
|
|
|
with open(archive_path, 'w', encoding='utf-8') as file:
|
|
|
|
file.write(f'{song_id}\t{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\t{author_name}\t{song_name}\t{filename}\n')
|
2021-11-08 19:35:32 +01:00
|
|
|
|
2021-11-22 11:41:38 +01:00
|
|
|
|
2021-10-30 14:58:09 +02:00
|
|
|
def get_directory_song_ids(download_path: str) -> List[str]:
|
2021-10-30 14:35:01 +02:00
|
|
|
""" 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:
|
2021-11-19 17:46:32 +01:00
|
|
|
song_ids.extend([line.strip().split('\t')[0] for line in file.readlines()])
|
2021-10-30 14:35:01 +02:00
|
|
|
|
|
|
|
return song_ids
|
|
|
|
|
2021-11-22 11:41:38 +01:00
|
|
|
|
2021-11-19 17:46:32 +01:00
|
|
|
def add_to_directory_song_ids(download_path: str, song_id: str, filename: str, author_name: str, song_name: str) -> None:
|
2021-10-30 14:35:01 +02:00
|
|
|
""" 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:
|
2021-11-19 17:46:32 +01:00
|
|
|
file.write(f'{song_id}\t{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\t{author_name}\t{song_name}\t{filename}\n')
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-11-22 11:41:38 +01:00
|
|
|
|
2021-10-31 19:55:00 +01:00
|
|
|
def get_downloaded_song_duration(filename: str) -> float:
|
|
|
|
""" Returns the downloaded file's duration in seconds """
|
|
|
|
|
|
|
|
command = ['ffprobe', '-show_entries', 'format=duration', '-i', f'{filename}']
|
|
|
|
output = subprocess.run(command, capture_output=True)
|
|
|
|
|
|
|
|
duration = re.search(r'[\D]=([\d\.]*)', str(output.stdout)).groups()[0]
|
|
|
|
duration = float(duration)
|
|
|
|
|
|
|
|
return duration
|
|
|
|
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-10-24 19:45:52 +02:00
|
|
|
def split_input(selection) -> List[str]:
|
2021-10-23 17:36:37 +02:00
|
|
|
""" Returns a list of inputted strings """
|
|
|
|
inputs = []
|
|
|
|
if '-' in selection:
|
|
|
|
for number in range(int(selection.split('-')[0]), int(selection.split('-')[1]) + 1):
|
|
|
|
inputs.append(number)
|
|
|
|
else:
|
|
|
|
selections = selection.split(',')
|
|
|
|
for i in selections:
|
|
|
|
inputs.append(i.strip())
|
|
|
|
return inputs
|
|
|
|
|
|
|
|
|
2021-11-19 18:45:04 +01:00
|
|
|
def splash() -> str:
|
2021-10-23 17:36:37 +02:00
|
|
|
""" Displays splash screen """
|
2021-11-19 18:45:04 +01:00
|
|
|
return """
|
2021-10-23 17:36:37 +02:00
|
|
|
███████ ███████ ██████ ██████ ████████ ██ ███████ ██ ██
|
|
|
|
███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███ ███████ ██████ ██ ██ ██ ██ █████ ████
|
|
|
|
███ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███████ ███████ ██ ██████ ██ ██ ██ ██
|
2021-11-19 18:45:04 +01:00
|
|
|
"""
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def clear() -> None:
|
|
|
|
""" Clear the console window """
|
|
|
|
if platform.system() == WINDOWS_SYSTEM:
|
|
|
|
os.system('cls')
|
|
|
|
else:
|
|
|
|
os.system('clear')
|
|
|
|
|
|
|
|
|
2021-11-30 14:45:06 +01:00
|
|
|
def set_audio_tags(filename, artists, genres, name, album_name, release_year, disc_number, track_number) -> None:
|
2021-10-23 17:36:37 +02:00
|
|
|
""" sets music_tag metadata """
|
|
|
|
tags = music_tag.load_file(filename)
|
2021-11-05 16:50:55 +01:00
|
|
|
tags[ALBUMARTIST] = artists[0]
|
2021-10-23 17:36:37 +02:00
|
|
|
tags[ARTIST] = conv_artist_format(artists)
|
2021-12-18 12:12:22 +01:00
|
|
|
tags[GENRE] = genres[0] if not ZSpotify.CONFIG.get_all_genres() else ZSpotify.CONFIG.get_all_genres_delimiter().join(genres)
|
2021-10-23 17:36:37 +02:00
|
|
|
tags[TRACKTITLE] = name
|
|
|
|
tags[ALBUM] = album_name
|
|
|
|
tags[YEAR] = release_year
|
|
|
|
tags[DISCNUMBER] = disc_number
|
|
|
|
tags[TRACKNUMBER] = track_number
|
|
|
|
tags.save()
|
|
|
|
|
|
|
|
|
|
|
|
def conv_artist_format(artists) -> str:
|
|
|
|
""" Returns converted artist format """
|
2021-10-24 01:31:44 +02:00
|
|
|
return ', '.join(artists)
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_music_thumbnail(filename, image_url) -> None:
|
|
|
|
""" Downloads cover artwork """
|
|
|
|
img = requests.get(image_url).content
|
|
|
|
tags = music_tag.load_file(filename)
|
|
|
|
tags[ARTWORK] = img
|
|
|
|
tags.save()
|
|
|
|
|
|
|
|
|
2021-10-24 19:45:52 +02:00
|
|
|
def regex_input_for_urls(search_input) -> Tuple[str, str, str, str, str, str]:
|
2021-10-23 17:36:37 +02:00
|
|
|
""" Since many kinds of search may be passed at the command line, process them all here. """
|
|
|
|
track_uri_search = re.search(
|
|
|
|
r'^spotify:track:(?P<TrackID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
track_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/track/(?P<TrackID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
album_uri_search = re.search(
|
|
|
|
r'^spotify:album:(?P<AlbumID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
album_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/album/(?P<AlbumID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
playlist_uri_search = re.search(
|
|
|
|
r'^spotify:playlist:(?P<PlaylistID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
playlist_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/playlist/(?P<PlaylistID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
episode_uri_search = re.search(
|
|
|
|
r'^spotify:episode:(?P<EpisodeID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
episode_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/episode/(?P<EpisodeID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
show_uri_search = re.search(
|
|
|
|
r'^spotify:show:(?P<ShowID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
show_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/show/(?P<ShowID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
artist_uri_search = re.search(
|
|
|
|
r'^spotify:artist:(?P<ArtistID>[0-9a-zA-Z]{22})$', search_input)
|
|
|
|
artist_url_search = re.search(
|
|
|
|
r'^(https?://)?open\.spotify\.com/artist/(?P<ArtistID>[0-9a-zA-Z]{22})(\?si=.+?)?$',
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if track_uri_search is not None or track_url_search is not None:
|
|
|
|
track_id_str = (track_uri_search
|
|
|
|
if track_uri_search is not None else
|
|
|
|
track_url_search).group('TrackID')
|
|
|
|
else:
|
|
|
|
track_id_str = None
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if album_uri_search is not None or album_url_search is not None:
|
|
|
|
album_id_str = (album_uri_search
|
|
|
|
if album_uri_search is not None else
|
|
|
|
album_url_search).group('AlbumID')
|
|
|
|
else:
|
|
|
|
album_id_str = None
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if playlist_uri_search is not None or playlist_url_search is not None:
|
|
|
|
playlist_id_str = (playlist_uri_search
|
|
|
|
if playlist_uri_search is not None else
|
|
|
|
playlist_url_search).group('PlaylistID')
|
|
|
|
else:
|
|
|
|
playlist_id_str = None
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if episode_uri_search is not None or episode_url_search is not None:
|
|
|
|
episode_id_str = (episode_uri_search
|
|
|
|
if episode_uri_search is not None else
|
|
|
|
episode_url_search).group('EpisodeID')
|
|
|
|
else:
|
|
|
|
episode_id_str = None
|
2021-10-23 17:36:37 +02:00
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if show_uri_search is not None or show_url_search is not None:
|
|
|
|
show_id_str = (show_uri_search
|
|
|
|
if show_uri_search is not None else
|
|
|
|
show_url_search).group('ShowID')
|
|
|
|
else:
|
|
|
|
show_id_str = None
|
2021-10-24 15:05:40 +02:00
|
|
|
|
2021-10-25 07:36:17 +02:00
|
|
|
if artist_uri_search is not None or artist_url_search is not None:
|
|
|
|
artist_id_str = (artist_uri_search
|
|
|
|
if artist_uri_search is not None else
|
|
|
|
artist_url_search).group('ArtistID')
|
2021-10-24 15:05:40 +02:00
|
|
|
else:
|
2021-10-25 07:36:17 +02:00
|
|
|
artist_id_str = None
|
|
|
|
|
|
|
|
return track_id_str, album_id_str, playlist_id_str, episode_id_str, show_id_str, artist_id_str
|
2021-10-28 11:01:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def fix_filename(name):
|
|
|
|
"""
|
|
|
|
Replace invalid characters on Linux/Windows/MacOS with underscores.
|
|
|
|
List from https://stackoverflow.com/a/31976060/819417
|
|
|
|
Trailing spaces & periods are ignored on Windows.
|
|
|
|
>>> fix_filename(" COM1 ")
|
|
|
|
'_ COM1 _'
|
|
|
|
>>> fix_filename("COM10")
|
|
|
|
'COM10'
|
|
|
|
>>> fix_filename("COM1,")
|
|
|
|
'COM1,'
|
|
|
|
>>> fix_filename("COM1.txt")
|
|
|
|
'_.txt'
|
|
|
|
>>> all('_' == fix_filename(chr(i)) for i in list(range(32)))
|
|
|
|
True
|
|
|
|
"""
|
2021-11-19 00:20:17 +01:00
|
|
|
return re.sub(r'[/\\:|<>"?*\0-\x1f]|^(AUX|COM[1-9]|CON|LPT[1-9]|NUL|PRN)(?![^.])|^\s|[\s.]$', "_", str(name), flags=re.IGNORECASE)
|
2021-11-22 11:41:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def fmt_seconds(secs: float) -> str:
|
|
|
|
val = math.floor(secs)
|
|
|
|
|
|
|
|
s = math.floor(val % 60)
|
|
|
|
val -= s
|
|
|
|
val /= 60
|
|
|
|
|
|
|
|
m = math.floor(val % 60)
|
|
|
|
val -= m
|
|
|
|
val /= 60
|
|
|
|
|
|
|
|
h = math.floor(val)
|
|
|
|
|
|
|
|
if h == 0 and m == 0 and s == 0:
|
2021-12-18 12:12:22 +01:00
|
|
|
return "0s"
|
2021-11-22 11:41:38 +01:00
|
|
|
elif h == 0 and m == 0:
|
2021-12-18 12:12:22 +01:00
|
|
|
return f'{s}s'.zfill(2)
|
2021-11-22 11:41:38 +01:00
|
|
|
elif h == 0:
|
|
|
|
return f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
|
|
|
else:
|
|
|
|
return f'{h}'.zfill(2) + ':' + f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
2021-12-18 10:11:43 +01:00
|
|
|
|
|
|
|
|