Added in config and --setup flag

This commit is contained in:
Cooper Hammond 2019-05-28 22:36:23 -07:00
parent 422fdd232c
commit 1b044b615a
8 changed files with 83 additions and 31 deletions

0
irs/cli/__init__.py Normal file
View file

View file

@ -1,17 +1,20 @@
import sys
import os
import argparse import argparse
from .song import Song from ..glue.song import Song
from .album import Album from ..glue.album import Album
from .playlist import Playlist from ..glue.playlist import Playlist
from ..install.setup import set_it_up
from .config_parser import parse_config
def main(): def main():
""" """The main cli method. Parses arguments from the command line."""
"""
parser = argparse.ArgumentParser() 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", parser.add_argument("-a", "--artist", dest="artist",
help="Specify artist name. Must be used with -s/--song or -A/--album") 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", parser.add_argument("-o", "--organization", dest="organization",
default="single-folder", help="Specify type of organization for list." 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() args = parser.parse_args()
set_local_env() if args.setup:
set_it_up()
parse_config()
if args.song and args.artist: # single song if args.song and args.artist: # single song
Song(args.song, args.artist).grab_it() Song(args.song, args.artist).grab_it()
@ -43,9 +49,3 @@ def main():
Album(args.album).grab_it() Album(args.album).grab_it()
elif args.playlist and args.username: # playlist elif args.playlist and args.username: # playlist
Playlist(args.playlist, args.username, args.organization).grab_it() 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"
)

51
irs/cli/config_parser.py Normal file
View file

@ -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_

View file

@ -185,7 +185,6 @@ class Song(object):
"""If metadata has not been provided, search for it.""" """If metadata has not been provided, search for it."""
if not self.metadata: if not self.metadata:
print("Searching for metadata ...")
self.metadata = self.spotify_searcher.find_song( self.metadata = self.spotify_searcher.find_song(
self.song_title, self.artist_name self.song_title, self.artist_name
) )

1
irs/install Submodule

@ -0,0 +1 @@
Subproject commit 3a8323f39eda597ac5c4b4e2559577ca757aedc0

View file

@ -1,3 +1,4 @@
import os
import sys import sys
import glob import glob
import shutil import shutil
@ -22,7 +23,7 @@ def rip_from_url(video_url, output_name):
'progress_hooks': [_download_hook], 'progress_hooks': [_download_hook],
'output': "tmp_file", 'output': "tmp_file",
'prefer-ffmpeg': True, '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: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
@ -43,6 +44,7 @@ class _DownloadLogger(object):
print(msg) print(msg)
# TODO: update the download log
def _download_hook(d): def _download_hook(d):
if d['status'] == 'finished': if d['status'] == 'finished':
print("Done!") print("Done!")

View file

@ -1,13 +1,10 @@
import os
import re import re
import spotipy import spotipy
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
CLIENT_ID = 'e4198f6a3f7b48029366f22528b5dc66'
CLIENT_SECRET = 'ba057d0621a5496bbb64edccf758bde5'
class SpotifySearcher(object): class SpotifySearcher(object):
"""Searches spotify for song, album, and playlist metadata.""" """Searches spotify for song, album, and playlist metadata."""
@ -18,9 +15,9 @@ class SpotifySearcher(object):
# TODO: remove these when you finish config files # TODO: remove these when you finish config files
if not client_id: if not client_id:
client_id = 'e4198f6a3f7b48029366f22528b5dc66' client_id = os.environ["SPOTIFY_CLIENT_ID"]
if not client_secret: if not client_secret:
client_secret = 'ba057d0621a5496bbb64edccf758bde5' client_secret = os.environ["SPOTIFY_CLIENT_SECRET"]
try: try:
creds = SpotifyClientCredentials(client_id, client_secret) creds = SpotifyClientCredentials(client_id, client_secret)

View file

@ -8,15 +8,17 @@ setup(
author = 'Kepoor Hampond', author = 'Kepoor Hampond',
author_email = 'kepoorh@gmail.com', author_email = 'kepoorh@gmail.com',
license = 'GPL', license = 'GPL',
packages = ['irs', 'irs.search', 'irs.interact', 'irs.glue'], packages = ['irs', 'irs.search', 'irs.interact', 'irs.glue',
'irs.install', 'irs.cli'],
install_requires = [ install_requires = [
'bs4', 'bs4', # HTML parsing
'mutagen', 'mutagen', # MP3 tags
'argparse' 'argparse', # CLI arg parsing
'spotipy', 'spotipy', # Interfacing w/ Spotify API
'ydl-binaries', 'ydl-binaries', # Downloading ffmpeg/ffprobe binaries
'pyyaml' # Config files done simply
], ],
entry_points = { entry_points = {
'console_scripts': ['irs = irs.glue.cli:main'], 'console_scripts': ['irs = irs.cli.cli:main'],
}, },
) )