mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Add command line option for target folder (-f)
This commit is contained in:
@@ -2,20 +2,20 @@ import subprocess
|
||||
import os
|
||||
|
||||
|
||||
def song(input_song, output_song, avconv=False, verbose=False):
|
||||
def song(input_song, output_song, folder, avconv=False, verbose=False):
|
||||
"""Do the audio format conversion."""
|
||||
if not input_song == output_song:
|
||||
print('Converting {0} to {1}'.format(
|
||||
input_song, output_song.split('.')[-1]))
|
||||
if avconv:
|
||||
exit_code = convert_with_avconv(input_song, output_song, verbose)
|
||||
exit_code = convert_with_avconv(input_song, output_song, folder, verbose)
|
||||
else:
|
||||
exit_code = convert_with_ffmpeg(input_song, output_song, verbose)
|
||||
exit_code = convert_with_ffmpeg(input_song, output_song, folder, verbose)
|
||||
return exit_code
|
||||
return 0
|
||||
|
||||
|
||||
def convert_with_avconv(input_song, output_song, verbose):
|
||||
def convert_with_avconv(input_song, output_song, folder, verbose):
|
||||
"""Convert the audio file using avconv."""
|
||||
if os.name == 'nt':
|
||||
avconv_path = '..\\Scripts\\avconv.exe'
|
||||
@@ -31,12 +31,12 @@ def convert_with_avconv(input_song, output_song, verbose):
|
||||
'-loglevel', level,
|
||||
'-i', 'Music/' + input_song,
|
||||
'-ab', '192k',
|
||||
'Music/' + output_song]
|
||||
os.path.join(folder, output_song)]
|
||||
|
||||
return subprocess.call(command)
|
||||
|
||||
|
||||
def convert_with_ffmpeg(input_song, output_song, verbose):
|
||||
def convert_with_ffmpeg(input_song, output_song, folder, verbose):
|
||||
"""Convert the audio file using FFMpeg.
|
||||
|
||||
What are the differences and similarities between ffmpeg, libav, and avconv?
|
||||
@@ -73,7 +73,7 @@ def convert_with_ffmpeg(input_song, output_song, verbose):
|
||||
elif output_ext == 'm4a':
|
||||
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
|
||||
|
||||
command = '{0}-i Music/{1} {2}Music/{3}'.format(
|
||||
ffmpeg_pre, input_song, ffmpeg_params, output_song).split(' ')
|
||||
command = '{0}-i {1} {2}{3}'.format(
|
||||
ffmpeg_pre, os.path.join(folder, input_song), ffmpeg_params, os.path.join(folder, output_song)).split(' ')
|
||||
|
||||
return subprocess.call(command)
|
||||
|
||||
@@ -10,12 +10,12 @@ def compare(music_file, metadata):
|
||||
already_tagged = False
|
||||
try:
|
||||
if music_file.endswith('.mp3'):
|
||||
audiofile = EasyID3('Music/' + music_file)
|
||||
audiofile = EasyID3(music_file)
|
||||
# fetch track title metadata
|
||||
already_tagged = audiofile['title'][0] == metadata['name']
|
||||
elif music_file.endswith('.m4a'):
|
||||
tags = {'title': '\xa9nam'}
|
||||
audiofile = MP4('Music/' + music_file)
|
||||
audiofile = MP4(music_file)
|
||||
# fetch track title metadata
|
||||
already_tagged = audiofile[tags['title']] == metadata['name']
|
||||
except (KeyError, TypeError):
|
||||
@@ -42,7 +42,7 @@ def embed(music_file, meta_tags):
|
||||
def embed_mp3(music_file, meta_tags):
|
||||
"""Embed metadata to MP3 files."""
|
||||
# EasyID3 is fun to use ;)
|
||||
audiofile = EasyID3('Music/' + music_file)
|
||||
audiofile = EasyID3(music_file)
|
||||
audiofile['artist'] = meta_tags['artists'][0]['name']
|
||||
audiofile['albumartist'] = meta_tags['artists'][0]['name']
|
||||
audiofile['album'] = meta_tags['album']['name']
|
||||
@@ -66,7 +66,7 @@ def embed_mp3(music_file, meta_tags):
|
||||
if meta_tags['copyright']:
|
||||
audiofile['copyright'] = meta_tags['copyright']
|
||||
audiofile.save(v2_version=3)
|
||||
audiofile = ID3('Music/' + music_file)
|
||||
audiofile = ID3(music_file)
|
||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
||||
desc=u'Cover', data=albumart.read())
|
||||
@@ -96,7 +96,7 @@ def embed_m4a(music_file, meta_tags):
|
||||
'copyright': 'cprt',
|
||||
'tempo': 'tmpo'}
|
||||
|
||||
audiofile = MP4('Music/' + music_file)
|
||||
audiofile = MP4(music_file)
|
||||
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
||||
audiofile[tags['albumartist']] = meta_tags['artists'][0]['name']
|
||||
audiofile[tags['album']] = meta_tags['album']['name']
|
||||
|
||||
@@ -5,6 +5,7 @@ import spotipy.oauth2 as oauth2
|
||||
from urllib.request import quote
|
||||
from slugify import slugify
|
||||
|
||||
|
||||
def input_link(links):
|
||||
"""Let the user input a number."""
|
||||
while True:
|
||||
@@ -51,6 +52,9 @@ def get_arguments():
|
||||
'-a', '--avconv', default=False,
|
||||
help='Use avconv for conversion otherwise set defaults to ffmpeg',
|
||||
action='store_true')
|
||||
parser.add_argument(
|
||||
'-f', '--folder', default='Music/',
|
||||
help='path to folder where files will stored in')
|
||||
parser.add_argument(
|
||||
'-v', '--verbose', default=False, help='show debug output',
|
||||
action='store_true')
|
||||
@@ -105,7 +109,7 @@ def filter_path(path):
|
||||
os.makedirs(path)
|
||||
for temp in os.listdir(path):
|
||||
if temp.endswith('.temp'):
|
||||
os.remove('{0}/{1}'.format(path, temp))
|
||||
os.remove(os.path.join(path, temp))
|
||||
|
||||
|
||||
def grace_quit():
|
||||
|
||||
32
spotdl.py
32
spotdl.py
@@ -175,26 +175,26 @@ def download_song(content):
|
||||
else:
|
||||
music_file = misc.generate_filename(content.title)
|
||||
link.download(
|
||||
filepath='Music/{0}{1}'.format(music_file, args.input_ext))
|
||||
filepath='{0}{1}'.format(os.path.join(args.folder, music_file), args.input_ext))
|
||||
return True
|
||||
|
||||
|
||||
def check_exists(music_file, raw_song, islist=True):
|
||||
"""Check if the input song already exists in the 'Music' folder."""
|
||||
songs = os.listdir('Music')
|
||||
"""Check if the input song already exists in the given folder."""
|
||||
songs = os.listdir(args.folder)
|
||||
for song in songs:
|
||||
if song.endswith('.temp'):
|
||||
os.remove(u'Music/{0}'.format(song))
|
||||
os.remove(os.path.join(args.folder, song))
|
||||
continue
|
||||
# check if any song with similar name is already present in Music/
|
||||
# check if any song with similar name is already present in the given folder
|
||||
umfile = misc.generate_filename(music_file)
|
||||
if song.startswith(umfile):
|
||||
# check if the already downloaded song has correct metadata
|
||||
already_tagged = metadata.compare(song, generate_metadata(raw_song))
|
||||
already_tagged = metadata.compare(os.path.join(args.folder, song), generate_metadata(raw_song))
|
||||
|
||||
# if not, remove it and download again without prompt
|
||||
if misc.is_spotify(raw_song) and not already_tagged:
|
||||
os.remove('Music/{0}'.format(song))
|
||||
os.remove(os.path.join(args.folder, song))
|
||||
return False
|
||||
|
||||
# do not prompt and skip the current song
|
||||
@@ -206,7 +206,7 @@ def check_exists(music_file, raw_song, islist=True):
|
||||
prompt = input('Song with same name has already been downloaded. '
|
||||
'Re-download? (y/n): ').lower()
|
||||
if prompt == 'y':
|
||||
os.remove('Music/{0}'.format(song))
|
||||
os.remove(os.path.join(args.folder, song))
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
@@ -274,13 +274,13 @@ def grab_single(raw_song, number=None):
|
||||
print('')
|
||||
input_song = music_file + args.input_ext
|
||||
output_song = music_file + args.output_ext
|
||||
convert.song(input_song, output_song, avconv=args.avconv,
|
||||
verbose=args.verbose)
|
||||
convert.song(input_song, output_song, args.folder,
|
||||
avconv=args.avconv, verbose=args.verbose)
|
||||
if not args.input_ext == args.output_ext:
|
||||
os.remove('Music/{0}'.format(input_song))
|
||||
os.remove(os.path.join(args.folder, input_song))
|
||||
meta_tags = generate_metadata(raw_song)
|
||||
if not args.no_metadata:
|
||||
metadata.embed(output_song, meta_tags)
|
||||
metadata.embed(os.path.join(args.folder, output_song), meta_tags)
|
||||
else:
|
||||
print('No audio streams available')
|
||||
|
||||
@@ -289,6 +289,7 @@ class Args(object):
|
||||
manual = False
|
||||
input_ext = '.m4a'
|
||||
output_ext = '.mp3'
|
||||
folder = 'Music/'
|
||||
|
||||
args = Args()
|
||||
# token is mandatory when using Spotify's API
|
||||
@@ -296,15 +297,18 @@ args = Args()
|
||||
token = misc.generate_token()
|
||||
spotify = spotipy.Spotify(auth=token)
|
||||
|
||||
misc.filter_path('Music')
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.chdir(sys.path[0])
|
||||
args = misc.get_arguments()
|
||||
|
||||
misc.filter_path(args.folder)
|
||||
|
||||
if args.song:
|
||||
grab_single(raw_song=args.song)
|
||||
elif args.list:
|
||||
grab_list(text_file=args.list)
|
||||
elif args.username:
|
||||
feed_playlist(username=args.username)
|
||||
else:
|
||||
misc.filter_path('Music')
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ import os
|
||||
|
||||
raw_song = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
|
||||
|
||||
for x in os.listdir('Music'):
|
||||
os.remove('Music/' + x)
|
||||
for x in os.listdir(spotdl.args.folder):
|
||||
os.remove(os.path.join(spotdl.args.folder, x))
|
||||
|
||||
|
||||
def test_spotify_title():
|
||||
@@ -50,7 +50,7 @@ def test_convert():
|
||||
music_file = spotdl.misc.generate_filename(content.title)
|
||||
input_song = music_file + spotdl.args.input_ext
|
||||
output_song = music_file + spotdl.args.output_ext
|
||||
convert = spotdl.convert.song(input_song, output_song)
|
||||
convert = spotdl.convert.song(input_song, output_song, spotdl.args.folder)
|
||||
assert convert == expect_convert
|
||||
|
||||
|
||||
@@ -60,9 +60,9 @@ def test_metadata():
|
||||
music_file = spotdl.misc.generate_filename(content.title)
|
||||
meta_tags = spotdl.generate_metadata(raw_song)
|
||||
output_song = music_file + spotdl.args.output_ext
|
||||
metadata_output = spotdl.metadata.embed(output_song, meta_tags)
|
||||
metadata_output = spotdl.metadata.embed(os.path.join(spotdl.args.folder, output_song), meta_tags)
|
||||
input_song = music_file + spotdl.args.input_ext
|
||||
metadata_input = spotdl.metadata.embed(input_song, meta_tags)
|
||||
metadata_input = spotdl.metadata.embed(os.path.join(spotdl.args.folder, input_song), meta_tags)
|
||||
assert metadata_output == (metadata_input == expect_metadata)
|
||||
|
||||
|
||||
@@ -71,6 +71,6 @@ def test_check_exists2():
|
||||
content = spotdl.go_pafy(raw_song)
|
||||
music_file = spotdl.misc.generate_filename(content.title)
|
||||
input_song = music_file + spotdl.args.input_ext
|
||||
os.remove('Music/' + input_song)
|
||||
os.remove(os.path.join(spotdl.args.folder, input_song))
|
||||
check = spotdl.check_exists(music_file, raw_song, islist=True)
|
||||
assert check == expect_check
|
||||
|
||||
Reference in New Issue
Block a user