More error catching and a higher success rate in album finding

This commit is contained in:
Kepoor Hampond 2016-07-24 15:39:05 -07:00
parent 6709388022
commit 0ffe3803c6

34
irs.py
View file

@ -27,20 +27,23 @@ from bs4 import BeautifulSoup
import youtube_dl, mutagen
def download_album_art(album, band):
try:
search = "%s %s" % (album, band)
url = "http://www.seekacover.com/cd/" + urllib.parse.quote_plus(search)
page = requests.get(url).text
soup = BeautifulSoup(page)
soup = BeautifulSoup(page, 'html.parser')
done = False
for img in soup.findAll('img'):
if done == False:
try:
if search.lower() in img['title'].lower():
url = img['src']
urllib.request.urlretrieve(url, "cover.jpg")
urllib.request.urlretrieve(url, "%s/cover.jpg" % album)
done = True
except Exception:
pass
except Exception:
print ("%s There was an error parsing the album art of '%s'" % (output("e"), album) )
def embed_mp3(art_location, song_path):
music = mutagen.id3.ID3(song_path)
@ -58,6 +61,7 @@ def embed_mp3(art_location, song_path):
music.save()
def find_mp3(song, author):
try:
print ("'%s' by '%s'\n" % (song, author))
query_string = urllib.parse.urlencode({"search_query" : ("%s %s lyrics" % (song, author))})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
@ -78,6 +82,8 @@ def find_mp3(song, author):
if in_song == False:
given_up_score += 1
return audio_url
except Exception as e:
print ("%s There was an error finding the url of '%s'" % (output("e"), song) )
def rip_mp3(song, author, album, tracknum):
audio_url = find_mp3(song, author)
@ -166,9 +172,9 @@ def visible(element):
return False
return True
def search_google(song_name, band):
def search_google(song_name, band, search_terms):
try:
string = "%s %s" % (song_name, band)
string = "%s %s %s" % (song_name, band, search_terms)
filename = 'http://www.google.com/search?q=' + urllib.parse.quote_plus(string)
hdr = {
'User-Agent':'Mozilla/5.0',
@ -180,10 +186,11 @@ def search_google(song_name, band):
return visible_texts
except Exception as e:
print ("%s There was an error with Auto-parsing." % output("e"))
return ""
return
def get_album(album_name, artist, what_to_do):
visible_texts = search_google(album_name, artist)
def get_album(album_name, artist, what_to_do, search):
visible_texts = search_google(album_name, artist, search)
try:
songs = []
num = True
for i, j in enumerate(visible_texts):
@ -204,15 +211,22 @@ def get_album(album_name, artist, what_to_do):
pass
if what_to_do == "download":
for i, j in enumerate(songs):
rip_mp3(j, artist, album_name, i + 1)
rip_mp3(j, artist, album_name, i + 1, True)
elif what_to_do == "stream":
for i in songs:
a = find_mp3(i, artist)
command = 'mpv "%s" --no-video' % a
os.system(command)
except Exception as e:
if str(e) == "local variable 'indexed' referenced before assignment":
get_album(album_name, artist, what_to_do, "")
else:
print ("%s There was an error with getting the contents \
of the album '%s':\n%s" % (output("e"), album_name, e) )
def get_torrent_url(args, category):
try:
search_url = 'https://kat.cr/usearch/' + urllib.parse.quote_plus((" ".join(args) + " category:" + category))
search_request_response = requests.get(search_url, verify=True)
soup = BeautifulSoup(search_request_response.text, 'html.parser')
@ -245,6 +259,8 @@ def get_torrent_url(args, category):
soup = BeautifulSoup(search_url.text, 'html.parser')
torrent_url = 'https:' + soup.find_all('a', class_='siteButton')[0].get('href')
return torrent_url
except Exception as e:
print ("%s There was an error getting the torrent url with '%s'" % (output("e"), args))
def rip_playlist(file_name, what_to_do):
txt_file = open(file_name, 'r')
@ -290,7 +306,7 @@ def main():
elif media == "album":
album_name = (" ".join(args)).split(" by ")
get_album(album_name[0], album_name[1], what_to_do)
get_album(album_name[0], album_name[1], what_to_do, "album")
elif media == "playlist":
rip_playlist(args[-1], what_to_do)