mirror of
https://github.com/cooperhammond/irs.git
synced 2025-01-17 21:17:07 +00:00
Follows pep8 guides with more organization
This commit is contained in:
parent
04909589cf
commit
09a839aeab
32
irs/cli.py
32
irs/cli.py
|
@ -8,35 +8,42 @@ import os
|
|||
# Powered by:
|
||||
from .ripper import Ripper
|
||||
from .utils import console, parse_default_flags
|
||||
from .config import CONFIG
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
# Single Song
|
||||
parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song or -A/--album")
|
||||
parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist")
|
||||
parser.add_argument("-a", "--artist", dest="artist", help="Specify artist \
|
||||
name. Must be used with -s/--song or -A/--album")
|
||||
parser.add_argument("-s", "--song", dest="song", help="Specify song name.\
|
||||
Must be used with -a/--artist")
|
||||
|
||||
# Album
|
||||
parser.add_argument("-A", "--album", dest="album", help="Specify album name")
|
||||
parser.add_argument("-A", "--album", dest="album", help="Specify album \
|
||||
name")
|
||||
|
||||
# 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("-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")
|
||||
|
||||
# Post-Processors
|
||||
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("-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.")
|
||||
|
||||
# Config
|
||||
parser.add_argument("-c", "--config", dest="config", action="store_true", help="Display path to config file.")
|
||||
|
||||
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")
|
||||
print(os.path.dirname(irs.__file__) + "/config.py")
|
||||
sys.exit()
|
||||
|
||||
ripper_args = {
|
||||
|
@ -62,5 +69,6 @@ def main():
|
|||
else:
|
||||
console(ripper)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -16,7 +16,8 @@ CONFIG = dict(
|
|||
organize = True,
|
||||
# True always forces organization.
|
||||
# False always forces non-organization.
|
||||
# None allows options and flags to determine if the files will be organized.
|
||||
# None allows options and flags to determine if the files
|
||||
# will be organized.
|
||||
|
||||
custom_directory = "",
|
||||
# Defaults to '~/Music'
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
# MP3 Metadata editing
|
||||
from mutagen.mp3 import MP3, EasyMP3
|
||||
from mutagen.easyid3 import EasyID3, EasyID3KeyError
|
||||
from mutagen.id3 import * # There's A LOT of stuff to import, forgive me.
|
||||
from mutagen.id3 import APIC, ID3, COMM
|
||||
from mutagen.mp3 import EasyMP3
|
||||
from mutagen.easyid3 import EasyID3
|
||||
from mutagen.id3 import * # There's A LOT of stuff to import, forgive me.
|
||||
from mutagen.id3 import APIC, ID3
|
||||
|
||||
# System
|
||||
import sys
|
||||
|
||||
# Powered by...
|
||||
import spotipy
|
||||
|
||||
# Local utils
|
||||
from .utils import ObjManip
|
||||
om = ObjManip
|
||||
|
||||
# Info finding
|
||||
if sys.version_info[0] >= 3:
|
||||
from urllib.parse import quote_plus, quote
|
||||
|
@ -16,16 +23,6 @@ elif sys.version_info[0] < 3:
|
|||
from urllib import urlopen
|
||||
from urllib2 import Request
|
||||
|
||||
# Info parsing
|
||||
import json
|
||||
from re import match
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
# Local utils
|
||||
from .utils import *
|
||||
|
||||
# Powered by...
|
||||
import spotipy
|
||||
|
||||
class Metadata:
|
||||
def __init__(self, location):
|
||||
|
@ -50,22 +47,25 @@ class Metadata:
|
|||
mp3.tags.add(
|
||||
APIC(
|
||||
encoding = 3,
|
||||
mime = 'image/png',
|
||||
type = 3,
|
||||
desc = 'cover',
|
||||
data = urlopen(image_url).read()
|
||||
mime = 'image/png',
|
||||
type = 3,
|
||||
desc = 'cover',
|
||||
data = urlopen(image_url).read()
|
||||
)
|
||||
)
|
||||
mp3.save()
|
||||
|
||||
|
||||
def find_album_and_track(song, artist):
|
||||
tracks = spotipy.Spotify().search(q=song, type="track")["tracks"]["items"]
|
||||
tracks = spotipy.Spotify().search(q=song, type="track"
|
||||
)["tracks"]["items"]
|
||||
for track in tracks:
|
||||
if blank_include(track["name"], song):
|
||||
if blank_include(track["artists"][0]["name"], artist):
|
||||
if om.blank_include(track["name"], song):
|
||||
if om.blank_include(track["artists"][0]["name"], artist):
|
||||
return track["album"], track
|
||||
return False, False
|
||||
|
||||
|
||||
def parse_genre(genres):
|
||||
if genres != []:
|
||||
genres.reverse()
|
||||
|
@ -73,4 +73,4 @@ def parse_genre(genres):
|
|||
genres.sort(key=lambda x: len(x.split()))
|
||||
return genres[0].title()
|
||||
else:
|
||||
return ""
|
||||
return ""
|
||||
|
|
204
irs/ripper.py
204
irs/ripper.py
|
@ -8,6 +8,11 @@ import sys
|
|||
import os
|
||||
import glob
|
||||
|
||||
# Local utilities
|
||||
from .utils import YdlUtils, ObjManip, Config
|
||||
from .metadata import Metadata
|
||||
from .metadata import find_album_and_track, parse_genre
|
||||
|
||||
# Parsing
|
||||
from bs4 import BeautifulSoup
|
||||
if sys.version_info[0] >= 3:
|
||||
|
@ -17,12 +22,9 @@ elif sys.version_info[0] < 3:
|
|||
from urllib import urlencode
|
||||
from urllib import urlopen
|
||||
else:
|
||||
print ("Must be using Python 2 or 3")
|
||||
print("Must be using Python 2 or 3")
|
||||
sys.exit(1)
|
||||
|
||||
# Local utilities
|
||||
from .utils import *
|
||||
from .metadata import *
|
||||
|
||||
class Ripper:
|
||||
def __init__(self, args={}):
|
||||
|
@ -30,9 +32,15 @@ class Ripper:
|
|||
self.locations = []
|
||||
self.type = None
|
||||
try:
|
||||
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)
|
||||
CLIENT_ID, CLIENT_SECRET = Config.parse_spotify_creds(self)
|
||||
client_credentials_manager = SpotifyClientCredentials(CLIENT_ID,
|
||||
CLIENT_SECRET
|
||||
# Stupid lint
|
||||
)
|
||||
|
||||
self.spotify = spotipy.Spotify(
|
||||
client_credentials_manager=client_credentials_manager)
|
||||
|
||||
self.authorized = True
|
||||
except Exception as e:
|
||||
self.spotify = spotipy.Spotify()
|
||||
|
@ -40,12 +48,13 @@ class Ripper:
|
|||
|
||||
def post_processing(self, locations):
|
||||
post_processors = self.args.get("post_processors")
|
||||
directory_option = Config.parse_directory(self)
|
||||
if post_processors:
|
||||
if parse_directory(self) != None:
|
||||
if directory_option is not None:
|
||||
for index, loc in enumerate(locations):
|
||||
new_file_name = parse_directory(self) + "/" + loc
|
||||
if not os.path.exists(parse_directory(self)):
|
||||
os.makedirs(parse_directory(self))
|
||||
new_file_name = directory_option + "/" + loc
|
||||
if not os.path.exists(directory_option):
|
||||
os.makedirs(directory_option)
|
||||
os.rename(loc, new_file_name)
|
||||
locations[index] = new_file_name
|
||||
# I'd just go on believing that code this terrible doesn't exist.
|
||||
|
@ -55,10 +64,11 @@ class Ripper:
|
|||
# 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.
|
||||
# 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 Config.parse_organize(self):
|
||||
if self.type in ("album", "song"):
|
||||
for index, loc in enumerate(locations):
|
||||
mp3 = Metadata(loc)
|
||||
|
@ -68,10 +78,11 @@ class Ripper:
|
|||
file_name = loc.split("/")[-1]
|
||||
else:
|
||||
file_name = loc
|
||||
artist, album = mp3.read_tag("artist")[0], mp3.read_tag("album")
|
||||
new_loc += blank(artist, False)
|
||||
artist, album = mp3.read_tag("artist")[0]
|
||||
album = mp3.read_tag("album")
|
||||
new_loc += ObjManip.blank(artist, False)
|
||||
if album != []:
|
||||
new_loc += "/" + blank(album[0], False)
|
||||
new_loc += "/" + ObjManip.blank(album[0], False)
|
||||
if not os.path.exists(new_loc):
|
||||
os.makedirs(new_loc)
|
||||
new_loc += "/" + file_name
|
||||
|
@ -87,68 +98,80 @@ class Ripper:
|
|||
file_name = loc.split("/")[-1]
|
||||
else:
|
||||
file_name = loc
|
||||
new_loc += blank(self.playlist_title, False)
|
||||
new_loc += ObjManip.blank(self.playlist_title, False)
|
||||
if not os.path.exists(new_loc):
|
||||
os.makedirs(new_loc)
|
||||
loc = loc.replace("//", "/")
|
||||
new_loc = (new_loc + "/" + file_name).replace("//", "/")
|
||||
new_loc = (new_loc + "/" + file_name)\
|
||||
.replace("//", "/")
|
||||
os.rename(loc, new_loc)
|
||||
|
||||
return locations
|
||||
|
||||
def find_yt_url(self, song=None, artist=None, additional_search=None):
|
||||
if additional_search == None:
|
||||
additional_search = parse_search_terms(self)
|
||||
if additional_search is None:
|
||||
additional_search = Config.parse_search_terms(self)
|
||||
try:
|
||||
if not song: song = self.args["song_title"]
|
||||
if not artist: artist = self.args["artist"]
|
||||
if not song:
|
||||
song = self.args["song_title"]
|
||||
if not artist:
|
||||
artist = self.args["artist"]
|
||||
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.")
|
||||
|
||||
print ("Finding Youtube Link ...")
|
||||
print("Finding Youtube Link ...")
|
||||
|
||||
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
|
||||
|
||||
html_content = urlopen(link).read()
|
||||
soup = BeautifulSoup(html_content, 'html.parser')#.prettify()
|
||||
soup = BeautifulSoup(html_content, 'html.parser') # .prettify()
|
||||
|
||||
def find_link(link):
|
||||
try:
|
||||
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 "yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-\
|
||||
sessionlink spf-link" in str(" ".join(link["class"])):
|
||||
if "&list=" not in link["href"]:
|
||||
return link
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
results = list(filter(None, (map(find_link, soup.find_all("a")))))
|
||||
|
||||
garbage_phrases = "cover album live clean rare version full full album".split(" ")
|
||||
garbage_phrases = "cover album live clean rare version full full \
|
||||
album".split(" ")
|
||||
|
||||
self.code = None
|
||||
counter = 0
|
||||
|
||||
|
||||
while self.code == None and counter <= 10:
|
||||
while self.code is None and counter <= 10:
|
||||
counter += 1
|
||||
for link in results:
|
||||
if blank_include(link["title"], song) and blank_include(link["title"], artist):
|
||||
if check_garbage_phrases(garbage_phrases, link["title"], song): continue
|
||||
if ObjManip.blank_include(link["title"], song) and \
|
||||
ObjManip.blank_include(link["title"], artist):
|
||||
if ObjManip.check_garbage_phrases(garbage_phrases,
|
||||
link["title"], song):
|
||||
continue
|
||||
self.code = link
|
||||
break
|
||||
|
||||
if self.code == None:
|
||||
if self.code is None:
|
||||
for link in results:
|
||||
if check_garbage_phrases(garbage_phrases, link["title"], song): continue
|
||||
if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist):
|
||||
if ObjManip.check_garbage_phrases(garbage_phrases,
|
||||
link["title"], song):
|
||||
continue
|
||||
if ObjManip.individual_word_match(song, link["title"]) \
|
||||
>= 0.8 and ObjManip.blank_include(link["title"],
|
||||
artist):
|
||||
self.code = link
|
||||
break
|
||||
|
||||
if self.code == None:
|
||||
song = limit_song_name(song)
|
||||
if self.code is None:
|
||||
song = ObjManip.limit_song_name(song)
|
||||
|
||||
if self.code == None:
|
||||
if self.code is None:
|
||||
if additional_search == "lyrics":
|
||||
return self.find_yt_url(song, artist, "")
|
||||
else:
|
||||
|
@ -156,20 +179,24 @@ class Ripper:
|
|||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
def spotify_list(self, type=None, title=None, username=None, artist=None):
|
||||
try:
|
||||
if not type: type = self.args["type"]
|
||||
if not title: title = self.args["list_title"]
|
||||
if not type:
|
||||
type = self.args["type"]
|
||||
if not title:
|
||||
title = self.args["list_title"]
|
||||
if not username and type == "playlist":
|
||||
username = self.args["username"]
|
||||
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:
|
||||
self.type = type
|
||||
|
@ -178,28 +205,34 @@ class Ripper:
|
|||
search = title
|
||||
if "artist" in self.args:
|
||||
search += " " + self.args["artist"]
|
||||
list_of_lists = self.spotify.search(q=search, type="album")["albums"]["items"]
|
||||
list_of_lists = self.spotify.search(q=search, type="album")
|
||||
list_of_lists = list_of_lists["albums"]["items"]
|
||||
elif type == "playlist":
|
||||
list_of_lists = self.spotify.user_playlists(username)["items"]
|
||||
|
||||
if len(list_of_lists) > 0:
|
||||
the_list = None
|
||||
for list_ in list_of_lists:
|
||||
if blank_include(list_["name"], title):
|
||||
if parse_artist(self):
|
||||
if blank_include(list_["artists"][0]["name"], parse_artist(self)):
|
||||
if ObjManip.blank_include(list_["name"], title):
|
||||
if Config.parse_artist(self):
|
||||
if ObjManip.blank_include(list_["artists"][0]["name"],
|
||||
ObjManip.arse_artist(self)):
|
||||
the_list = self.spotify.album(list_["uri"])
|
||||
break
|
||||
else:
|
||||
if type == "album":
|
||||
the_list = self.spotify.album(list_["uri"])
|
||||
else:
|
||||
the_list = self.spotify.user_playlist(list_["owner"]["id"], list_["uri"])
|
||||
the_list = self.spotify.user_playlist(
|
||||
list_["owner"]["id"], list_["uri"])
|
||||
the_list["artists"] = [{"name": username}]
|
||||
break
|
||||
if the_list != None:
|
||||
clear_line()
|
||||
print (type.title() + ': "%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
||||
if the_list is not None:
|
||||
YdlUtils.clear_line()
|
||||
|
||||
print(type.title() + ': "%s" by \
|
||||
"%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
||||
|
||||
compilation = ""
|
||||
if type == "album":
|
||||
tmp_artists = []
|
||||
|
@ -215,7 +248,8 @@ class Ripper:
|
|||
|
||||
for track in the_list["tracks"]["items"]:
|
||||
if type == "playlist":
|
||||
self.playlist_title = the_list["name"] # For post-processors
|
||||
# For post-processors
|
||||
self.playlist_title = the_list["name"]
|
||||
|
||||
file_prefix = str(len(tracks) + 1) + " - "
|
||||
track = track["track"]
|
||||
|
@ -229,7 +263,9 @@ class Ripper:
|
|||
"name": track["name"],
|
||||
"artist": track["artists"][0]["name"],
|
||||
"album": album["name"],
|
||||
"genre": parse_genre(self.spotify.artist(track["artists"][0]["uri"])["genres"]),
|
||||
"genre": m.MetadataUtils.parse_genre(
|
||||
self.spotify.artist(track["artists"][0]["uri"]
|
||||
)["genres"]),
|
||||
"track_number": track["track_number"],
|
||||
"disc_number": track["disc_number"],
|
||||
"album_art": album["images"][0]["url"],
|
||||
|
@ -241,32 +277,32 @@ class Ripper:
|
|||
|
||||
locations = self.list(tracks)
|
||||
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
|
||||
|
||||
def list(self, list_data):
|
||||
locations = []
|
||||
#with open(".irs-download-log", "w+") as file:
|
||||
# file.write(format_download_log_data(list_data))
|
||||
# with open(".irs-download-log", "w+") as file:
|
||||
# file.write(format_download_log_data(list_data))
|
||||
|
||||
for track in list_data:
|
||||
loc = self.song(track["name"], track["artist"], track)
|
||||
|
||||
if loc != False:
|
||||
#update_download_log_line_status(track, "downloaded")
|
||||
if loc is not False:
|
||||
# update_download_log_line_status(track, "downloaded")
|
||||
locations.append(loc)
|
||||
|
||||
if self.type in ("album", "playlist"):
|
||||
return self.post_processing(locations)
|
||||
|
||||
#os.remove(".irs-download-log")
|
||||
# os.remove(".irs-download-log")
|
||||
return locations
|
||||
|
||||
def parse_song_data(self, song, artist):
|
||||
album, track = find_album_and_track(song, artist)
|
||||
if album == False:
|
||||
if album is False:
|
||||
return {}
|
||||
|
||||
album = self.spotify.album(album["uri"])
|
||||
|
@ -282,19 +318,26 @@ class Ripper:
|
|||
"track_number": track["track_number"],
|
||||
"disc_number": track["disc_number"],
|
||||
|
||||
"compilation": "", # If this method is being called, it's not a compilation
|
||||
"file_prefix": "" # And therefore, won't have a prefix
|
||||
# If this method is being called, it's not a compilation
|
||||
"compilation": "",
|
||||
# And therefore, won't have a prefix
|
||||
"file_prefix": ""
|
||||
}
|
||||
|
||||
def song(self, song, artist, data={}): # Takes data from `parse_song_data`
|
||||
def song(self, song, artist, data={}):
|
||||
# "data" comes from "self.parse_song_data"'s layout
|
||||
|
||||
if not self.type:
|
||||
self.type = "song"
|
||||
|
||||
try:
|
||||
if not song: song = self.args["song_title"]
|
||||
if not artist: artist = self.args["artist"]
|
||||
if not song:
|
||||
song = self.args["song_title"]
|
||||
if not artist:
|
||||
artist = self.args["artist"]
|
||||
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 == {}:
|
||||
data = self.parse_song_data(song, artist)
|
||||
|
@ -307,20 +350,20 @@ class Ripper:
|
|||
|
||||
video_url, video_title = self.find_yt_url(song, artist)
|
||||
|
||||
print ('Downloading "%s" by "%s" ...' % (song, artist))
|
||||
print('Downloading "%s" by "%s" ...' % (song, artist))
|
||||
|
||||
file_name = str(data["file_prefix"] + blank(song, False) + ".mp3")
|
||||
file_name = str(data["file_prefix"] + ObjManip.blank(song, False) +
|
||||
".mp3")
|
||||
|
||||
ydl_opts = {
|
||||
'format': 'bestaudio/best',
|
||||
#'quiet': True,
|
||||
'postprocessors': [{
|
||||
'key': 'FFmpegExtractAudio',
|
||||
'preferredcodec': 'mp3',
|
||||
'preferredquality': '192',
|
||||
}],
|
||||
'logger': MyLogger(),
|
||||
'progress_hooks': [my_hook],
|
||||
'logger': YdlUtils.MyLogger(),
|
||||
'progress_hooks': [YdlUtils.my_hook],
|
||||
'output': "tmp_file",
|
||||
'prefer-ffmpeg': True,
|
||||
}
|
||||
|
@ -328,19 +371,18 @@ class Ripper:
|
|||
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
||||
ydl.download([video_url])
|
||||
|
||||
|
||||
for file in glob.glob("./*%s*" % video_url.split("/watch?v=")[-1]):
|
||||
os.rename(file, file_name)
|
||||
|
||||
# Ease of Variables (copyright) (patent pending) (git yer filthy hands off)
|
||||
# Ease of Variables (C) (patent pending) (git yer filthy hands off)
|
||||
# [CENSORED BY THE BAD CODE ACT]
|
||||
# *5 Minutes Later*
|
||||
# Deprecated. It won't be the next big thing. :(
|
||||
|
||||
|
||||
m = Metadata(file_name)
|
||||
|
||||
m.add_tag("comment", 'URL: "%s"\nVideo Title: "%s"' % (video_url, video_title))
|
||||
m.add_tag("comment", 'URL: "%s"\nVideo Title: "%s"' %
|
||||
(video_url, video_title))
|
||||
if len(data.keys()) > 1:
|
||||
m.add_tag("title", data["name"])
|
||||
m.add_tag("artist", data["artist"])
|
||||
|
@ -349,13 +391,13 @@ class Ripper:
|
|||
m.add_tag("tracknumber", str(data["track_number"]))
|
||||
m.add_tag("discnumber", str(data["disc_number"]))
|
||||
m.add_tag("compilation", data["compilation"])
|
||||
m.add_album_art( str(data["album_art"]))
|
||||
m.add_album_art(str(data["album_art"]))
|
||||
else:
|
||||
print ("Could not find metadata.")
|
||||
print("Could not find metadata.")
|
||||
m.add_tag("title", song)
|
||||
m.add_tag("artist", artist)
|
||||
|
||||
if self.type == "song":
|
||||
return self.post_processing([file_name])
|
||||
|
||||
return file_name
|
||||
return file_name
|
||||
|
|
442
irs/utils.py
442
irs/utils.py
|
@ -1,144 +1,163 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
|
||||
# =======
|
||||
# Imports
|
||||
# =======
|
||||
|
||||
#==========================
|
||||
# Youtube-DL Logs and Hooks
|
||||
#==========================
|
||||
|
||||
def clear_line():
|
||||
sys.stdout.write("\x1b[2K\r")
|
||||
|
||||
class MyLogger(object):
|
||||
def debug(self, msg):
|
||||
pass
|
||||
|
||||
def warning(self, msg):
|
||||
pass
|
||||
|
||||
def error(self, msg):
|
||||
print(msg)
|
||||
|
||||
|
||||
def my_hook(d):
|
||||
if d['status'] == 'finished':
|
||||
print ("Converting to mp3 ...")
|
||||
|
||||
|
||||
#=================================
|
||||
# Object Manipulation and Checking
|
||||
#=================================
|
||||
|
||||
def limit_song_name(song):
|
||||
bad_phrases = "remaster remastered master".split(" ")
|
||||
# I have "master" here because Spotify actually sometimes mispells stuff
|
||||
# and it is hella annoying, so this was my solution
|
||||
for phrase in bad_phrases:
|
||||
if blank_include(song.split(" - ")[-1], phrase):
|
||||
return song.split(" - ")[0]
|
||||
return song
|
||||
|
||||
def check_garbage_phrases(phrases, string, title):
|
||||
for phrase in phrases:
|
||||
if phrase in blank(string):
|
||||
if not phrase in blank(title):
|
||||
return True
|
||||
return False
|
||||
|
||||
def blank(string, downcase=True):
|
||||
import re
|
||||
regex = re.compile('[^a-zA-Z0-9\ ]')
|
||||
string = regex.sub('', string)
|
||||
if downcase: string = string.lower()
|
||||
return ' '.join(string.split())
|
||||
|
||||
def blank_include(this, includes_this):
|
||||
this = blank(this)
|
||||
includes_this = blank(includes_this)
|
||||
if includes_this in this:
|
||||
return True
|
||||
return False
|
||||
|
||||
def individual_word_match(match_against, match):
|
||||
match_against = blank(match_against).split(" ")
|
||||
match = blank(match).split(" ")
|
||||
matched = []
|
||||
for match_ag in match_against:
|
||||
for word in match:
|
||||
if match_ag == word:
|
||||
matched.append(word)
|
||||
return (float(len(set(matched))) / float(len(match_against)))
|
||||
|
||||
def flatten(l):
|
||||
flattened_list = []
|
||||
for x in l:
|
||||
if type(x) != str:
|
||||
for y in x:
|
||||
flattened_list.append(y)
|
||||
else:
|
||||
flattened_list.append(x)
|
||||
return flattened_list
|
||||
|
||||
def remove_none_values(d):
|
||||
new_d = d
|
||||
for x in list(d.keys()):
|
||||
if type(new_d[x]) is list:
|
||||
new_d[x] = remove_none_values(d[x])
|
||||
elif new_d[x] == None:
|
||||
del new_d[x]
|
||||
return new_d
|
||||
|
||||
#=========================================
|
||||
# Download Log Reading/Updating/Formatting
|
||||
#=========================================
|
||||
|
||||
def format_download_log_line(t, download_status="not downloaded"):
|
||||
return (" @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
||||
str(t["genre"]), t["track_number"], t["disc_number"], t["compilation"], \
|
||||
t["file_prefix"], download_status]))
|
||||
|
||||
def format_download_log_data(data):
|
||||
lines = []
|
||||
for track in data:
|
||||
lines.append(format_download_log_line(track))
|
||||
return "\n".join(lines)
|
||||
|
||||
def read_download_log(spotify):
|
||||
data = []
|
||||
with open(".irs-download-log", "r") as file:
|
||||
for line in file:
|
||||
line = line.split(" @@ ")
|
||||
data.append({
|
||||
"name": line[0],
|
||||
"artist": line[1],
|
||||
"album": spotify.album(line[2]),
|
||||
"genre": eval(line[3]),
|
||||
"track_number": line[4],
|
||||
"disc_number": line[5],
|
||||
"compilation": bool(line[6]),
|
||||
"file_prefix": line[7],
|
||||
})
|
||||
return data
|
||||
|
||||
def update_download_log_line_status(track, status="downloaded"):
|
||||
line_to_find = format_download_log_line(track)
|
||||
with open(".irs-download-log", "r") as input_file, \
|
||||
open(".irs-download-log", "w") as output_file:
|
||||
for line in input_file:
|
||||
if line == line_to_find:
|
||||
output_file.write(format_download_log_line(track, status))
|
||||
else:
|
||||
output_file.write(line)
|
||||
|
||||
|
||||
#============================================
|
||||
# And Now, For Something Completely Different
|
||||
#============================================
|
||||
# (It's for the CLI)
|
||||
|
||||
import os, sys, re
|
||||
# And Now For Something Completely Different
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
from time import sleep
|
||||
import pkg_resources
|
||||
|
||||
# Config File and Flags
|
||||
from .config import CONFIG
|
||||
|
||||
|
||||
# =========================
|
||||
# Youtube-DL Logs and Hooks
|
||||
# =========================
|
||||
|
||||
class YdlUtils:
|
||||
def clear_line():
|
||||
sys.stdout.write("\x1b[2K\r")
|
||||
|
||||
class MyLogger(object):
|
||||
def debug(self, msg):
|
||||
pass
|
||||
|
||||
def warning(self, msg):
|
||||
pass
|
||||
|
||||
def error(self, msg):
|
||||
print(msg)
|
||||
|
||||
@staticmethod
|
||||
def my_hook(d):
|
||||
if d['status'] == 'finished':
|
||||
print("Converting to mp3 ...")
|
||||
|
||||
|
||||
# ================================
|
||||
# Object Manipulation and Checking
|
||||
# ================================
|
||||
|
||||
class ObjManip: # Object Manipulation
|
||||
def limit_song_name(song):
|
||||
bad_phrases = "remaster remastered master".split(" ")
|
||||
# I have "master" here because Spotify actually sometimes mispells
|
||||
# stuff and it is hella annoying, so this was my solution
|
||||
for phrase in bad_phrases:
|
||||
if ObjManip.blank_include(song.split(" - ")[-1], phrase):
|
||||
return song.split(" - ")[0]
|
||||
return song
|
||||
|
||||
@staticmethod
|
||||
def check_garbage_phrases(phrases, string, title):
|
||||
for phrase in phrases:
|
||||
if phrase in ObjManip.blank(string):
|
||||
if phrase not in ObjManip.blank(title):
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def blank(string, downcase=True):
|
||||
import re
|
||||
regex = re.compile('[^a-zA-Z0-9\ ]')
|
||||
string = regex.sub('', string)
|
||||
if downcase:
|
||||
string = string.lower()
|
||||
return ' '.join(string.split())
|
||||
|
||||
@staticmethod
|
||||
def blank_include(this, includes_this):
|
||||
this = ObjManip.blank(this)
|
||||
includes_this = ObjManip.blank(includes_this)
|
||||
if includes_this in this:
|
||||
return True
|
||||
return False
|
||||
|
||||
def individual_word_match(match_against, match):
|
||||
match_against = ObjManip.blank(match_against).split(" ")
|
||||
match = ObjManip.blank(match).split(" ")
|
||||
matched = []
|
||||
for match_ag in match_against:
|
||||
for word in match:
|
||||
if match_ag == word:
|
||||
matched.append(word)
|
||||
return (float(len(set(matched))) / float(len(match_against)))
|
||||
|
||||
def flatten(l):
|
||||
flattened_list = []
|
||||
for x in l:
|
||||
if type(x) != str:
|
||||
for y in x:
|
||||
flattened_list.append(y)
|
||||
else:
|
||||
flattened_list.append(x)
|
||||
return flattened_list
|
||||
|
||||
def remove_none_values(d):
|
||||
new_d = d
|
||||
for x in list(d.keys()):
|
||||
if type(new_d[x]) is list:
|
||||
new_d[x] = ObjManip.remove_none_values(d[x])
|
||||
elif new_d[x] is None:
|
||||
del new_d[x]
|
||||
return new_d
|
||||
|
||||
|
||||
# ========================================
|
||||
# Download Log Reading/Updating/Formatting
|
||||
# ========================================
|
||||
|
||||
class DLog:
|
||||
def format_download_log_line(t, download_status="not downloaded"):
|
||||
return (" @@ ".join([t["name"], t["artist"], t["album"]["id"],
|
||||
str(t["genre"]), t["track_number"], t["disc_number"],
|
||||
t["compilation"], t["file_prefix"], download_status]))
|
||||
|
||||
def format_download_log_data(data):
|
||||
lines = []
|
||||
for track in data:
|
||||
lines.append(DLog.format_download_log_line(track))
|
||||
return "\n".join(lines)
|
||||
|
||||
def read_download_log(spotify):
|
||||
data = []
|
||||
with open(".irs-download-log", "r") as file:
|
||||
for line in file:
|
||||
line = line.split(" @@ ")
|
||||
data.append({
|
||||
"name": line[0],
|
||||
"artist": line[1],
|
||||
"album": spotify.album(line[2]),
|
||||
"genre": eval(line[3]),
|
||||
"track_number": line[4],
|
||||
"disc_number": line[5],
|
||||
"compilation": bool(line[6]),
|
||||
"file_prefix": line[7],
|
||||
})
|
||||
return data
|
||||
|
||||
def update_download_log_line_status(track, status="downloaded"):
|
||||
line_to_find = DLog.format_download_log_line(track)
|
||||
with open(".irs-download-log", "r") as input_file:
|
||||
with open(".irs-download-log", "w") as output_file:
|
||||
for line in input_file:
|
||||
if line == line_to_find:
|
||||
output_file.write(
|
||||
DLog.format_download_log_line(track, status))
|
||||
else:
|
||||
output_file.write(line)
|
||||
|
||||
|
||||
# ===========================================
|
||||
# And Now, For Something Completely Different
|
||||
# ===========================================
|
||||
# (It's for the CLI)
|
||||
|
||||
COLS = int(os.popen('tput cols').read().strip("\n"))
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
|
@ -149,36 +168,41 @@ if sys.version_info[0] == 2:
|
|||
def code(code1):
|
||||
return "\x1b[%sm" % str(code1)
|
||||
|
||||
|
||||
def no_colors(string):
|
||||
return re.sub("\x1b\[\d+m", "", string)
|
||||
|
||||
|
||||
def center_colors(string, cols):
|
||||
return no_colors(string).center(cols).replace(no_colors(string), string)
|
||||
|
||||
|
||||
def decode_utf8(string):
|
||||
if sys.version_info[0] == 3:
|
||||
return string.encode("utf8", "strict").decode()
|
||||
elif sys.version_info[0] == 2:
|
||||
return string.decode("utf8")
|
||||
|
||||
|
||||
def center_unicode(string, cols):
|
||||
tmp_chars = "X" * len(decode_utf8(string))
|
||||
chars = center_colors(tmp_chars, cols)
|
||||
return chars.replace(tmp_chars, string)
|
||||
|
||||
|
||||
def center_lines(string, cols, end="\n"):
|
||||
lines = []
|
||||
for line in string.split("\n"):
|
||||
lines.append(center_unicode(line, cols))
|
||||
return end.join(lines)
|
||||
|
||||
|
||||
def flush_puts(msg, time=0.01):
|
||||
# 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.
|
||||
# You just give the time for how *buurp* slow you wa-want it, Morty.
|
||||
# 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.
|
||||
# It works with colors and escape characters too, Morty.
|
||||
# Your grandpa's a genius *burrrp* Morty
|
||||
pattern = re.compile("(\x1b\[\d+m)")
|
||||
def check_color(s):
|
||||
if "\x1b" not in s:
|
||||
new = list(s)
|
||||
|
@ -187,13 +211,13 @@ def flush_puts(msg, time=0.01):
|
|||
return new
|
||||
msg = re.split("(\x1b\[\d+m)", msg)
|
||||
msg = list(filter(None, map(check_color, msg)))
|
||||
msg = flatten(msg)
|
||||
msg = ObjManip.flatten(msg)
|
||||
for char in msg:
|
||||
if char not in (" ", "", "\n") and "\x1b" not in char:
|
||||
sleep(time)
|
||||
sys.stdout.write(char)
|
||||
sys.stdout.flush()
|
||||
print ("")
|
||||
print("")
|
||||
|
||||
|
||||
BOLD = code(1)
|
||||
|
@ -213,6 +237,7 @@ BPURPLE = PURPLE + BOLD
|
|||
BCYAN = CYAN + BOLD
|
||||
BGRAY = GRAY + BOLD
|
||||
|
||||
|
||||
def banner():
|
||||
title = (BCYAN + center_lines("""\
|
||||
██╗██████╗ ███████╗
|
||||
|
@ -225,37 +250,42 @@ def banner():
|
|||
for num in range(0, 6):
|
||||
os.system("clear || cls")
|
||||
if num % 2 == 1:
|
||||
print (BRED + center_unicode("🚨 🚨 🚨 🚨 🚨 \r", COLS))
|
||||
print(BRED + center_unicode("🚨 🚨 🚨 🚨 🚨 \r", COLS))
|
||||
else:
|
||||
print ("")
|
||||
print (title)
|
||||
print("")
|
||||
print(title)
|
||||
sleep(0.3)
|
||||
flush_puts(center_colors("{0}Ironic Redistribution System ({1}IRS{2})"\
|
||||
.format(BYELLOW, BRED, BYELLOW), COLS))
|
||||
flush_puts(center_colors("{0}Ironic Redistribution System ({1}IRS{2})"
|
||||
.format(BYELLOW, BRED, BYELLOW), COLS))
|
||||
|
||||
flush_puts(center_colors("{0}Made with 😈 by: {1}Kepoor Hampond ({2}kepoorhampond{3})"\
|
||||
.format(BBLUE, BYELLOW, BRED, BYELLOW) + END, COLS))
|
||||
flush_puts(center_colors("{0}Made with 😈 by: {1}Kepoor Hampond \
|
||||
({2}kepoorhampond{3})".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):
|
||||
flush_puts("Choose option from menu:", time)
|
||||
flush_puts("\t[{0}song{1}] Download Song".format(BGREEN, END), time)
|
||||
flush_puts("\t[{0}album{1}] Download Album".format(BGREEN, END), time)
|
||||
flush_puts("\t[{0}{1}{2}] Download Playlist".format(BGREEN, unicode[-1], END), time)
|
||||
flush_puts("\t[{0}{1}{2}] Download Playlist"
|
||||
.format(BGREEN, unicode[-1], END), time)
|
||||
flush_puts("\t[{0}help{1}] Print This Menu".format(BGREEN, END), time)
|
||||
flush_puts("\t[{0}exit{1}] Exit IRS".format(BGREEN, END), time)
|
||||
print ("")
|
||||
print("")
|
||||
|
||||
|
||||
def console(ripper):
|
||||
banner()
|
||||
print (END)
|
||||
if ripper.authorized == True:
|
||||
print(END)
|
||||
if ripper.authorized is True:
|
||||
unicode = [BGREEN + "✔" + END, "list"]
|
||||
elif ripper.authorized == False:
|
||||
elif ripper.authorized is False:
|
||||
unicode = [BRED + "✘" + END]
|
||||
flush_puts("[{0}] Authenticated with Spotify".format(unicode[0]))
|
||||
print ("")
|
||||
print("")
|
||||
menu(unicode)
|
||||
while True:
|
||||
try:
|
||||
|
@ -267,92 +297,104 @@ def console(ripper):
|
|||
try:
|
||||
if choice in ("song", "s"):
|
||||
song_name = input("Song name{0}:{1} ".format(BBLUE, END))
|
||||
artist_name = input("Artist name{0}:{1} ".format(BBLUE, END))
|
||||
artist_name = input("Artist name{0}:{1} "
|
||||
.format(BBLUE, END))
|
||||
ripper.song(song_name, artist_name)
|
||||
|
||||
elif choice in ("album", "a"):
|
||||
album_name = input("Album name{0}:{1} ".format(BBLUE, END))
|
||||
ripper.spotify_list("album", album_name)
|
||||
|
||||
elif choice in ("list", "l") and ripper.authorized == True:
|
||||
username = input("Spotify Username{0}:{1} ".format(BBLUE, END))
|
||||
list_name = input("Playlist Name{0}:{1} ".format(BBLUE, END))
|
||||
elif choice in ("list", "l") and ripper.authorized is True:
|
||||
username = input("Spotify Username{0}:{1} "
|
||||
.format(BBLUE, END))
|
||||
list_name = input("Playlist Name{0}:{1} "
|
||||
.format(BBLUE, END))
|
||||
ripper.spotify_list("playlist", list_name, username)
|
||||
|
||||
elif choice in ("help", "h", "?"):
|
||||
menu(unicode, 0)
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
print ("")
|
||||
print("")
|
||||
pass
|
||||
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
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:
|
||||
if where is not 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, where=None):
|
||||
tmp_args = ripper.args
|
||||
if where != None and ripper.args.get(where):
|
||||
tmp_args = ripper.args.get("where")
|
||||
# tmp_args = ripper.args
|
||||
# if where is not None and ripper.args.get(where):
|
||||
# tmp_args = ripper.args.get("where")
|
||||
|
||||
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:
|
||||
elif os.environ.get(key) and environment is 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
|
||||
class Config:
|
||||
|
||||
def parse_artist(ripper):
|
||||
artist = check_sources(ripper, "artist")
|
||||
return artist
|
||||
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_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("~"))
|
||||
@staticmethod
|
||||
def parse_search_terms(ripper):
|
||||
search_terms = check_sources(ripper, "additional_search_terms",
|
||||
"lyrics")
|
||||
return search_terms
|
||||
|
||||
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_artist(ripper):
|
||||
artist = check_sources(ripper, "artist")
|
||||
return artist
|
||||
|
||||
def parse_organize(ripper):
|
||||
organize = check_sources(ripper, "organize")
|
||||
if organize == None:
|
||||
return check_sources(ripper, "organize", False, where="post_processors")
|
||||
else:
|
||||
return True
|
||||
@staticmethod
|
||||
def parse_directory(ripper):
|
||||
directory = check_sources(ripper, "custom_directory",
|
||||
where="post_processors")
|
||||
if directory is None:
|
||||
directory = check_sources(ripper, "custom_directory", "~/Music")
|
||||
return directory.replace("~", os.path.expanduser("~"))
|
||||
|
||||
def parse_search_terms(ripper):
|
||||
search_terms = check_sources(ripper, "additional_search_terms", "lyrics")
|
||||
return search_terms
|
||||
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 is None:
|
||||
return check_sources(ripper, "organize", False,
|
||||
where="post_processors")
|
||||
else:
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue