Merge pull request #164 from thomaslty/main

fix windows invalid folder name
This commit is contained in:
Logykk 2021-10-30 20:54:55 +13:00 committed by GitHub
commit b991c29d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -2,7 +2,7 @@ from tqdm import tqdm
from const import ITEMS, ARTISTS, NAME, ID
from track import download_track
from utils import sanitize_data
from utils import sanitize_data, fix_filename
from zspotify import ZSpotify
ALBUM_URL = 'https://api.spotify.com/v1/albums'
@ -47,9 +47,11 @@ def get_artist_albums(artist_id):
def download_album(album):
""" Downloads songs from an album """
artist, album_name = get_album_name(album)
artist_fixed = fix_filename(artist)
album_name_fixed = fix_filename(album_name)
tracks = get_album_tracks(album)
for n, track in tqdm(enumerate(tracks, start=1), unit_scale=True, unit='Song', total=len(tracks)):
download_track(track[ID], f'{artist}/{album_name}',
download_track(track[ID], f'{artist_fixed}/{album_name_fixed}',
prefix=True, prefix_value=str(n), disable_progressbar=True)

View File

@ -179,3 +179,22 @@ def regex_input_for_urls(search_input) -> Tuple[str, str, str, str, str, str]:
artist_id_str = None
return track_id_str, album_id_str, playlist_id_str, episode_id_str, show_id_str, artist_id_str
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
"""
return re.sub(r'[/\\:|<>"?*\0-\x1f]|^(AUX|COM[1-9]|CON|LPT[1-9]|NUL|PRN)(?![^.])|^\s|[\s.]$', "_", name, flags=re.IGNORECASE)