mirror of
https://github.com/cooperhammond/irs.git
synced 2025-01-02 19:15:26 +00:00
Merge branch 'master' of https://github.com/kepoorhampond/irs
This commit is contained in:
commit
4a1d34db0b
|
@ -5,7 +5,7 @@ Ironic Redistribution System
|
||||||
|
|
||||||
[![License: GNU](https://img.shields.io/badge/license-gnu-yellow.svg?style=flat-square)](http://www.gnu.org/licenses/gpl.html)
|
[![License: GNU](https://img.shields.io/badge/license-gnu-yellow.svg?style=flat-square)](http://www.gnu.org/licenses/gpl.html)
|
||||||
[![Stars](https://img.shields.io/github/stars/kepoorhampond/irs.svg?style=flat-square)](https://github.com/kepoorhampond/irs/stargazers)
|
[![Stars](https://img.shields.io/github/stars/kepoorhampond/irs.svg?style=flat-square)](https://github.com/kepoorhampond/irs/stargazers)
|
||||||
[![Build Status](https://img.shields.io/travis/kepoorhampond/irs/master.svg?style=flat-square)](https://travis-ci.org/karma-runner/karma-coverage)
|
[![Build Status](https://img.shields.io/travis/kepoorhampond/irs/master.svg?style=flat-square)](https://travis-ci.org/kepoorhampond/irs)
|
||||||
[![Say Thanks](https://img.shields.io/badge/say-thanks-ff69b4.svg?style=flat-square)](https://saythanks.io/to/kepoorhampond)
|
[![Say Thanks](https://img.shields.io/badge/say-thanks-ff69b4.svg?style=flat-square)](https://saythanks.io/to/kepoorhampond)
|
||||||
[![Coverage Status](http://img.shields.io/coveralls/kepoorhampond/irs.svg?style=flat-square)](https://coveralls.io/github/kepoorhampond/irs?branch=master&style=flat-square)
|
[![Coverage Status](http://img.shields.io/coveralls/kepoorhampond/irs.svg?style=flat-square)](https://coveralls.io/github/kepoorhampond/irs?branch=master&style=flat-square)
|
||||||
[![PyPI](https://img.shields.io/badge/pypi-irs-blue.svg?style=flat-square)](https://pypi.python.org/pypi/irs)
|
[![PyPI](https://img.shields.io/badge/pypi-irs-blue.svg?style=flat-square)](https://pypi.python.org/pypi/irs)
|
||||||
|
@ -42,6 +42,7 @@ optional arguments:
|
||||||
-l LOCATION, --location LOCATION
|
-l LOCATION, --location LOCATION
|
||||||
Specify a directory to place files in.
|
Specify a directory to place files in.
|
||||||
-o, --organize Organize downloaded files.
|
-o, --organize Organize downloaded files.
|
||||||
|
-c, --config Display path to config file.
|
||||||
```
|
```
|
||||||
So all of these are valid commands:
|
So all of these are valid commands:
|
||||||
```
|
```
|
||||||
|
|
38
irs/cli.py
38
irs/cli.py
|
@ -7,38 +7,48 @@ import os
|
||||||
|
|
||||||
# Powered by:
|
# Powered by:
|
||||||
from .ripper import Ripper
|
from .ripper import Ripper
|
||||||
from .utils import console, remove_none_values
|
from .utils import console, parse_default_flags
|
||||||
|
from .config import CONFIG
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
# Single Song
|
# Single Song
|
||||||
parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song")
|
parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song")
|
||||||
parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist")
|
parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist")
|
||||||
|
|
||||||
# Album
|
# Album
|
||||||
parser.add_argument("-A", "--album", dest="album", help="Specify album name")
|
parser.add_argument("-A", "--album", dest="album", help="Specify album name")
|
||||||
|
|
||||||
# Playlist
|
# Playlist
|
||||||
parser.add_argument("-u", "--username", dest="username", help="Specify username. Must be used with -p/--playlist")
|
parser.add_argument("-u", "--username", dest="username", help="Specify username. Must be used with -p/--playlist")
|
||||||
parser.add_argument("-p", "--playlist", dest="playlist", help="Specify playlist name. Must be used with -u/--username")
|
parser.add_argument("-p", "--playlist", dest="playlist", help="Specify playlist name. Must be used with -u/--username")
|
||||||
|
|
||||||
# Post-Processors
|
# Post-Processors
|
||||||
parser.add_argument("-l", "--location", dest="location", help="Specify a directory to place files in.")
|
parser.add_argument("-l", "--location", dest="location", help="Specify a directory to place files in.")
|
||||||
parser.add_argument("-o", "--organize", dest="organize", action="store_true", help="Organize downloaded files.")
|
parser.add_argument("-o", "--organize", dest="organize", action="store_true", help="Organize downloaded files.")
|
||||||
|
|
||||||
|
# Config
|
||||||
args = parser.parse_args()
|
parser.add_argument("-c", "--config", dest="config", action="store_true", help="Display path to config file.")
|
||||||
|
|
||||||
|
|
||||||
|
args = parser.parse_args(parse_default_flags())
|
||||||
|
|
||||||
|
if args.config:
|
||||||
|
import irs
|
||||||
|
print (os.path.dirname(irs.__file__) + "/config.py")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
ripper_args = {
|
ripper_args = {
|
||||||
"post_processors": {
|
"post_processors": {
|
||||||
"location": args.location,
|
"custom_directory": args.location,
|
||||||
"organize": args.organize,
|
"organize": args.organize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ripper = Ripper(ripper_args)
|
ripper = Ripper(ripper_args)
|
||||||
|
|
||||||
if args.artist and args.song:
|
if args.artist and args.song:
|
||||||
ripper.song(args.song, args.artist)
|
ripper.song(args.song, args.artist)
|
||||||
elif args.album:
|
elif args.album:
|
||||||
|
@ -46,7 +56,7 @@ def main():
|
||||||
elif args.username and args.playlist:
|
elif args.username and args.playlist:
|
||||||
ripper.spotify_list("playlist", args.playlist, args.username)
|
ripper.spotify_list("playlist", args.playlist, args.username)
|
||||||
else:
|
else:
|
||||||
console(Ripper())
|
console(ripper)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
|
@ -1,29 +1,22 @@
|
||||||
from os.path import expanduser
|
|
||||||
|
|
||||||
CONFIG = dict(
|
CONFIG = dict(
|
||||||
|
|
||||||
# To autostart rhythmbox with a new song:
|
default_flags = ['-o'],
|
||||||
# default_flags = '-c rhythmbox %(loc)s',
|
# For default flags. Right now, it organizes your files into an
|
||||||
# To make choosing of links default:
|
# artist/album/song structure.
|
||||||
# default_flags = '-l',
|
# To add a flag or argument, add an element to the index:
|
||||||
# To place all playlist songs into one folder:
|
# default_flags = ['-o', '-l', '~/Music']
|
||||||
# default_flags = '-of',
|
|
||||||
default_flags = '',
|
|
||||||
|
|
||||||
|
SPOTIFY_CLIENT_ID = '',
|
||||||
# These are necessary to download Spotify playlists
|
SPOTIFY_CLIENT_SECRET = '',
|
||||||
client_id = '',
|
# You can either specify Spotify keys here, or in environment variables.
|
||||||
client_secret = '',
|
|
||||||
|
|
||||||
additional_search_terms = 'lyrics',
|
additional_search_terms = 'lyrics',
|
||||||
|
|
||||||
# For a custom directory. Note that `~` will not work as a shortcut in a
|
organize = True,
|
||||||
# plain text manner.
|
# True always forces organization.
|
||||||
directory = str(expanduser("~")) + "/Music",
|
# False always forces non-organization.
|
||||||
|
# None allows options and flags to determine if the files will be organized.
|
||||||
|
|
||||||
# If you want numbered file names
|
custom_directory = "",
|
||||||
numbered_file_names = True,
|
# Defaults to '~/Music'
|
||||||
|
|
||||||
# Downloaded file names
|
|
||||||
download_file_names = False,
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -73,6 +73,4 @@ def parse_genre(genres):
|
||||||
genres.sort(key=lambda x: len(x.split()))
|
genres.sort(key=lambda x: len(x.split()))
|
||||||
return genres[0].title()
|
return genres[0].title()
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
#Metadata("Da Frame 2R.mp3").add_album_art("https://i.scdn.co/image/c8931dc7eb717531de93ae0d4626362875c51ba9")
|
|
157
irs/ripper.py
157
irs/ripper.py
|
@ -19,7 +19,7 @@ elif sys.version_info[0] < 3:
|
||||||
else:
|
else:
|
||||||
print ("Must be using Python 2 or 3")
|
print ("Must be using Python 2 or 3")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Local utilities
|
# Local utilities
|
||||||
from .utils import *
|
from .utils import *
|
||||||
from .metadata import *
|
from .metadata import *
|
||||||
|
@ -30,28 +30,39 @@ class Ripper:
|
||||||
self.locations = []
|
self.locations = []
|
||||||
self.type = None
|
self.type = None
|
||||||
try:
|
try:
|
||||||
client_credentials_manager = SpotifyClientCredentials(os.environ["SPOTIFY_CLIENT_ID"], os.environ["SPOTIFY_CLIENT_SECRET"])
|
CLIENT_ID, CLIENT_SECRET = parse_spotify_creds(self)
|
||||||
|
client_credentials_manager = SpotifyClientCredentials(CLIENT_ID, CLIENT_SECRET)
|
||||||
self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
self.authorized = True
|
self.authorized = True
|
||||||
except spotipy.oauth2.SpotifyOauthError:
|
except Exception as e:
|
||||||
self.spotify = spotipy.Spotify()
|
self.spotify = spotipy.Spotify()
|
||||||
self.authorized = False
|
self.authorized = False
|
||||||
|
|
||||||
def post_processing(self, locations):
|
def post_processing(self, locations):
|
||||||
post_processors = self.args.get("post_processors")
|
post_processors = self.args.get("post_processors")
|
||||||
if post_processors:
|
if post_processors:
|
||||||
if type(post_processors.get("location")) is str:
|
if parse_directory(self) != None:
|
||||||
for index, loc in enumerate(locations):
|
for index, loc in enumerate(locations):
|
||||||
new_file_name = post_processors["location"] + "/" + loc
|
new_file_name = parse_directory(self) + "/" + loc
|
||||||
os.rename(loc, new_file_name)
|
os.rename(loc, new_file_name)
|
||||||
locations[index] = new_file_name
|
locations[index] = new_file_name
|
||||||
if post_processors.get("organize") == True:
|
# I'd just go on believing that code this terrible doesn't exist.
|
||||||
if self.type == "album":
|
# You can just close your eyes and scroll by. I'd encourage it.
|
||||||
|
# It's okay if you need to cry though.
|
||||||
|
# The rest of the code is here for you.
|
||||||
|
# It's like loving someone,
|
||||||
|
# Everyone has some flaws, but you still appreciate and embrace
|
||||||
|
# those flaws for being exclusive to them.
|
||||||
|
# And if those flaws are really enough to turn you off of them, then
|
||||||
|
# you *probably* don't really want to be with them anyways.
|
||||||
|
# Either way, it's up to you.
|
||||||
|
if parse_organize(self):
|
||||||
|
if self.type in ("album", "song"):
|
||||||
for index, loc in enumerate(locations):
|
for index, loc in enumerate(locations):
|
||||||
mp3 = Metadata(loc)
|
mp3 = Metadata(loc)
|
||||||
new_loc = ""
|
new_loc = ""
|
||||||
if len(loc.split("/")) > 1:
|
if len(loc.split("/")) >= 2:
|
||||||
new_loc = "/".join(loc.split("/")[0:-1])
|
new_loc = "/".join(loc.split("/")[0:-1]) + "/"
|
||||||
file_name = loc.split("/")[-1]
|
file_name = loc.split("/")[-1]
|
||||||
else:
|
else:
|
||||||
file_name = loc
|
file_name = loc
|
||||||
|
@ -66,6 +77,7 @@ class Ripper:
|
||||||
new_loc = new_loc.replace("//", "/")
|
new_loc = new_loc.replace("//", "/")
|
||||||
os.rename(loc, new_loc)
|
os.rename(loc, new_loc)
|
||||||
locations[index] = new_loc
|
locations[index] = new_loc
|
||||||
|
print (new_loc)
|
||||||
elif self.type == "playlist":
|
elif self.type == "playlist":
|
||||||
for index, loc in enumerate(locations):
|
for index, loc in enumerate(locations):
|
||||||
new_loc = ""
|
new_loc = ""
|
||||||
|
@ -80,79 +92,79 @@ class Ripper:
|
||||||
loc = loc.replace("//", "/")
|
loc = loc.replace("//", "/")
|
||||||
new_loc = (new_loc + "/" + file_name).replace("//", "/")
|
new_loc = (new_loc + "/" + file_name).replace("//", "/")
|
||||||
os.rename(loc, new_loc)
|
os.rename(loc, new_loc)
|
||||||
|
|
||||||
return locations
|
return locations
|
||||||
|
|
||||||
def find_yt_url(self, song=None, artist=None, additional_search="lyrics"):
|
def find_yt_url(self, song=None, artist=None, additional_search=None):
|
||||||
|
if additional_search == None:
|
||||||
|
additional_search = parse_search_terms(self)
|
||||||
try:
|
try:
|
||||||
if not song: song = self.args["song_title"]
|
if not song: song = self.args["song_title"]
|
||||||
if not artist: artist = self.args["artist"]
|
if not artist: artist = self.args["artist"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
||||||
|
|
||||||
clear_line()
|
print ("Finding Youtube Link ...")
|
||||||
sys.stdout.write("Finding Youtube Link ...\r")
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
search_terms = song + " " + artist + " " + additional_search
|
search_terms = song + " " + artist + " " + additional_search
|
||||||
query_string = urlencode({"search_query" : (search_terms)})
|
query_string = urlencode({"search_query" : (search_terms)})
|
||||||
link = "http://www.youtube.com/results?" + query_string
|
link = "http://www.youtube.com/results?" + query_string
|
||||||
|
|
||||||
html_content = urlopen(link).read()
|
html_content = urlopen(link).read()
|
||||||
soup = BeautifulSoup(html_content, 'html.parser')#.prettify
|
soup = BeautifulSoup(html_content, 'html.parser')#.prettify
|
||||||
|
|
||||||
def find_link(link):
|
def find_link(link):
|
||||||
try:
|
try:
|
||||||
if "yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link" in str(" ".join(link["class"])):
|
if "yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link" in str(" ".join(link["class"])):
|
||||||
if not "&list=" in link["href"]:
|
if not "&list=" in link["href"]:
|
||||||
return link
|
return link
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
results = list(filter(None, (map(find_link, soup.find_all("a")))))
|
results = list(filter(None, (map(find_link, soup.find_all("a")))))
|
||||||
|
|
||||||
garbage_phrases = "cover album live clean rare version".split(" ")
|
garbage_phrases = "cover album live clean rare version".split(" ")
|
||||||
|
|
||||||
self.code = None
|
self.code = None
|
||||||
for link in results:
|
for link in results:
|
||||||
if blank_include(link["title"], song) and blank_include(link["title"], artist):
|
if blank_include(link["title"], song) and blank_include(link["title"], artist):
|
||||||
if check_garbage_phrases: continue
|
if check_garbage_phrases: continue
|
||||||
self.code = link
|
self.code = link
|
||||||
break
|
break
|
||||||
|
|
||||||
if self.code == None:
|
if self.code == None:
|
||||||
for link in results:
|
for link in results:
|
||||||
if check_garbage_phrases: continue
|
if check_garbage_phrases: continue
|
||||||
if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist):
|
if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist):
|
||||||
self.code = link
|
self.code = link
|
||||||
break
|
break
|
||||||
|
|
||||||
if self.code == None:
|
if self.code == None:
|
||||||
if additional_search == "lyrics":
|
if additional_search == "lyrics":
|
||||||
return self.find_yt_url(song, artist, "")
|
return self.find_yt_url(song, artist, "")
|
||||||
else:
|
else:
|
||||||
self.code = results[0]
|
self.code = results[0]
|
||||||
|
|
||||||
return ("https://youtube.com" + self.code["href"], self.code["title"])
|
return ("https://youtube.com" + self.code["href"], self.code["title"])
|
||||||
|
|
||||||
def album(self, title): # Alias for `spotify_list("album", ...)`
|
def album(self, title): # Alias for `spotify_list("album", ...)`
|
||||||
return self.spotify_list("album", title)
|
return self.spotify_list("album", title)
|
||||||
|
|
||||||
def playlist(self, title, username): # Alias for `spotify_list("playlist", ...)`
|
def playlist(self, title, username): # Alias for `spotify_list("playlist", ...)`
|
||||||
return self.spotify_list("playlist", title, username)
|
return self.spotify_list("playlist", title, username)
|
||||||
|
|
||||||
def spotify_list(self, type=None, title=None, username=None):
|
def spotify_list(self, type=None, title=None, username=None):
|
||||||
try:
|
try:
|
||||||
if not type: type = self.args["type"]
|
if not type: type = self.args["type"]
|
||||||
if not title: title = self.args["list_title"]
|
if not title: title = self.args["list_title"]
|
||||||
if not username and type == "playlist":
|
if not username and type == "playlist":
|
||||||
username = self.args["username"]
|
username = self.args["username"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError("Must specify type/title/username in `args` with init, or in method arguments.")
|
raise ValueError("Must specify type/title/username in `args` with init, or in method arguments.")
|
||||||
|
|
||||||
if not self.type:
|
if not self.type:
|
||||||
self.type = type
|
self.type = type
|
||||||
|
|
||||||
if type == "album":
|
if type == "album":
|
||||||
search = title
|
search = title
|
||||||
if "artist" in self.args:
|
if "artist" in self.args:
|
||||||
|
@ -160,7 +172,7 @@ class Ripper:
|
||||||
list_of_lists = self.spotify.search(q=search, type="album")["albums"]["items"]
|
list_of_lists = self.spotify.search(q=search, type="album")["albums"]["items"]
|
||||||
elif type == "playlist":
|
elif type == "playlist":
|
||||||
list_of_lists = self.spotify.user_playlists(username)["items"]
|
list_of_lists = self.spotify.user_playlists(username)["items"]
|
||||||
|
|
||||||
if len(list_of_lists) > 0:
|
if len(list_of_lists) > 0:
|
||||||
the_list = None
|
the_list = None
|
||||||
for list_ in list_of_lists:
|
for list_ in list_of_lists:
|
||||||
|
@ -178,8 +190,7 @@ class Ripper:
|
||||||
break
|
break
|
||||||
if the_list != None:
|
if the_list != None:
|
||||||
clear_line()
|
clear_line()
|
||||||
sys.stdout.write(type.title() + ': "%s" by "%s"\r' % (the_list["name"], the_list["artists"][0]["name"]))
|
print (type.title() + ': "%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
||||||
sys.stdout.flush()
|
|
||||||
compilation = ""
|
compilation = ""
|
||||||
if type == "album":
|
if type == "album":
|
||||||
tmp_artists = []
|
tmp_artists = []
|
||||||
|
@ -188,14 +199,14 @@ class Ripper:
|
||||||
tmp_artists = list(set(tmp_artists))
|
tmp_artists = list(set(tmp_artists))
|
||||||
if len(tmp_artists) > 1:
|
if len(tmp_artists) > 1:
|
||||||
compilation = "1"
|
compilation = "1"
|
||||||
|
|
||||||
tracks = []
|
tracks = []
|
||||||
file_prefix = ""
|
file_prefix = ""
|
||||||
|
|
||||||
for track in the_list["tracks"]["items"]:
|
for track in the_list["tracks"]["items"]:
|
||||||
if type == "playlist":
|
if type == "playlist":
|
||||||
self.playlist_title = the_list["name"] # For post-processors
|
self.playlist_title = the_list["name"] # For post-processors
|
||||||
|
|
||||||
file_prefix = str(len(tracks) + 1) + " - "
|
file_prefix = str(len(tracks) + 1) + " - "
|
||||||
track = track["track"]
|
track = track["track"]
|
||||||
album = self.spotify.album(track["album"]["uri"])
|
album = self.spotify.album(track["album"]["uri"])
|
||||||
|
@ -215,13 +226,13 @@ class Ripper:
|
||||||
"compilation": compilation,
|
"compilation": compilation,
|
||||||
"file_prefix": file_prefix,
|
"file_prefix": file_prefix,
|
||||||
}
|
}
|
||||||
|
|
||||||
tracks.append(data)
|
tracks.append(data)
|
||||||
|
|
||||||
locations = self.list(tracks)
|
locations = self.list(tracks)
|
||||||
return locations
|
return locations
|
||||||
#return self.post_processing(locations)
|
#return self.post_processing(locations)
|
||||||
|
|
||||||
print ('Could not find any lists.')
|
print ('Could not find any lists.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -229,27 +240,27 @@ class Ripper:
|
||||||
locations = []
|
locations = []
|
||||||
#with open(".irs-download-log", "w+") as file:
|
#with open(".irs-download-log", "w+") as file:
|
||||||
# file.write(format_download_log_data(list_data))
|
# file.write(format_download_log_data(list_data))
|
||||||
|
|
||||||
for track in list_data:
|
for track in list_data:
|
||||||
loc = self.song(track["name"], track["artist"], track)
|
loc = self.song(track["name"], track["artist"], track)
|
||||||
|
|
||||||
if loc != False:
|
if loc != False:
|
||||||
#update_download_log_line_status(track, "downloaded")
|
#update_download_log_line_status(track, "downloaded")
|
||||||
locations.append(loc)
|
locations.append(loc)
|
||||||
|
|
||||||
if self.type in ("album", "playlist"):
|
if self.type in ("album", "playlist"):
|
||||||
return self.post_processing(locations)
|
return self.post_processing(locations)
|
||||||
|
|
||||||
#os.remove(".irs-download-log")
|
#os.remove(".irs-download-log")
|
||||||
return locations
|
return locations
|
||||||
|
|
||||||
def parse_song_data(self, song, artist):
|
def parse_song_data(self, song, artist):
|
||||||
album, track = find_album_and_track(song, artist)
|
album, track = find_album_and_track(song, artist)
|
||||||
if album == False:
|
if album == False:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
album = self.spotify.album(album["uri"])
|
album = self.spotify.album(album["uri"])
|
||||||
track = self.spotify.track(track["uri"])
|
track = self.spotify.track(track["uri"])
|
||||||
genre = self.spotify.artist(album["artists"][0]["uri"])["genres"]
|
genre = self.spotify.artist(album["artists"][0]["uri"])["genres"]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -260,37 +271,35 @@ class Ripper:
|
||||||
"genre": parse_genre(genre),
|
"genre": parse_genre(genre),
|
||||||
"track_number": track["track_number"],
|
"track_number": track["track_number"],
|
||||||
"disc_number": track["disc_number"],
|
"disc_number": track["disc_number"],
|
||||||
|
|
||||||
"compilation": "", # If this method is being called, it's not a compilation
|
"compilation": "", # If this method is being called, it's not a compilation
|
||||||
"file_prefix": "" # And therefore, won't have a prefix
|
"file_prefix": "" # And therefore, won't have a prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
def song(self, song, artist, data={}): # Takes data from `parse_song_data`
|
def song(self, song, artist, data={}): # Takes data from `parse_song_data`
|
||||||
if not self.type:
|
if not self.type:
|
||||||
self.type = "song"
|
self.type = "song"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not song: song = self.args["song_title"]
|
if not song: song = self.args["song_title"]
|
||||||
if not artist: artist = self.args["artist"]
|
if not artist: artist = self.args["artist"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
||||||
|
|
||||||
if data == {}:
|
if data == {}:
|
||||||
data = self.parse_song_data(song, artist)
|
data = self.parse_song_data(song, artist)
|
||||||
song = data["name"]
|
song = data["name"]
|
||||||
artist = data["artist"]
|
artist = data["artist"]
|
||||||
|
|
||||||
if "file_prefix" not in data:
|
if "file_prefix" not in data:
|
||||||
data["file_prefix"] = ""
|
data["file_prefix"] = ""
|
||||||
|
|
||||||
video_url, video_title = self.find_yt_url(song, artist)
|
video_url, video_title = self.find_yt_url(song, artist)
|
||||||
|
|
||||||
clear_line()
|
print ('Downloading "%s" by "%s" ...' % (song, artist))
|
||||||
sys.stdout.write('Downloading "%s" by "%s" ...\r' % (song, artist))
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
file_name = str(data["file_prefix"] + blank(song, False) + ".mp3")
|
file_name = str(data["file_prefix"] + blank(song, False) + ".mp3")
|
||||||
|
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
#'quiet': True,
|
#'quiet': True,
|
||||||
|
@ -304,22 +313,22 @@ class Ripper:
|
||||||
'output': "tmp_file",
|
'output': "tmp_file",
|
||||||
'prefer-ffmpeg': True,
|
'prefer-ffmpeg': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
||||||
ydl.download([video_url])
|
ydl.download([video_url])
|
||||||
|
|
||||||
|
|
||||||
for file in glob.glob("./*%s*" % video_url.split("/watch?v=")[-1]):
|
for file in glob.glob("./*%s*" % video_url.split("/watch?v=")[-1]):
|
||||||
os.rename(file, file_name)
|
os.rename(file, file_name)
|
||||||
|
|
||||||
# Ease of Variables (copyright) (patent pending) (git yer filthy hands off)
|
# Ease of Variables (copyright) (patent pending) (git yer filthy hands off)
|
||||||
# [CENSORED BY THE BAD CODE ACT]
|
# [CENSORED BY THE BAD CODE ACT]
|
||||||
# *5 Minutes Later*
|
# *5 Minutes Later*
|
||||||
# Deprecated. It won't be the next big thing. :(
|
# Deprecated. It won't be the next big thing. :(
|
||||||
|
|
||||||
|
|
||||||
m = Metadata(file_name)
|
m = Metadata(file_name)
|
||||||
|
|
||||||
m.add_tag( "title", data["name"])
|
m.add_tag( "title", data["name"])
|
||||||
m.add_tag( "artist", data["artist"])
|
m.add_tag( "artist", data["artist"])
|
||||||
if data != {}:
|
if data != {}:
|
||||||
|
@ -329,8 +338,8 @@ class Ripper:
|
||||||
m.add_tag("discnumber", str(data["disc_number"]))
|
m.add_tag("discnumber", str(data["disc_number"]))
|
||||||
m.add_tag("compilation", data["compilation"])
|
m.add_tag("compilation", data["compilation"])
|
||||||
m.add_album_art( str(data["album_art"]))
|
m.add_album_art( str(data["album_art"]))
|
||||||
|
|
||||||
if self.type == "song":
|
if self.type == "song":
|
||||||
return self.post_processing([file_name])
|
return self.post_processing([file_name])
|
||||||
|
|
||||||
return file_name
|
return file_name
|
109
irs/utils.py
109
irs/utils.py
|
@ -21,9 +21,7 @@ class MyLogger(object):
|
||||||
|
|
||||||
def my_hook(d):
|
def my_hook(d):
|
||||||
if d['status'] == 'finished':
|
if d['status'] == 'finished':
|
||||||
clear_line() # TODO: Make this into a method.
|
print ("Converting to mp3 ...")
|
||||||
sys.stdout.write("Converting to mp3 ...\r")
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
|
|
||||||
#=================================
|
#=================================
|
||||||
|
@ -36,14 +34,14 @@ def check_garbage_phrases(phrases, string, title):
|
||||||
if not phrase in blank(title):
|
if not phrase in blank(title):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def blank(string, downcase=True):
|
def blank(string, downcase=True):
|
||||||
import re
|
import re
|
||||||
regex = re.compile('[^a-zA-Z0-9\ ]')
|
regex = re.compile('[^a-zA-Z0-9\ ]')
|
||||||
string = regex.sub('', string)
|
string = regex.sub('', string)
|
||||||
if downcase: string = string.lower()
|
if downcase: string = string.lower()
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def blank_include(this, includes_this):
|
def blank_include(this, includes_this):
|
||||||
this = blank(this)
|
this = blank(this)
|
||||||
includes_this = blank(includes_this)
|
includes_this = blank(includes_this)
|
||||||
|
@ -77,13 +75,13 @@ def remove_none_values(d):
|
||||||
if type(new_d[x]) is list:
|
if type(new_d[x]) is list:
|
||||||
new_d[x] = remove_none_values(d[x])
|
new_d[x] = remove_none_values(d[x])
|
||||||
elif new_d[x] == None:
|
elif new_d[x] == None:
|
||||||
del new_d[x]
|
del new_d[x]
|
||||||
return new_d
|
return new_d
|
||||||
|
|
||||||
#=========================================
|
#=========================================
|
||||||
# Download Log Reading/Updating/Formatting
|
# Download Log Reading/Updating/Formatting
|
||||||
#=========================================
|
#=========================================
|
||||||
|
|
||||||
def format_download_log_line(t, download_status="not downloaded"):
|
def format_download_log_line(t, download_status="not downloaded"):
|
||||||
return (" @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
return (" @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
||||||
str(t["genre"]), t["track_number"], t["disc_number"], t["compilation"], \
|
str(t["genre"]), t["track_number"], t["disc_number"], t["compilation"], \
|
||||||
|
@ -94,7 +92,7 @@ def format_download_log_data(data):
|
||||||
for track in data:
|
for track in data:
|
||||||
lines.append(format_download_log_line(track))
|
lines.append(format_download_log_line(track))
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
def read_download_log(spotify):
|
def read_download_log(spotify):
|
||||||
data = []
|
data = []
|
||||||
with open(".irs-download-log", "r") as file:
|
with open(".irs-download-log", "r") as file:
|
||||||
|
@ -111,7 +109,7 @@ def read_download_log(spotify):
|
||||||
"file_prefix": line[7],
|
"file_prefix": line[7],
|
||||||
})
|
})
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def update_download_log_line_status(track, status="downloaded"):
|
def update_download_log_line_status(track, status="downloaded"):
|
||||||
line_to_find = format_download_log_line(track)
|
line_to_find = format_download_log_line(track)
|
||||||
with open(".irs-download-log", "r") as input_file, \
|
with open(".irs-download-log", "r") as input_file, \
|
||||||
|
@ -121,8 +119,8 @@ def update_download_log_line_status(track, status="downloaded"):
|
||||||
output_file.write(format_download_log_line(track, status))
|
output_file.write(format_download_log_line(track, status))
|
||||||
else:
|
else:
|
||||||
output_file.write(line)
|
output_file.write(line)
|
||||||
|
|
||||||
|
|
||||||
#============================================
|
#============================================
|
||||||
# And Now, For Something Completely Different
|
# And Now, For Something Completely Different
|
||||||
#============================================
|
#============================================
|
||||||
|
@ -137,39 +135,39 @@ COLS = int(os.popen('tput cols').read().strip("\n"))
|
||||||
if sys.version_info[0] == 2:
|
if sys.version_info[0] == 2:
|
||||||
def input(string):
|
def input(string):
|
||||||
return raw_input(string)
|
return raw_input(string)
|
||||||
|
|
||||||
|
|
||||||
def code(code1):
|
def code(code1):
|
||||||
return "\x1b[%sm" % str(code1)
|
return "\x1b[%sm" % str(code1)
|
||||||
|
|
||||||
def no_colors(string):
|
def no_colors(string):
|
||||||
return re.sub("\x1b\[\d+m", "", string)
|
return re.sub("\x1b\[\d+m", "", string)
|
||||||
|
|
||||||
def center_colors(string, cols):
|
def center_colors(string, cols):
|
||||||
return no_colors(string).center(cols).replace(no_colors(string), string)
|
return no_colors(string).center(cols).replace(no_colors(string), string)
|
||||||
|
|
||||||
def decode_utf8(string):
|
def decode_utf8(string):
|
||||||
if sys.version_info[0] == 3:
|
if sys.version_info[0] == 3:
|
||||||
return string.encode("utf8", "strict").decode()
|
return string.encode("utf8", "strict").decode()
|
||||||
elif sys.version_info[0] == 2:
|
elif sys.version_info[0] == 2:
|
||||||
return string.decode("utf8")
|
return string.decode("utf8")
|
||||||
|
|
||||||
def center_unicode(string, cols):
|
def center_unicode(string, cols):
|
||||||
tmp_chars = "X" * len(decode_utf8(string))
|
tmp_chars = "X" * len(decode_utf8(string))
|
||||||
chars = center_colors(tmp_chars, cols)
|
chars = center_colors(tmp_chars, cols)
|
||||||
return chars.replace(tmp_chars, string)
|
return chars.replace(tmp_chars, string)
|
||||||
|
|
||||||
def center_lines(string, cols, end="\n"):
|
def center_lines(string, cols, end="\n"):
|
||||||
lines = []
|
lines = []
|
||||||
for line in string.split("\n"):
|
for line in string.split("\n"):
|
||||||
lines.append(center_unicode(line, cols))
|
lines.append(center_unicode(line, cols))
|
||||||
return end.join(lines)
|
return end.join(lines)
|
||||||
|
|
||||||
def flush_puts(msg, time=0.01):
|
def flush_puts(msg, time=0.01):
|
||||||
# For slow *burrrp* scroll text, Morty. They-They just love it, Morty.
|
# For slow *burrrp* scroll text, Morty. They-They just love it, Morty.
|
||||||
# When they see this text. Just slowwwly extending across the page. Mmm, mmm.
|
# When they see this text. Just slowwwly extending across the page. Mmm, mmm.
|
||||||
# You just give the time for how *buurp* slow you wa-want it, Morty.
|
# You just give the time for how *buurp* slow you wa-want it, Morty.
|
||||||
# It works with colors and escape characters too, Morty.
|
# It works with colors and escape characters too, Morty.
|
||||||
# Your grandpa's a genius *burrrp* Morty
|
# Your grandpa's a genius *burrrp* Morty
|
||||||
pattern = re.compile("(\x1b\[\d+m)")
|
pattern = re.compile("(\x1b\[\d+m)")
|
||||||
def check_color(s):
|
def check_color(s):
|
||||||
|
@ -187,7 +185,7 @@ def flush_puts(msg, time=0.01):
|
||||||
sys.stdout.write(char)
|
sys.stdout.write(char)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
print ("")
|
print ("")
|
||||||
|
|
||||||
|
|
||||||
BOLD = code(1)
|
BOLD = code(1)
|
||||||
END = code(0)
|
END = code(0)
|
||||||
|
@ -225,10 +223,10 @@ def banner():
|
||||||
sleep(0.3)
|
sleep(0.3)
|
||||||
flush_puts(center_colors("{0}Ironic Redistribution System ({1}IRS{2})"\
|
flush_puts(center_colors("{0}Ironic Redistribution System ({1}IRS{2})"\
|
||||||
.format(BYELLOW, BRED, BYELLOW), COLS))
|
.format(BYELLOW, BRED, BYELLOW), COLS))
|
||||||
|
|
||||||
flush_puts(center_colors("{0}Made with 😈 by: {1}Kepoor Hampond ({2}kepoorhampond{3})"\
|
flush_puts(center_colors("{0}Made with 😈 by: {1}Kepoor Hampond ({2}kepoorhampond{3})"\
|
||||||
.format(BBLUE, BYELLOW, BRED, BYELLOW) + END, COLS))
|
.format(BBLUE, BYELLOW, BRED, BYELLOW) + END, COLS))
|
||||||
|
|
||||||
flush_puts(center_colors("{0}Version: {1}".format(BBLUE, BYELLOW) + pkg_resources.get_distribution("irs").version, COLS))
|
flush_puts(center_colors("{0}Version: {1}".format(BBLUE, BYELLOW) + pkg_resources.get_distribution("irs").version, COLS))
|
||||||
|
|
||||||
def menu(unicode, time=0.01):
|
def menu(unicode, time=0.01):
|
||||||
|
@ -277,6 +275,67 @@ def console(ripper):
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print ("")
|
print ("")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
#======================
|
||||||
|
# Config File and Flags
|
||||||
|
#======================
|
||||||
|
from .config import CONFIG
|
||||||
|
|
||||||
|
def check_sources(ripper, key, default=None, environment=False, where=None):
|
||||||
|
if where != None:
|
||||||
|
tmp_args = ripper.args.get(where)
|
||||||
|
else:
|
||||||
|
tmp_args = ripper.args
|
||||||
|
|
||||||
|
if tmp_args.get(key):
|
||||||
|
return tmp_args.get(key)
|
||||||
|
#============
|
||||||
|
# CONFIG FILE
|
||||||
|
#============
|
||||||
|
from .config import CONFIG
|
||||||
|
|
||||||
|
def check_sources(ripper, key, default=None, environment=False):
|
||||||
|
if ripper.args.get(key):
|
||||||
|
return ripper.args.get(key)
|
||||||
|
elif CONFIG.get(key):
|
||||||
|
return CONFIG.get(key)
|
||||||
|
elif os.environ.get(key) and environment == True:
|
||||||
|
return os.environ.get(key)
|
||||||
|
else:
|
||||||
|
return default
|
||||||
|
|
||||||
|
def parse_spotify_creds(ripper):
|
||||||
|
CLIENT_ID = check_sources(ripper, "SPOTIFY_CLIENT_ID", environment=True)
|
||||||
|
CLIENT_SECRET = check_sources(ripper, "SPOTIFY_CLIENT_SECRET", environment=True)
|
||||||
|
return CLIENT_ID, CLIENT_SECRET
|
||||||
|
|
||||||
|
def parse_search_terms(ripper):
|
||||||
|
search_terms = check_sources(ripper, "additional_search_terms", "lyrics")
|
||||||
|
return search_terms
|
||||||
|
|
||||||
|
def parse_directory(ripper):
|
||||||
|
directory = check_sources(ripper, "custom_directory", where="post_processors")
|
||||||
|
if directory == None:
|
||||||
|
directory = check_sources(ripper, "custom_directory", "~/Music")
|
||||||
|
return directory.replace("~", os.path.expanduser("~"))
|
||||||
|
|
||||||
|
def parse_default_flags(default=""):
|
||||||
|
if CONFIG.get("default_flags"):
|
||||||
|
args = sys.argv[1:] + CONFIG.get("default_flags")
|
||||||
|
else:
|
||||||
|
args = default
|
||||||
|
return args
|
||||||
|
|
||||||
|
def parse_organize(ripper):
|
||||||
|
organize = check_sources(ripper, "organize")
|
||||||
|
if organize == None:
|
||||||
|
return check_sources(ripper, "organize", False, where="post_processors")
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def parse_search_terms(ripper):
|
||||||
|
search_terms = check_sources(ripper, "additional_search_terms", "lyrics")
|
||||||
|
return search_terms
|
||||||
|
|
|
@ -7,14 +7,14 @@ if not os.path.exists("test_dir"):
|
||||||
os.makedirs("test_dir")
|
os.makedirs("test_dir")
|
||||||
Ripper({
|
Ripper({
|
||||||
"post_processors": {
|
"post_processors": {
|
||||||
"location": "test_dir/",
|
"custom_directory": "test_dir/",
|
||||||
"organize": True,
|
"organize": True,
|
||||||
}
|
}
|
||||||
}).album("Da Frame 2R / Matador")
|
}).album("Da Frame 2R / Matador")
|
||||||
|
|
||||||
Ripper({
|
Ripper({
|
||||||
"post_processors": {
|
"post_processors": {
|
||||||
"location": "test_dir/",
|
"custom_directory": "test_dir/",
|
||||||
"organize": True,
|
"organize": True,
|
||||||
}
|
}
|
||||||
}).playlist("IRS Testing", "prakkillian")
|
}).playlist("IRS Testing", "prakkillian")
|
||||||
|
|
Loading…
Reference in a new issue