mirror of
https://github.com/cooperhammond/irs.git
synced 2025-02-28 23:26:48 +00:00
Added the ability to choose a link from a list of titles
This commit is contained in:
parent
c743fcc51d
commit
e5b17ca049
|
@ -1,4 +1,35 @@
|
||||||
#!/usr/bin python
|
#!/usr/bin python
|
||||||
|
|
||||||
|
HELP = \
|
||||||
|
"""
|
||||||
|
usage:
|
||||||
|
irs (-h | -v)
|
||||||
|
irs [-l]
|
||||||
|
irs -p PLAYLIST [-c COMMAND] [-l]
|
||||||
|
irs -a ARTIST (-s SONG | -A ALBUM [-st SEARCH_TERMS]) [-c COMMAND] [-l]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
-v, --version Display the version and exit.
|
||||||
|
-c COMMAND, --command COMMAND
|
||||||
|
Run a background command with each song's location.
|
||||||
|
Example: `-c "rhythmbox %(loc)s"`
|
||||||
|
-a ARTIST, --artist ARTIST
|
||||||
|
Specify the artist name.
|
||||||
|
-p PLAYLIST, --playlist PLAYLIST
|
||||||
|
Specify playlist filename. Each line in the file
|
||||||
|
should be formatted like so: `SONGNAME - ARTIST`
|
||||||
|
-s SONG, --song SONG Specify song name of the artist.
|
||||||
|
-A ALBUM, --album ALBUM
|
||||||
|
Specify album name of the artist.
|
||||||
|
-st SEARCH_TERMS, --search-terms SEARCH_TERMS
|
||||||
|
Only use if calling -A/--album. Acts as extra search
|
||||||
|
terms when looking for the album.
|
||||||
|
|
||||||
|
-l, --choose-link If supplied, will bring up a console choice for what
|
||||||
|
link you want to download based off a list of titles.
|
||||||
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from os import system
|
from os import system
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
@ -36,10 +67,12 @@ def main():
|
||||||
parser.add_argument('-c', '--command', dest="command", help="Run a background command with each song's location.")
|
parser.add_argument('-c', '--command', dest="command", help="Run a background command with each song's location.")
|
||||||
parser.add_argument('-a', '--artist', dest="artist", help="Specify the artist name.")
|
parser.add_argument('-a', '--artist', dest="artist", help="Specify the artist name.")
|
||||||
|
|
||||||
|
parser.add_argument('-l', '--choose-link', action='store_true', dest="link", \
|
||||||
|
help="Whether or not to choose the link from a list of titles.")
|
||||||
|
|
||||||
parser.add_argument('-p', '--playlist', dest="playlist", \
|
parser.add_argument('-p', '--playlist', dest="playlist", \
|
||||||
help="Specify playlist filename. Each line should be formatted like so: SONGNAME - ARTIST")
|
help="Specify playlist filename. Each line should be formatted like so: SONGNAME - ARTIST")
|
||||||
|
|
||||||
|
|
||||||
media = parser.add_mutually_exclusive_group()
|
media = parser.add_mutually_exclusive_group()
|
||||||
media.add_argument('-s', '--song', dest="song", help="Specify song name of the artist.")
|
media.add_argument('-s', '--song', dest="song", help="Specify song name of the artist.")
|
||||||
|
|
||||||
|
@ -51,33 +84,8 @@ def main():
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.help:
|
if args.help:
|
||||||
help = \
|
global HELP
|
||||||
"""
|
print (HELP)
|
||||||
usage:
|
|
||||||
irs
|
|
||||||
irs (-h | -v)
|
|
||||||
irs -p PLAYLIST [-c COMMAND]
|
|
||||||
irs -a ARTIST (-s SONG | -A ALBUM [-st SEARCH_TERMS]) [-c COMMAND]
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-h, --help show this help message and exit
|
|
||||||
-v, --version Display the version and exit.
|
|
||||||
-c COMMAND, --command COMMAND
|
|
||||||
Run a background command with each song's location.
|
|
||||||
Example: `-c "rhythmbox %(loc)s"`
|
|
||||||
-a ARTIST, --artist ARTIST
|
|
||||||
Specify the artist name.
|
|
||||||
-p PLAYLIST, --playlist PLAYLIST
|
|
||||||
Specify playlist filename. Each line in the file
|
|
||||||
should be formatted like so: `SONGNAME - ARTIST`
|
|
||||||
-s SONG, --song SONG Specify song name of the artist.
|
|
||||||
-A ALBUM, --album ALBUM
|
|
||||||
Specify album name of the artist.
|
|
||||||
-st SEARCH_TERMS, --search-terms SEARCH_TERMS
|
|
||||||
Only use if calling -A/--album. Acts as extra search
|
|
||||||
terms when looking for the album.
|
|
||||||
"""
|
|
||||||
print (help)
|
|
||||||
|
|
||||||
elif args.version:
|
elif args.version:
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
|
@ -101,17 +109,15 @@ Options:
|
||||||
console()
|
console()
|
||||||
|
|
||||||
elif args.playlist:
|
elif args.playlist:
|
||||||
rip_playlist(args.playlist, args.command)
|
rip_playlist(args.playlist, args.command, choose_link=args.link)
|
||||||
|
|
||||||
elif args.artist:
|
elif args.artist:
|
||||||
if args.album:
|
if args.album:
|
||||||
if args.search_terms:
|
rip_album(args.album, args.artist, command=args.command, \
|
||||||
rip_album(args.album, args.artist, command=args.command, search=args.search_terms)
|
search=args.search_terms, choose_link=args.link)
|
||||||
else:
|
|
||||||
rip_album(args.album, args.artist, command=args.command)
|
|
||||||
|
|
||||||
elif args.song:
|
elif args.song:
|
||||||
rip_mp3(args.song, args.artist, command=args.command)
|
rip_mp3(args.song, args.artist, command=args.command, choose_link=args.link)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,12 @@ from bs4 import BeautifulSoup
|
||||||
from .utils import *
|
from .utils import *
|
||||||
from .metadata import *
|
from .metadata import *
|
||||||
|
|
||||||
def find_mp3(song, artist):
|
def find_mp3(song, artist,
|
||||||
print (color(song, ["BOLD", "UNDERLINE"]) + ' by ' + color(artist, ["BOLD", "UNDERLINE"]) + '\n')
|
choose_link=False, # Whether to allow the user to choose the link.
|
||||||
|
):
|
||||||
|
|
||||||
|
os.system("clear")
|
||||||
|
print (color(song, ["BOLD", "UNDERLINE"]) + ' by ' + color(artist, ["BOLD", "UNDERLINE"]))
|
||||||
|
|
||||||
search_terms = song + " " + artist
|
search_terms = song + " " + artist
|
||||||
query_string = urlencode({"search_query" : (search_terms)})
|
query_string = urlencode({"search_query" : (search_terms)})
|
||||||
|
@ -23,10 +27,14 @@ def find_mp3(song, artist):
|
||||||
html_content = urlopen("http://www.youtube.com/results?" + query_string)
|
html_content = urlopen("http://www.youtube.com/results?" + query_string)
|
||||||
search_results = findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
|
search_results = findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
|
||||||
|
|
||||||
|
no_download_terms = ["full album"]
|
||||||
|
|
||||||
in_title = False
|
in_title = False
|
||||||
i = -1
|
i = -1
|
||||||
given_up_score = 0
|
given_up_score = 0
|
||||||
|
|
||||||
|
if not choose_link:
|
||||||
|
print (bc.YELLOW + "Finding youtube link ...", end="\r")
|
||||||
while in_title == False:
|
while in_title == False:
|
||||||
i += 1
|
i += 1
|
||||||
given_up_score += 1
|
given_up_score += 1
|
||||||
|
@ -39,13 +47,38 @@ def find_mp3(song, artist):
|
||||||
song_title = song.lower().split("/")
|
song_title = song.lower().split("/")
|
||||||
|
|
||||||
for song in song_title:
|
for song in song_title:
|
||||||
if strip_special_chars(song) in strip_special_chars(title):
|
song = strip_special_chars(song)
|
||||||
|
if song in title and any(term in song for term in no_download_terms):
|
||||||
in_title = True
|
in_title = True
|
||||||
|
|
||||||
|
print (bc.OKGREEN + "Found youtube link! \n" + bc.ENDC)
|
||||||
|
else:
|
||||||
|
results = []
|
||||||
|
|
||||||
|
print (bc.YELLOW + "Finding links ... " + bc.ENDC, end="\r")
|
||||||
|
|
||||||
|
for key in search_results[:10]:
|
||||||
|
results.append(BeautifulSoup(urlopen(("http://www.youtube.com/watch?v="\
|
||||||
|
+ key)), 'html.parser').title.string.replace(" - YouTube" , ""))
|
||||||
|
|
||||||
|
valid_choice = False
|
||||||
|
while valid_choice == False:
|
||||||
|
print (bc.HEADER + "What song would you like to download?")
|
||||||
|
index = 0
|
||||||
|
for result in results:
|
||||||
|
index += 1
|
||||||
|
print (" %s) %s" % (index, result))
|
||||||
|
i = int(input(bc.YELLOW + bc.BOLD + ":: " + bc.ENDC))
|
||||||
|
if i in tuple(range(1, 11)):
|
||||||
|
i -= 1
|
||||||
|
valid_choice = True
|
||||||
|
|
||||||
return search_results[i]
|
return search_results[i]
|
||||||
|
|
||||||
def rip_playlist(file_name, command=None):
|
def rip_playlist(file_name,
|
||||||
print (command.inspect)
|
command=None, # Whether to run a special user-supplied command.
|
||||||
|
choose_link=False, # Whether to allow the user to choose the link.
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
file = open(file_name, 'r')
|
file = open(file_name, 'r')
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -62,7 +95,7 @@ def rip_playlist(file_name, command=None):
|
||||||
rip_mp3(song, artist, command=command)
|
rip_mp3(song, artist, command=command)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.append("%s" + color(" : ", ["YELLOw"]) + bc.FAIL + str(e) + bc.ENDC)
|
errors.append(line + color(" : ", ["YELLOW"]) + bc.FAIL + str(e) + bc.ENDC)
|
||||||
|
|
||||||
if len(errors) > 0:
|
if len(errors) > 0:
|
||||||
print (bc.FAIL + "Something was wrong with the formatting of the following lines:" + bc.ENDC)
|
print (bc.FAIL + "Something was wrong with the formatting of the following lines:" + bc.ENDC)
|
||||||
|
@ -74,8 +107,13 @@ def rip_playlist(file_name, command=None):
|
||||||
def rip_album(album, artist,
|
def rip_album(album, artist,
|
||||||
tried=False, # for if it can't find the album the first time
|
tried=False, # for if it can't find the album the first time
|
||||||
search="album", # ditto
|
search="album", # ditto
|
||||||
command=None # For running a command with the song's location
|
command=None, # For running a command with the song's location
|
||||||
|
choose_link=False # Whether to allow the user to choose the link.
|
||||||
):
|
):
|
||||||
|
|
||||||
|
if search in (None, False):
|
||||||
|
search = "album"
|
||||||
|
|
||||||
visible_texts = search_google(album, artist, search)
|
visible_texts = search_google(album, artist, search)
|
||||||
errors = []
|
errors = []
|
||||||
try:
|
try:
|
||||||
|
@ -107,14 +145,15 @@ def rip_album(album, artist,
|
||||||
for i, j in enumerate(songs):
|
for i, j in enumerate(songs):
|
||||||
print (bc.OKBLUE + " - " + j + bc.ENDC)
|
print (bc.OKBLUE + " - " + j + bc.ENDC)
|
||||||
|
|
||||||
print (bc.YELLOW + "\nFinding album cover ... " + bc.ENDC, end="")
|
print (bc.YELLOW + "\nFinding album cover ... " + bc.ENDC, end="\r")
|
||||||
album_art_url = get_albumart_url(album, artist)
|
album_art_url = get_albumart_url(album, artist)
|
||||||
print (bc.OKGREEN + "\rAlbum cover found: " + bc.ENDC + album_art_url)
|
print (bc.OKGREEN + "Album cover found: " + bc.ENDC + album_art_url)
|
||||||
|
|
||||||
for i, j in enumerate(songs):
|
for i, j in enumerate(songs):
|
||||||
song = j
|
song = j
|
||||||
print (color("\n%s/%s - " % (i + 1, len(songs)), ["UNDERLINE"]), end="")
|
print (color("\n%s/%s - " % (i + 1, len(songs)), ["UNDERLINE"]), end="")
|
||||||
rip_mp3(j, artist, part_of_album=True, album=album, tracknum=i + 1, album_art_url=album_art_url, command=command)
|
rip_mp3(j, artist, part_of_album=True, album=album, tracknum=i + 1, \
|
||||||
|
album_art_url=album_art_url, command=command, choose_link=choose_link)
|
||||||
|
|
||||||
if len(errors) > 0:
|
if len(errors) > 0:
|
||||||
for error in errors: print (error)
|
for error in errors: print (error)
|
||||||
|
@ -125,21 +164,21 @@ def rip_album(album, artist,
|
||||||
if str(e) == "local variable 'indexed' referenced before assignment" or str(e) == 'list index out of range':
|
if str(e) == "local variable 'indexed' referenced before assignment" or str(e) == 'list index out of range':
|
||||||
if tried != True:
|
if tried != True:
|
||||||
print (bc.OKBLUE + "Trying to find album ..." + bc.ENDC)
|
print (bc.OKBLUE + "Trying to find album ..." + bc.ENDC)
|
||||||
rip_album(album, artist, tried=True, search="")
|
rip_album(album, artist, tried=True, search="", choose_link=choose_link)
|
||||||
else:
|
else:
|
||||||
print (bc.FAIL + 'Could not find album "%s"' % album + bc.ENDC)
|
print (bc.FAIL + 'Could not find album "%s"' % album + bc.ENDC)
|
||||||
else:
|
else:
|
||||||
errors.append(bc.FAIL + "There was a problem with downloading: " + bc.ENDC + song)
|
errors.append(bc.FAIL + "There was a problem with downloading: " + bc.ENDC + song + "\n" + str(e))
|
||||||
print (bc.FAIL + "Something major went wrong: " + str(e) + bc.ENDC)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def rip_mp3(song, artist,
|
def rip_mp3(song, artist,
|
||||||
part_of_album=False, # neccessary for creating folders
|
part_of_album=False, # neccessary for creating folders.
|
||||||
album=None, # if you want to specify an album and save a bit of time
|
album=None, # if you want to specify an album and save a bit of time.
|
||||||
tracknum=None, # to specify the tracknumber in the album
|
tracknum=None, # to specify the tracknumber in the album.
|
||||||
album_art_url=None, # if you want to save a lot of time trying to find album cover.
|
album_art_url=None, # if you want to save a lot of time trying to find album cover.
|
||||||
command=None, # For running a command with the song's location
|
command=None, # For running a command with the song's location.
|
||||||
|
choose_link=False # Whether to allow the user to choose the link.
|
||||||
):
|
):
|
||||||
|
|
||||||
audio_code = find_mp3(song, artist)
|
audio_code = find_mp3(song, artist)
|
||||||
|
|
|
@ -59,10 +59,14 @@ def parse_metadata(song, artist, location, filename,
|
||||||
|
|
||||||
|
|
||||||
# Album
|
# Album
|
||||||
|
try:
|
||||||
if not album:
|
if not album:
|
||||||
for i, j in enumerate(googled):
|
for i, j in enumerate(googled):
|
||||||
if "Album:" in j:
|
if "Album:" in j:
|
||||||
album = (googled[i + 1])
|
album = (googled[i + 1])
|
||||||
|
except Exception as e:
|
||||||
|
album = None
|
||||||
|
|
||||||
if album:
|
if album:
|
||||||
mp3file['album'] = album
|
mp3file['album'] = album
|
||||||
print (bc.OKGREEN + "Album parsed: " + bc.ENDC + mp3file['album'][0])
|
print (bc.OKGREEN + "Album parsed: " + bc.ENDC + mp3file['album'][0])
|
||||||
|
@ -95,7 +99,9 @@ def parse_metadata(song, artist, location, filename,
|
||||||
|
|
||||||
# Album art
|
# Album art
|
||||||
try:
|
try:
|
||||||
|
if album:
|
||||||
if not album_art_url:
|
if not album_art_url:
|
||||||
|
print (bc.YELLOW + "Parsing album art ..." + bc.ENDC, end="\r")
|
||||||
temp_url = get_albumart_url(album, artist)
|
temp_url = get_albumart_url(album, artist)
|
||||||
embed_mp3(temp_url, location + "/" + filename)
|
embed_mp3(temp_url, location + "/" + filename)
|
||||||
print (bc.OKGREEN + "Album art parsed: " + bc.ENDC + temp_url)
|
print (bc.OKGREEN + "Album art parsed: " + bc.ENDC + temp_url)
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -2,7 +2,7 @@ from setuptools import setup
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='irs',
|
name='irs',
|
||||||
version='1.4.9',
|
version='1.5.9',
|
||||||
description='A music downloader that just gets metadata.',
|
description='A music downloader that just gets metadata.',
|
||||||
url='https://github.com/kepoorhampond/irs',
|
url='https://github.com/kepoorhampond/irs',
|
||||||
author='Kepoor Hampond',
|
author='Kepoor Hampond',
|
||||||
|
|
Loading…
Reference in a new issue