Added status update and minor usability features

This commit is contained in:
Kepoor Hampond 2016-09-16 10:55:44 -07:00
parent b386df5368
commit c0330ce3f4
2 changed files with 77 additions and 63 deletions

View file

@ -2,6 +2,8 @@
Downloads or streams media from the name of the song / album / movie / tv-show that you requested. Downloads or streams media from the name of the song / album / movie / tv-show that you requested.
# CURRENTLY BEING REMADE BECAUSE kat.cr WAS TAKEN DOWN.
### Dependencies ### Dependencies
First, actually install python and pip: First, actually install python and pip:

138
irs.py
View file

@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, sys, time, re, select, requests, subprocess import os, sys, time, re, select, requests
import urllib.request, urllib.parse import urllib.request, urllib.parse
from termcolor import colored from termcolor import colored
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
@ -62,12 +62,13 @@ def embed_mp3(art_location, song_path):
def find_mp3(song, author, soundtrack=False): def find_mp3(song, author, soundtrack=False):
try: try:
special_chars = "\ / : * ? \" < > | - ( )".split(" ")
operator = 'by' operator = 'by'
searching = "%s %s lyrics" % (song, author) searching = "%s %s lyrics" % (song, author)
if soundtrack == True: if soundtrack == True:
operator = "from" operator = "from"
searching = "%s soundtrack %s" % (author, song) searching = "%s soundtrack %s" % (author, song)
print ("'%s' %s '%s'\n" % (song, operator, author)) print ("%s %s %s\n" % (song, operator, author))
query_string = urllib.parse.urlencode({"search_query" : (searching)}) query_string = urllib.parse.urlencode({"search_query" : (searching)})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string) html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode()) search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
@ -80,9 +81,13 @@ def find_mp3(song, author, soundtrack=False):
i += 1 i += 1
audio_url = ("http://www.youtube.com/watch?v=" + search_results[i]) audio_url = ("http://www.youtube.com/watch?v=" + search_results[i])
title = (BeautifulSoup(urlopen(audio_url), 'html.parser')).title.string.lower() title = (BeautifulSoup(urlopen(audio_url), 'html.parser')).title.string.lower()
for char in special_chars:
title = title.replace(char, "")
song_title = (song.lower()).split("/") song_title = (song.lower()).split("/")
for song in song_title: for song in song_title:
if song in title: for char in special_chars:
song = song.replace(char, "")
if song in title and "full album" not in title:
in_song = True in_song = True
if in_song == False: if in_song == False:
given_up_score += 1 given_up_score += 1
@ -92,20 +97,23 @@ def find_mp3(song, author, soundtrack=False):
def rip_mp3(song, author, album, tracknum, soundtrack=False): def rip_mp3(song, author, album, tracknum, soundtrack=False):
audio_url = find_mp3(song, author, soundtrack=soundtrack) audio_url = find_mp3(song, author, soundtrack=soundtrack)
song = song.replace("/", "\\") special_chars = "\ / : * ? \" < > | ".split(" ")
filename = song
for char in special_chars:
filename = filename.replace(char, "")
command = 'youtube-dl --metadata-from-title "%(title)s" --extract-audio \ command = 'youtube-dl --metadata-from-title "%(title)s" --extract-audio \
--audio-format mp3 --add-metadata ' + audio_url --audio-format mp3 --add-metadata ' + audio_url
os.system(command) os.system(command)
for filename in os.listdir("."): for file in os.listdir("."):
if str(audio_url).strip("http://www.youtube.com/watch?v=") in filename: if str(audio_url).strip("http://www.youtube.com/watch?v=") in file:
os.rename(filename, song + ".mp3") # &PATH; os.rename(file, filename + ".mp3") # &PATH;
try: try:
googled = search_google(song, author, "") googled = search_google(song, author, "")
mp3file = MP3(song + ".mp3", ID3=EasyID3) mp3file = MP3(filename + ".mp3", ID3=EasyID3)
print ("\n%s Metadata parsing:" % output("s")) print ("\n%s Metadata parsing:" % output("s"))
try: try:
mp3file['title'] = song.replace("\\", "/") mp3file['title'] = song
except Exception: except Exception:
mp3file['title'] = "" mp3file['title'] = ""
mp3file.save() mp3file.save()
@ -231,11 +239,11 @@ def get_album(album_name, artist, what_to_do, search, tried=False):
print ("%s Trying to find album ..." % output("s")) print ("%s Trying to find album ..." % output("s"))
get_album(album_name, artist, what_to_do, "", True) get_album(album_name, artist, what_to_do, "", True)
else: else:
print ("%s Could not find album '%s'" % (output("e"), album_)) print ("%s Could not find album %s" % (output("e"), album_))
exit(0) exit(0)
else: else:
print ("%s There was an error with getting the contents \ print ("%s There was an error with getting the contents \
of the album '%s':\n%s" % (output("e"), album_name, e) ) of the album %s:\n%s" % (output("e"), album_name, e) )
def get_torrent_url(args, category): def get_torrent_url(args, category):
@ -308,7 +316,7 @@ def download_link(link, title, artist):
mp3file = MP3(title + '.mp3', ID3=EasyID3) mp3file = MP3(title + '.mp3', ID3=EasyID3)
googled = search_google(title, artist, "") googled = search_google(title, artist, "")
try: try:
mp3file['title'] = title.replace("\\", "/") mp3file['title'] = title
print ("\n%s Metadata parsing:") print ("\n%s Metadata parsing:")
print ('\t%s Title parsed: %s' % (output("g"), mp3file['title'])) print ('\t%s Title parsed: %s' % (output("g"), mp3file['title']))
mp3file['artist'] = artist mp3file['artist'] = artist
@ -349,65 +357,69 @@ def rip_soundtrack(movie, what_to_do):
print ("%s There was an error finding the soundtrack for '%s':\n%s" % (output('e'), movie, e)) print ("%s There was an error finding the soundtrack for '%s':\n%s" % (output('e'), movie, e))
def main(): def main():
i = 0 try:
args = sys.argv i = 0
del args[i] args = sys.argv
what_to_do = args[i] del args[i]
del args[i] what_to_do = args[i]
del args[i]
if what_to_do not in ("download", "stream"): raise Exception("no what-to-do") if what_to_do not in ("download", "stream"): raise Exception("no what-to-do")
media = args[i] media = args[i]
del args[i] del args[i]
if media == "song": if media == "song":
song = (" ".join(args)).split(" by ") song = (" ".join(args)).split(" by ")
if what_to_do == "stream": if what_to_do == "stream":
command = 'mpv "%s" --no-video' % find_mp3(song[0], song[1]) command = 'mpv "%s" --no-video' % find_mp3(song[0], song[1])
os.system(command) os.system(command)
elif what_to_do == "download": elif what_to_do == "download":
rip_mp3(song[0], song[1], "", "") rip_mp3(song[0], song[1], "", "")
elif media == "album": elif media == "album":
album_name = (" ".join(args)).split(" by ") album_name = (" ".join(args)).split(" by ")
get_album(album_name[0], album_name[1], what_to_do, "album") get_album(album_name[0], album_name[1], what_to_do, "album")
elif media == "playlist": elif media == "playlist":
rip_playlist(args[-1], what_to_do) rip_playlist(args[-1], what_to_do)
elif media in ("comic", "book"): elif media in ("comic", "book"):
if what_to_do == "download": if what_to_do == "download":
os.system("rtorrent '%s'" % get_torrent_url(args, media + "s")) os.system("rtorrent '%s'" % get_torrent_url(args, media + "s"))
exit(0) exit(0)
elif what_to_do == "stream": elif what_to_do == "stream":
print ("\n%s Streaming is unavailable for comics and books.\n" % output("e")) print ("\n%s Streaming is unavailable for comics and books.\n" % output("e"))
exit(0) exit(0)
elif media == "movie": elif media == "movie":
if what_to_do == "stream": if what_to_do == "stream":
os.system('peerflix "%s" -a -d --mpv' % get_torrent_url(args, 'movie')) os.system('peerflix "%s" -a -d --mpv' % get_torrent_url(args, 'movie'))
exit(0) exit(0)
elif what_to_do == "download": elif what_to_do == "download":
os.system("rtorrent '%s'" % get_torrent_url(args, 'movie')) os.system("rtorrent '%s'" % get_torrent_url(args, 'movie'))
exit(0) exit(0)
elif media == "tv":
if what_to_do == "stream":
os.system('peerflix "%s" -a -d --mpv' % get_torrent_url(args, 'tv'))
exit(0)
elif what_to_do == "download":
os.system("rtorrent '%s'" % get_torrent_url(args, 'tv'))
exit(0)
elif media == 'link':
if what_to_do == 'stream':
os.system('mpv "%s" --no-video' % args[0])
exit(0)
elif what_to_do == 'download':
download_link(args[0], args[1], args[2])
elif media == 'soundtrack':
rip_soundtrack(" ".join(args[0:]), what_to_do)
else:
raise Exception("no media")
except Exception as e:
invalid_format()
elif media == "tv":
if what_to_do == "stream":
os.system('peerflix "%s" -a -d --mpv' % get_torrent_url(args, 'tv'))
exit(0)
elif what_to_do == "download":
os.system("rtorrent '%s'" % get_torrent_url(args, 'tv'))
exit(0)
elif media == 'link':
if what_to_do == 'stream':
os.system('mpv "%s" --no-video' % args[0])
exit(0)
elif what_to_do == 'download':
download_link(args[0], args[1], args[2])
elif media == 'soundtrack':
rip_soundtrack(" ".join(args[0:]), what_to_do)
else:
raise Exception("no media")
def invalid_format(): def invalid_format():
# I feel like there should be an easier way to write out help for command-line interfaces ... # I feel like there should be an easier way to write out help for command-line interfaces ...