From 1b044b615ad12ae135625caa49c55e1dc95d16cf Mon Sep 17 00:00:00 2001 From: Cooper Hammond Date: Tue, 28 May 2019 22:36:23 -0700 Subject: [PATCH] Added in config and --setup flag --- irs/cli/__init__.py | 0 irs/{glue => cli}/cli.py | 32 ++++++++++++------------- irs/cli/config_parser.py | 51 ++++++++++++++++++++++++++++++++++++++++ irs/glue/song.py | 1 - irs/install | 1 + irs/interact/ripper.py | 4 +++- irs/search/spotify.py | 9 +++---- setup.py | 16 +++++++------ 8 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 irs/cli/__init__.py rename irs/{glue => cli}/cli.py (70%) create mode 100644 irs/cli/config_parser.py create mode 160000 irs/install diff --git a/irs/cli/__init__.py b/irs/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/irs/glue/cli.py b/irs/cli/cli.py similarity index 70% rename from irs/glue/cli.py rename to irs/cli/cli.py index eaae45f..8079625 100644 --- a/irs/glue/cli.py +++ b/irs/cli/cli.py @@ -1,17 +1,20 @@ -import sys -import os - import argparse -from .song import Song -from .album import Album -from .playlist import Playlist +from ..glue.song import Song +from ..glue.album import Album +from ..glue.playlist import Playlist +from ..install.setup import set_it_up +from .config_parser import parse_config def main(): - """ - """ + """The main cli method. Parses arguments from the command line.""" + parser = argparse.ArgumentParser() + parser.add_argument("-S", "--setup", dest="setup", action='store_true', + help="Run this by itself to setup config files " + "and folder for irs and download the ffmpeg binaries") + parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song or -A/--album") @@ -29,11 +32,14 @@ def main(): parser.add_argument("-o", "--organization", dest="organization", default="single-folder", help="Specify type of organization for list." - "Used when downloading spotify playlist/album") + " Used when downloading spotify playlist/album") args = parser.parse_args() - set_local_env() + if args.setup: + set_it_up() + + parse_config() if args.song and args.artist: # single song Song(args.song, args.artist).grab_it() @@ -43,9 +49,3 @@ def main(): Album(args.album).grab_it() elif args.playlist and args.username: # playlist Playlist(args.playlist, args.username, args.organization).grab_it() - - -def set_local_env(): - os.environ["irs_music_dir"] = os.path.join( - os.environ["HOME"], "Audio" - ) \ No newline at end of file diff --git a/irs/cli/config_parser.py b/irs/cli/config_parser.py new file mode 100644 index 0000000..2f5a4a1 --- /dev/null +++ b/irs/cli/config_parser.py @@ -0,0 +1,51 @@ +import os +import sys + +import yaml + + +def parse_config(): + """Parses config using environment variables.""" + + check_for_and_set("irs_config_dir", os.environ["HOME"] + "/.irs", None) + + home = os.environ["HOME"] + check_for = [home + "/.irs/config.yml", home + "/.irs/bin/ffmpeg", + home + "/.irs/bin/ffprobe"] + + for path in check_for: + if not os.path.exists(path): + print("There's no config set up. Set up a configuration folder by " + "running `irs --setup`") + sys.exit(1) + + config = {} + + with open(os.environ["irs_config_dir"] + "/config.yml", "r") as stream: + try: + config = yaml.safe_load(stream) + except yaml.YAMLError as exc: + print(exc) + + check_for_and_set("SPOTIFY_CLIENT_ID", config.get( + "SPOTIFY_KEYS").get("CLIENT_ID"), None) + check_for_and_set("SPOTIFY_CLIENT_SECRET", config.get( + "SPOTIFY_KEYS").get("CLIENT_SECRET"), None) + + check_for_and_set("irs_music_dir", os.path.expanduser(config.get("music_directory")), + os.environ["HOME"] + "/Music") + check_for_and_set("irs_ffmpeg_dir", os.environ["irs_config_dir"] + "/bin", None) + + +def check_for_and_set(key, val, else_): + """Checks for an environment variable and if it doesn't exist, then set it + equal to the val given. + :param key: string, key to check for existence + :param val: value to replace key value with if it doesn't exists + :param else_: if val doesn't exist, use else_ instead + """ + if not os.environ.get(key): + if key: + os.environ[key] = val + else: + os.environ[key] = else_ diff --git a/irs/glue/song.py b/irs/glue/song.py index d819c9a..ca8f0e6 100644 --- a/irs/glue/song.py +++ b/irs/glue/song.py @@ -185,7 +185,6 @@ class Song(object): """If metadata has not been provided, search for it.""" if not self.metadata: - print("Searching for metadata ...") self.metadata = self.spotify_searcher.find_song( self.song_title, self.artist_name ) diff --git a/irs/install b/irs/install new file mode 160000 index 0000000..3a8323f --- /dev/null +++ b/irs/install @@ -0,0 +1 @@ +Subproject commit 3a8323f39eda597ac5c4b4e2559577ca757aedc0 diff --git a/irs/interact/ripper.py b/irs/interact/ripper.py index c417feb..b6dd57e 100644 --- a/irs/interact/ripper.py +++ b/irs/interact/ripper.py @@ -1,3 +1,4 @@ +import os import sys import glob import shutil @@ -22,7 +23,7 @@ def rip_from_url(video_url, output_name): 'progress_hooks': [_download_hook], 'output': "tmp_file", 'prefer-ffmpeg': True, - #'ffmpeg_location': os.path.expanduser("~/.irs/bin/"), + 'ffmpeg_location': os.environ["irs_ffmpeg_dir"], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: @@ -43,6 +44,7 @@ class _DownloadLogger(object): print(msg) +# TODO: update the download log def _download_hook(d): if d['status'] == 'finished': print("Done!") \ No newline at end of file diff --git a/irs/search/spotify.py b/irs/search/spotify.py index 6691206..8628c53 100644 --- a/irs/search/spotify.py +++ b/irs/search/spotify.py @@ -1,13 +1,10 @@ +import os import re import spotipy from spotipy.oauth2 import SpotifyClientCredentials -CLIENT_ID = 'e4198f6a3f7b48029366f22528b5dc66' -CLIENT_SECRET = 'ba057d0621a5496bbb64edccf758bde5' - - class SpotifySearcher(object): """Searches spotify for song, album, and playlist metadata.""" @@ -18,9 +15,9 @@ class SpotifySearcher(object): # TODO: remove these when you finish config files if not client_id: - client_id = 'e4198f6a3f7b48029366f22528b5dc66' + client_id = os.environ["SPOTIFY_CLIENT_ID"] if not client_secret: - client_secret = 'ba057d0621a5496bbb64edccf758bde5' + client_secret = os.environ["SPOTIFY_CLIENT_SECRET"] try: creds = SpotifyClientCredentials(client_id, client_secret) diff --git a/setup.py b/setup.py index e7c7731..96b4e66 100644 --- a/setup.py +++ b/setup.py @@ -8,15 +8,17 @@ setup( author = 'Kepoor Hampond', author_email = 'kepoorh@gmail.com', license = 'GPL', - packages = ['irs', 'irs.search', 'irs.interact', 'irs.glue'], + packages = ['irs', 'irs.search', 'irs.interact', 'irs.glue', + 'irs.install', 'irs.cli'], install_requires = [ - 'bs4', - 'mutagen', - 'argparse' - 'spotipy', - 'ydl-binaries', + 'bs4', # HTML parsing + 'mutagen', # MP3 tags + 'argparse', # CLI arg parsing + 'spotipy', # Interfacing w/ Spotify API + 'ydl-binaries', # Downloading ffmpeg/ffprobe binaries + 'pyyaml' # Config files done simply ], entry_points = { - 'console_scripts': ['irs = irs.glue.cli:main'], + 'console_scripts': ['irs = irs.cli.cli:main'], }, )