21 Jan update

This commit is contained in:
Nitesh Sawant
2018-01-22 23:47:56 +05:30
12 changed files with 176 additions and 100 deletions

View File

@@ -31,9 +31,8 @@ def song(input_song, output_song, folder, avconv=False):
class Converter:
def __init__(self, input_song, output_song, folder):
self.input_song = input_song
self.output_song = output_song
self.folder = folder
self.input_file = os.path.join(folder, input_song)
self.output_file = os.path.join(folder, output_song)
def with_avconv(self):
if log.level == 10:
@@ -42,8 +41,8 @@ class Converter:
level = '0'
command = ['avconv', '-loglevel', level, '-i',
os.path.join(self.folder, self.input_song), '-ab', '192k',
os.path.join(self.folder, self.output_song)]
self.input_file, '-ab', '192k',
self.output_file]
log.debug(command)
return subprocess.call(command)
@@ -54,8 +53,8 @@ class Converter:
if not log.level == 10:
ffmpeg_pre += '-hide_banner -nostats -v panic '
input_ext = self.input_song.split('.')[-1]
output_ext = self.output_song.split('.')[-1]
input_ext = self.input_file.split('.')[-1]
output_ext = self.output_file.split('.')[-1]
if input_ext == 'm4a':
if output_ext == 'mp3':
@@ -69,9 +68,8 @@ class Converter:
elif output_ext == 'm4a':
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
command = '{0}-i {1} {2}{3}'.format(
ffmpeg_pre, os.path.join(self.folder, self.input_song),
ffmpeg_params, os.path.join(self.folder, self.output_song)).split(' ')
ffmpeg_pre += ' -i'
command = ffmpeg_pre.split() + [self.input_file] + ffmpeg_params.split() + [self.output_file]
log.debug(command)
return subprocess.call(command)

View File

@@ -1,3 +1,5 @@
from core import internals
import logging
import yaml
import argparse
@@ -19,7 +21,8 @@ default_conf = { 'spotify-downloader':
'download-only-metadata' : False,
'dry-run' : False,
'music-videos-only' : False,
'preserve-spaces' : False,
'no-spaces' : False,
'file-format' : '{artist} - {track_name}',
'log-level' : 'INFO' }
}
@@ -50,13 +53,16 @@ def get_config(config_file):
return cfg['spotify-downloader']
def get_arguments(to_group=True, raw_args=None):
def get_arguments(raw_args=None, to_group=True, to_merge=True):
parser = argparse.ArgumentParser(
description='Download and convert songs from Spotify, Youtube etc.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
config_file = os.path.join(sys.path[0], 'config.yml')
config = merge(default_conf, get_config(config_file))
if to_merge:
config_file = os.path.join(sys.path[0], 'config.yml')
config = merge(default_conf['spotify-downloader'], get_config(config_file))
else:
config = default_conf['spotify-downloader']
if to_group:
group = parser.add_mutually_exclusive_group(required=True)
@@ -84,7 +90,7 @@ def get_arguments(to_group=True, raw_args=None):
help='Use avconv for conversion otherwise set defaults to ffmpeg',
action='store_true')
parser.add_argument(
'-f', '--folder', default=config['folder'],
'-f', '--folder', default=os.path.relpath(config['folder'], os.getcwd()),
help='path to folder where files will be stored in')
parser.add_argument(
'--overwrite', default=config['overwrite'],
@@ -96,6 +102,12 @@ def get_arguments(to_group=True, raw_args=None):
parser.add_argument(
'-o', '--output-ext', default=config['output-ext'],
help='prefered output extension .mp3 or .m4a (AAC)')
parser.add_argument(
'-ff', '--file-format', default=config['file-format'],
help='File format to save the downloaded song with, each tag '
'is surrounded by curly braces. Possible formats: '
'{}'.format([internals.formats[x] for x in internals.formats]),
action='store_true')
parser.add_argument(
'-dm', '--download-only-metadata', default=config['download-only-metadata'],
help='download songs for which metadata is found',
@@ -109,8 +121,8 @@ def get_arguments(to_group=True, raw_args=None):
help='Search only for music on Youtube',
action='store_true')
parser.add_argument(
'-ps', '--preserve-spaces', default=config['preserve-spaces'],
help='Preserve spaces on file names',
'-ns', '--no-spaces', default=config['no-spaces'],
help='Replace spaces with underscores in file names',
action='store_true')
parser.add_argument(
'-ll', '--log-level', default=config['log-level'],

View File

@@ -1,4 +1,5 @@
from core.const import log
from slugify import SLUG_OK, slugify
from core import const
try:
from slugify import SLUG_OK, slugify
@@ -7,6 +8,21 @@ except ImportError:
import os
log = const.log
formats = { 0 : 'track_name',
1 : 'artist',
2 : 'album',
3 : 'album_artist',
4 : 'genre',
5 : 'disc_number',
6 : 'duration',
7 : 'year',
8 : 'original_date',
9 : 'track_number',
10 : 'total_tracks',
11 : 'isrc' }
def input_link(links):
""" Let the user input a choice. """
@@ -47,19 +63,37 @@ def is_youtube(raw_song):
return status
def generate_songname(tags):
def generate_songname(file_format, tags):
""" Generate a string of the format '[artist] - [song]' for the given spotify song. """
raw_song = u'{0} - {1}'.format(tags['artists'][0]['name'], tags['name'])
return raw_song
format_tags = dict(formats)
format_tags[0] = tags['name']
format_tags[1] = tags['artists'][0]['name']
format_tags[2] = tags['album']['name']
format_tags[3] = tags['artists'][0]['name']
format_tags[4] = tags['genre']
format_tags[5] = tags['disc_number']
format_tags[6] = tags['duration']
format_tags[7] = tags['year']
format_tags[8] = tags['release_date']
format_tags[9] = tags['track_number']
format_tags[10] = tags['total_tracks']
format_tags[11] = tags['external_ids']['isrc']
for x in formats:
file_format = file_format.replace('{' + formats[x] + '}',
str(format_tags[x]))
if const.args.no_spaces:
file_format = file_format.replace(' ', '_')
return file_format
def sanitize_title(title):
""" Generate filename of the song to be downloaded. """
title = title.replace(' ', '_')
title = title.replace('/', '_')
# slugify removes any special characters
title = slugify(title, ok='-_()[]{}', lower=False)
title = slugify(title, ok='-_()[]{}\/', lower=False,
spaces=(not const.args.no_spaces))
return title

View File

@@ -64,7 +64,7 @@ class EmbedMetadata:
audiofile['arranger'] = meta_tags['artists'][0]['name']
audiofile['performer'] = meta_tags['artists'][0]['name']
audiofile['website'] = meta_tags['external_urls']['spotify']
audiofile['length'] = str(meta_tags['duration_ms'] / 1000.0)
audiofile['length'] = str(meta_tags['duration'])
if meta_tags['publisher']:
audiofile['encodedby'] = meta_tags['publisher']
if meta_tags['genre']:
@@ -79,9 +79,8 @@ class EmbedMetadata:
# https://github.com/quodlibet/mutagen/blob/master/mutagen/id3/_frames.py
# Each class represents an id3 tag
audiofile = ID3(music_file)
year, *_ = meta_tags['release_date'].split('-')
audiofile['TORY'] = TORY(encoding=3, text=year)
audiofile['TYER'] = TYER(encoding=3, text=year)
audiofile['TORY'] = TORY(encoding=3, text=meta_tags['year'])
audiofile['TYER'] = TYER(encoding=3, text=meta_tags['year'])
audiofile['TPUB'] = TPUB(encoding=3, text=meta_tags['publisher'])
audiofile['COMM'] = COMM(encoding=3, text=meta_tags['external_urls']['spotify'])
if meta_tags['lyrics']:
@@ -131,8 +130,7 @@ class EmbedMetadata:
meta_tags['total_tracks'])]
audiofile[tags['disknumber']] = [(meta_tags['disc_number'], 0)]
audiofile[tags['date']] = meta_tags['release_date']
year, *_ = meta_tags['release_date'].split('-')
audiofile[tags['year']] = year
audiofile[tags['year']] = meta_tags['year']
audiofile[tags['originaldate']] = meta_tags['release_date']
audiofile[tags['comment']] = meta_tags['external_urls']['spotify']
if meta_tags['genre']:

View File

@@ -66,7 +66,10 @@ def generate_metadata(raw_song):
except lyricwikia.LyricsNotFound:
meta_tags['lyrics'] = None
# remove unused clutter when debug meta_tags
# fix clutter
meta_tags['year'], *_ = meta_tags['release_date'].split('-')
meta_tags['duration'] = meta_tags['duration_ms'] / 1000.0
del meta_tags['duration_ms']
del meta_tags['available_markets']
del meta_tags['album']['available_markets']

View File

@@ -69,7 +69,8 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5):
song = raw_song
query['q'] = song
else:
song = internals.generate_songname(meta_tags)
song = '{0} - {1}'.format(meta_tags['artists'][0]['name'],
meta_tags['name'])
query['q'] = song
log.debug('query: {0}'.format(query))
@@ -123,7 +124,7 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5):
the duration_tolerance has reached the max_duration_tolerance
'''
while len(possible_videos_by_duration) == 0:
possible_videos_by_duration = list(filter(lambda x: abs(x['seconds'] - (int(meta_tags['duration_ms'])/1000)) <= duration_tolerance, videos))
possible_videos_by_duration = list(filter(lambda x: abs(x['seconds'] - meta_tags['duration']) <= duration_tolerance, videos))
duration_tolerance += 1
if duration_tolerance > max_duration_tolerance:
log.error("{0} by {1} was not found.\n".format(meta_tags['name'], meta_tags['artists'][0]['name']))