Increase coverage (#218)

* Monkeypatch fetch user and use pytest.tempdir

* Cover spotify_tools.grab_album()

* Cover avconv conversion

* Cover grab_single()

* Reduce code repetition

* Move grab_playlist() to spotify_tools.py

* Move Spotify specific functions to spotify_tools.py

* Refactoring

* Return track list from write_tracks()

* Fix tests

* Cover more cases in generate_youtube_url()

* Test for unavailable audio streams

* Test for filename without spaces

* handle.py 100% coverage

* Improve config tests

* Speed up tests

* Install avconv and libfdk-aac

* Some cleaning

* FFmpeg with libfdk-aac, libopus

* Some refactoring

* Convert tmpdir to string

* Cover YouTube title when downloading from list

* Explicitly cover some internals.py functions
This commit is contained in:
Ritiek Malhotra
2018-01-26 20:44:37 +05:30
committed by GitHub
parent d624bbb3d5
commit 3e6b2d7702
18 changed files with 474 additions and 310 deletions

View File

@@ -42,7 +42,7 @@ class Converter:
command = ['avconv', '-loglevel', level, '-i',
self.input_file, '-ab', '192k',
self.output_file]
self.output_file, '-y']
log.debug(command)
return subprocess.call(command)
@@ -53,19 +53,19 @@ class Converter:
if not log.level == 10:
ffmpeg_pre += '-hide_banner -nostats -v panic '
input_ext = self.input_file.split('.')[-1]
output_ext = self.output_file.split('.')[-1]
_, input_ext = os.path.splitext(self.input_file)
_, output_ext = os.path.splitext(self.output_file)
if input_ext == 'm4a':
if output_ext == 'mp3':
if input_ext == '.m4a':
if output_ext == '.mp3':
ffmpeg_params = '-codec:v copy -codec:a libmp3lame -q:a 2 '
elif output_ext == 'webm':
elif output_ext == '.webm':
ffmpeg_params = '-c:a libopus -vbr on -b:a 192k -vn '
elif input_ext == 'webm':
if output_ext == 'mp3':
elif input_ext == '.webm':
if output_ext == '.mp3':
ffmpeg_params = ' -ab 192k -ar 44100 -vn '
elif output_ext == 'm4a':
elif output_ext == '.m4a':
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
ffmpeg_pre += ' -i'

View File

