mirror of
https://github.com/cooperhammond/irs.git
synced 2025-01-08 20:05:27 +00:00
Added more framework for downloading in python. It will happen. Soon.
This commit is contained in:
parent
846b2b9cb2
commit
cfdec19136
|
@ -28,18 +28,18 @@ class Ripper:
|
||||||
self.locations = []
|
self.locations = []
|
||||||
try:
|
try:
|
||||||
client_credentials_manager = SpotifyClientCredentials(os.environ["SPOTIFY_CLIENT_ID"], os.environ["SPOTIFY_CLIENT_SECRET"])
|
client_credentials_manager = SpotifyClientCredentials(os.environ["SPOTIFY_CLIENT_ID"], os.environ["SPOTIFY_CLIENT_SECRET"])
|
||||||
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 spotipy.oauth2.SpotifyOauthError:
|
||||||
spotify = spotipy.Spotify()
|
self.spotify = spotipy.Spotify()
|
||||||
self.authorized = False
|
self.authorized = False
|
||||||
|
|
||||||
def find_yt_url(self, song=None, artist=None, additional_search="lyrics"):
|
def find_yt_url(self, song=None, artist=None, additional_search="lyrics"):
|
||||||
if not song:
|
try:
|
||||||
song = self.args["song"]
|
if not song: song = self.args["song"]
|
||||||
|
if not artist: artist = self.args["artist"]
|
||||||
if not artist:
|
except ValueError:
|
||||||
artist = self.args["artist"]
|
raise ValueError("Must have specify `song` and/or `artist` in either init with `args` or with method arguments.")
|
||||||
|
|
||||||
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)})
|
||||||
|
@ -82,3 +82,83 @@ class Ripper:
|
||||||
|
|
||||||
return ("https://youtube.com" + self.code["href"], self.code["title"])
|
return ("https://youtube.com" + self.code["href"], self.code["title"])
|
||||||
|
|
||||||
|
def spotify_list(self, type=None, title=None, username=None):
|
||||||
|
try:
|
||||||
|
if not type: type = self.args["type"]
|
||||||
|
if not title: title = self.args["title"]
|
||||||
|
if not username: username = self.args["username"]
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError("Must specify type/title/username in `args` with init, or in method arguements.")
|
||||||
|
|
||||||
|
if type == "album":
|
||||||
|
search = title
|
||||||
|
if "artist" in self.args:
|
||||||
|
search += " " + self.args["artist"]
|
||||||
|
list_of_lists = self.spotify.search(q=search, type="album")
|
||||||
|
elif type == "playlist":
|
||||||
|
list_of_lists = self.spotify.user_playlists(username)
|
||||||
|
|
||||||
|
if len(list_of_lists) > 0:
|
||||||
|
the_list = None
|
||||||
|
for list_ in list_of_lists:
|
||||||
|
if utils.blank_include(list_.name, title):
|
||||||
|
if "artist" in self.args:
|
||||||
|
if utils.blank_include(list_["artists"][0]["name"], self.args["artist"]):
|
||||||
|
the_list = list_
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
the_list = list_
|
||||||
|
break
|
||||||
|
if the_list != None:
|
||||||
|
print ('"%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
||||||
|
compilation = False
|
||||||
|
if type == "album":
|
||||||
|
tmp_albums = []
|
||||||
|
tmp_artists = []
|
||||||
|
for track in the_list["tracks"]:
|
||||||
|
tmp_albums.append(track["album"]["name"])
|
||||||
|
tmp_artists.append(track["artists"][0]["name"])
|
||||||
|
tmp_albums = list(set(tmp_albums))
|
||||||
|
tmp_artists = list(set(tmp_artists))
|
||||||
|
if len(tmp_albums) == 1 and len(tmp_artists) > 1:
|
||||||
|
compilation = True
|
||||||
|
|
||||||
|
tracks = []
|
||||||
|
file_prefix = ""
|
||||||
|
for track in the_list["tracks"]:
|
||||||
|
if type == "playlist":
|
||||||
|
file_prefix = str(len(tracks)) + " - "
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"name": track["name"],
|
||||||
|
"artist": track["artists"][0]["name"],
|
||||||
|
"album": track["album"],
|
||||||
|
"genre": track["artists"][0]["genres"],
|
||||||
|
"track_number": track["track_number"],
|
||||||
|
"disc_number": track["disc_number"],
|
||||||
|
"compilation": compilation,
|
||||||
|
"file_prefix": file_prefix,
|
||||||
|
}
|
||||||
|
|
||||||
|
tracks.append(data)
|
||||||
|
|
||||||
|
locations = self.list(tracks)
|
||||||
|
return self.post_processing(locations)
|
||||||
|
|
||||||
|
print ('Could not find any lists.')
|
||||||
|
return False
|
||||||
|
|
||||||
|
def list(list_data):
|
||||||
|
locations = []
|
||||||
|
with open(".irs-download-log", "w+") as file:
|
||||||
|
file.write(utils.format_download_log_data(list_data))
|
||||||
|
|
||||||
|
for track in list_data:
|
||||||
|
loc = self.song(track["name"], track["artist"], track)
|
||||||
|
|
||||||
|
if loc != False:
|
||||||
|
utils.update_download_log_line_status(track, "downloaded")
|
||||||
|
locations.append(loc)
|
||||||
|
|
||||||
|
os.remove(".irs-download-log")
|
||||||
|
return locations
|
22
irs/utils.py
22
irs/utils.py
|
@ -28,3 +28,25 @@ def individual_word_match(match_against, match):
|
||||||
if match_ag == word:
|
if match_ag == word:
|
||||||
matched.append(word)
|
matched.append(word)
|
||||||
return (float(matched.uniq.size) / float(match_against.size))
|
return (float(matched.uniq.size) / float(match_against.size))
|
||||||
|
|
||||||
|
def format_download_log_line(track, download_status="not downloaded"):
|
||||||
|
return " @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
||||||
|
t["genre"], t["track_number"], t["disc_number"], t["compilation"], \
|
||||||
|
t["prefix"], download_status]) + "\n"
|
||||||
|
|
||||||
|
def format_download_log_data(data):
|
||||||
|
line = []
|
||||||
|
for track in data:
|
||||||
|
lines.append(format_download_log_line(track))
|
||||||
|
return lines
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in a new issue