diff --git a/badwitch.py b/badwitch.py index 560530b..bd7f967 100755 --- a/badwitch.py +++ b/badwitch.py @@ -15,6 +15,7 @@ import json import logging import pathlib import sys +import youtube_dl class Library: # A thing full of albums @@ -48,18 +49,35 @@ class Library: raise Exception('Library not valid') # Download library - def download(self, album=None): - if album is not None: + def download(self, targetalbum=None): + if targetalbum is not None: print('Downloading album: ' + album) else: print('Downloading entire library') for album, albumcontent in self.albums.items(): + # Skip albums that don't match our criterea + if targetalbum is not None and not album == targetalbum: + logging.debug('Skipping album ' + album) + continue # God have mercy on my soul - artist = (next(iter(next(iter(albumcontent.values())).values()))) - print(artist) - Path(Path.home() / 'Music' / artist / album).mkdir(parents=True, exist_ok=True) + artist = next(iter(albumcontent.values()))['artist'] + destpath = (Path.home() / 'Music' / artist / album) + Path(destpath).mkdir(parents=True, exist_ok=True) for song, songcontent in albumcontent.items(): - logging.info('Downloaded song: ' + song + ' by ' + songcontent['artist']) + # See if we already have it + matches = sorted(Path(destpath).glob(song + '.*')) + if not matches == []: + logging.debug('Using cached song: ' + song) + continue + # Download the song + ytdl_opts = { + 'audio-format': 'best', + 'x' + 'playlist-items': 1 + } + with youtube_dl.YoutubeDL(ytdl_opts) as ydl: + ydl.download([songcontent['source']]) + logging.info('Downloaded song: ' + song) class BadWitch: # Our program @@ -99,32 +117,41 @@ class BadWitch: elif self.args.action == 'list': for album, albumcontent in lib.albums.items(): for song, songcontent in albumcontent.items(): - print(album + ' - ' + song + ' by ' + songcontent['artist']) + print(album + + ' - ' + str(songcontent['track']) + + ' - ' + song + + ' by ' + songcontent['artist']) return elif self.args.action == 'test': # Set up a test album lib.albums['Bad Witch'] = { 'Shit Mirror': { + 'track': 1, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=yeqjz5mXrLQ' }, 'Ahead of Ourselves': { + 'track': 2, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=4Ab1O-i4ep4' }, 'Play the Goddamned Part': { + 'track': 3, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=85UgvBkMfr8' }, 'God Break Down the Door': { + 'track': 4, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=eeJ_DzRJUI4' }, 'I\'m Not From This World': { + 'track': 5, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=9fjbcSUSt9w' }, 'Over and Out': { + 'track': 6, 'artist': 'Nine Inch Nails', 'source': 'https://www.youtube.com/watch?v=h-XlN3N2fis' } diff --git a/requirements.txt b/requirements.txt index f1265fe..7b4b647 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ appdirs>=1.4.3 +youtube-dl