@@ -48,6 +48,7 @@ def trim_song(text_file):
data = file_in.read().splitlines(True)
with open(text_file, 'w') as file_out:
file_out.writelines(data[1:])
return data[0]
def is_spotify(raw_song):
@@ -117,3 +118,13 @@ def videotime_from_seconds(time):
return '{0}:{1:02}'.format(time//60, time % 60)
return '{0}:{1:02}:{2:02}'.format((time//60)//60, (time//60) % 60, time % 60)
def get_splits(url):
if '/' in url:
if url.endswith('/'):
url = url[:-1]
splits = url.split('/')
else:
splits = url.split(':')
return splits

View File

@@ -8,6 +8,7 @@ import urllib.request
def compare(music_file, metadata):
"""Check if the input music file title matches the expected title."""
already_tagged = False
try:
if music_file.endswith('.mp3'):
audiofile = EasyID3(music_file)
@@ -16,7 +17,7 @@ def compare(music_file, metadata):
audiofile = MP4(music_file)
already_tagged = audiofile['\xa9nam'][0] == metadata['name']
except (KeyError, TypeError):
already_tagged = False
pass
return already_tagged

View File

@@ -77,7 +77,13 @@ def generate_metadata(raw_song):
return meta_tags
def feed_playlist(username):
def write_user_playlist(username, text_file=None):
links = get_playlists(username=username)
playlist = internals.input_link(links)
return write_playlist(playlist, text_file)
def get_playlists(username):
""" Fetch user playlists when using the -u option. """
playlists = spotify.user_playlists(username)
links = []
@@ -91,19 +97,65 @@ def feed_playlist(username):
log.info(u'{0:>5}. {1:<30} ({2} tracks)'.format(
check, playlist['name'],
playlist['tracks']['total']))
log.debug(playlist['external_urls']['spotify'])
links.append(playlist)
playlist_url = playlist['external_urls']['spotify']
log.debug(playlist_url)
links.append(playlist_url)
check += 1
if playlists['next']:
playlists = spotify.next(playlists)
else:
break
playlist = internals.input_link(links)
write_playlist(playlist['owner']['id'], playlist['id'])
return links
def write_tracks(text_file, tracks):
def fetch_playlist(playlist):
splits = internals.get_splits(playlist)
try:
username = splits[-3]
except IndexError:
# Wrong format, in either case
log.error('The provided playlist URL is not in a recognized format!')
sys.exit(10)
playlist_id = splits[-1]
try:
results = spotify.user_playlist(username, playlist_id,
fields='tracks,next,name')
except spotipy.client.SpotifyException:
log.error('Unable to find playlist')
log.info('Make sure the playlist is set to publicly visible and then try again')
sys.exit(11)
return results
def write_playlist(playlist_url, text_file=None):
playlist = fetch_playlist(playlist_url)
tracks = playlist['tracks']
if not text_file:
text_file = u'{0}.txt'.format(slugify(playlist['name'], ok='-_()[]{}'))
return write_tracks(tracks, text_file)
def fetch_album(album):
splits = internals.get_splits(album)
album_id = splits[-1]
album = spotify.album(album_id)
return album
def write_album(album_url, text_file=None):
album = fetch_album(album_url)
tracks = spotify.album_tracks(album['id'])
if not text_file:
text_file = u'{0}.txt'.format(slugify(album['name'], ok='-_()[]{}'))
return write_tracks(tracks, text_file)
def write_tracks(tracks, text_file):
log.info(u'Writing {0} tracks to {1}'.format(
tracks['total'], text_file))
track_urls = []
with open(text_file, 'a') as file_out:
while True:
for item in tracks['items']:
@@ -113,8 +165,9 @@ def write_tracks(text_file, tracks):
track = item
try:
track_url = track['external_urls']['spotify']
file_out.write(track_url + '\n')
log.debug(track_url)
file_out.write(track_url + '\n')
track_urls.append(track_url)
except KeyError:
log.warning(u'Skipping track {0} by {1} (local only?)'.format(
track['name'], track['artists'][0]['name']))
@@ -124,34 +177,5 @@ def write_tracks(text_file, tracks):
tracks = spotify.next(tracks)
else:
break
def write_playlist(username, playlist_id):
results = spotify.user_playlist(username, playlist_id,
fields='tracks,next,name')
text_file = u'{0}.txt'.format(slugify(results['name'], ok='-_()[]{}'))
log.info(u'Writing {0} tracks to {1}'.format(
results['tracks']['total'], text_file))
tracks = results['tracks']
write_tracks(text_file, tracks)
def write_album(album):
tracks = spotify.album_tracks(album['id'])
text_file = u'{0}.txt'.format(slugify(album['name'], ok='-_()[]{}'))
log.info(u'writing {0} tracks to {1}'.format(
tracks['total'], text_file))
write_tracks(text_file, tracks)
def grab_album(album):
if '/' in album:
if album.endswith('/'):
playlist = playlist[:-1]
splits = album.split('/')
else:
splits = album.split(':')
album_id = splits[-1]
album = spotify.album(album_id)
write_album(album)
log.info(track_urls)
return track_urls

View File

@@ -35,19 +35,21 @@ def get_youtube_title(content, number=None):
def download_song(file_name, content):
""" Download the audio file from YouTube. """
if const.args.input_ext in (".webm", ".m4a"):
link = content.getbestaudio(preftype=const.args.input_ext[1:])
_, extension = os.path.splitext(file_name)
if extension in ('.webm', '.m4a'):
link = content.getbestaudio(preftype=extension[1:])
else:
log.debug('No audio streams available for {} type'.format(extension))
return False
if link:
log.debug('Downloading from URL: ' + link.url)
filepath = '{0}{1}'.format(os.path.join(const.args.folder, file_name),
const.args.input_ext)
filepath = os.path.join(const.args.folder, file_name)
log.debug('Saving to: ' + filepath)
link.download(filepath=filepath)
return True
else:
log.debug('No audio streams available')
return False