-
Notifications
You must be signed in to change notification settings - Fork 0
/
instantmusic.py
executable file
·397 lines (329 loc) · 13.1 KB
/
instantmusic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# -*- coding: UTF-8 -*-
from __future__ import print_function
import youtube_dl
import os
import sys
import re
import readline
import eyed3
from bs4 import BeautifulSoup
import requests
# Version compatiblity
import sys
import re
import utils
import unicodedata
import json
if (sys.version_info > (3, 0)):
from urllib.parse import quote_plus as qp
raw_input = input
unicode = str
else:
from urllib import quote_plus as qp
def get_download_name_file():
return "download_name_file"
def extract_videos(html):
"""
Parses given html and returns a list of (Title, Link) for
every movie found.
"""
soup = BeautifulSoup(html, 'html.parser')
pattern = re.compile(r'/watch\?v=')
found = soup.find_all('a', 'yt-uix-tile-link', href=pattern)
return [(x.text.encode('utf-8'), x.get('href')) for x in found]
def makeRequest(url, hdr):
http_proxy = os.environ.get("HTTP_PROXY")
https_proxy = os.environ.get("HTTPS_PROXY")
ftp_proxy = os.environ.get("FTP_PROXY")
proxyDict = {
"http": http_proxy,
"https": https_proxy,
"ftp": ftp_proxy
}
req = requests.get(url, headers=hdr, proxies=proxyDict)
return req
def grab_albumart(search=''):
search = qp(search)
site = "https://www.google.com/search?site=&tbm=isch&source=hp&biw=1112&bih=613&q=" + search + "&oq=backst&gs_l=img.3.0.0l10.1011.3209.0.4292.8.7.1.0.0.0.246.770.0j3j1.4.0..3..0...1.1.64.img..3.5.772.KyXkrVfTLT4#tbm=isch&q=back+street+boys+I+want+it+that+way"
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = makeRequest(site, hdr)
content = str(req.content)
end = content.find('jpg')
start = content[:end].rfind('http')
return content[start:end + 3]
def list_movies(movies):
for idx, (title, _) in enumerate(movies):
yield '[{}] {}'.format(idx, title.decode('utf-8').encode(sys.stdout.encoding))
def get_lyrics_url(response):
"""
Getting the url for lyrics.
"""
metrolyrics = 'https://www.metrolyrics.com'
link_start = response.find(metrolyrics)
if link_start is -1:
iprint("Not able to find https, trying http")
metrolyrics = 'http://www.metrolyrics.com'
link_start = response.find(metrolyrics)
if link_start is -1:
iprint("Metro lyrics not found")
return ""
link_end = response.find('html', link_start + 1)
link = response[link_start:link_end + 4]
return link
def search_videos(query):
"""
Searches for videos given a query
"""
response = makeRequest('https://www.youtube.com/results?search_query=' + query, {})
return extract_videos(response.content)
def iprint(str):pass
#print(str)
# Custom: Download and return id of track
def query_and_download_with_info(search, has_prompts=True, is_quiet=False):
if not is_quiet:
iprint('Searching...')
available = search_videos(search)
if not is_quiet:
if not available:
iprint('No results found matching your query.')
sys.exit(2) # Indicate failure using the exit code
else:
if has_prompts:
iprint('Found:', '\n'.join(list_movies(available)))
# We only ask the user which one they want if prompts are on, of course
if has_prompts and not is_quiet:
choice = raw_input('Pick one: ')
while not (choice.isdigit()) or not (0 <= int(choice) < len(available)):
iprint("Oups, that was wrong. Try again!")
choice = raw_input('Pick one: ')
title, video_link = available[int(choice)]
valid = ['Y', 'y', '']
prompt = raw_input('Download "%s"? (y/n) ' % title)
if prompt.lower() not in valid:
sys.exit()
# Otherwise, just download the first in available list
else:
title, video_link = available[0]
ydl_opts = {
'outtmpl': '%(id)s.%(ext)s',
#'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
#ydl_opts = { 'outtmpl': '%(id)s.%(ext)s' }
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info('https://www.youtube.com{0}'.format(video_link), download=True)
#print("info_dict",info_dict["display_id"])
return "{0}".format(info_dict["display_id"])
def query_and_download(search, has_prompts=True, is_quiet=False):
"""
Queries the internet for given lyrics and downloads them into the current working directory.
If has_prompts is False, will download first available song.
If is_quiet is True, will run beautiful-soup in quiet mode. Prompts will also automatically be turned
off in quiet mode. This is mainly so that instantmusic can be run as a background process.
Returns the title of the video downloaded from.
"""
if not is_quiet:
iprint('Searching...')
available = search_videos(search)
if not is_quiet:
if not available:
iprint('No results found matching your query.')
sys.exit(2) # Indicate failure using the exit code
else:
if has_prompts:
iprint('Found:', '\n'.join(list_movies(available)))
# We only ask the user which one they want if prompts are on, of course
if has_prompts and not is_quiet:
choice = raw_input('Pick one: ')
while not (choice.isdigit()) or not (0 <= int(choice) < len(available)):
iprint("Oups, that was wrong. Try again!")
choice = raw_input('Pick one: ')
title, video_link = available[int(choice)]
valid = ['Y', 'y', '']
prompt = raw_input('Download "%s"? (y/n) ' % title)
if prompt.lower() not in valid:
sys.exit()
# Otherwise, just download the first in available list
else:
title, video_link = available[0]
command_tokens = [
'youtube-dl',
'--extract-audio',
'--audio-format mp3',
'--audio-quality 9', #worse quality
#'--restrict-filenames',
'--output \'%(title)s.%(ext)s\'',
'https://www.youtube.com' + video_link]
if is_quiet:
command_tokens.insert(1, '-q')
command = ' '.join(command_tokens)
# Youtube-dl is a proof that god exists.
if not is_quiet:
iprint('Downloading')
os.system(command)
# Fixing id3 tags
try:
iprint('Fixing id3 tags')
list_name = title
artist = ''
track_name = title
if '-' in title:
list_name = title.split('-')
artist = list_name[0]
track_name = list_name[1]
album_name = ''
try:
try:
audiofile = eyed3.load((title + '.mp3'))
except:
audiofile = eyed3.load((title + '.m4a'))
url = 'https://www.google.com/search?q=' + qp(title)
req = makeRequest(url, {})
response = req.content
result = response
lyrics_html = get_lyrics_url(result)
a = makeRequest(lyrics_html, {})
iprint(lyrics_html)
html_doc = a.content
soup = BeautifulSoup(html_doc, 'html.parser')
try:
album_name = soup.find(id="album-name-link").text
except:
iprint('Cant get album name')
try:
lyrics = ('')
raw_lyrics = (soup.findAll('p', attrs={'class': 'verse'}))
for each_line in raw_lyrics:
lyrics = lyrics + str(each_line.get_text()) + '\n'
iprint(lyrics)
audiofile.tag.lyrics.set(u'' + lyrics)
except:
iprint('cant get lyrics')
except Exception as e:
iprint(e)
iprint('error getting album and lyrics')
iprint(artist, track_name, album_name)
def fix_string(s):
location = s.find('[')
if location != -1:
return s[:location].strip()
else:
return s.strip()
artist = fix_string(artist)
track_name = fix_string(track_name)
album_name = fix_string(album_name)
audiofile.tag.artist = normalize(artist)
audiofile.tag.title = normalize(track_name)
audiofile.tag.album = normalize(album_name)
search = title[:-4]
iprint('Downloading album art..')
image_link = grab_albumart(search)
title = normalize(title)
iprint('Fixing ' + title)
eyed3.log.setLevel("ERROR")
if audiofile.tag is None:
audiofile.tag = eyed3.id3.Tag()
audiofile.tag.file_info = eyed3.id3.FileInfo("foo.id3")
response = makeRequest(image_link, {}).content
imagedata = response
audiofile.tag.images.set(0, imagedata, "image/jpeg", u"Album Art")
audiofile.tag.save()
iprint('Fixed')
except Exception as e:
iprint(e)
iprint('couldnt get album art')
return title
def normalize(str):
return str.encode("utf8")
def search_uses_flags(argstring, *flags):
"""
Check if the given flags are being used in the command line argument string
"""
for flag in flags:
if (argstring.find(flag) != 0):
return True
return False
def main():
"""
Run the program session
"""
argument_string = ' '.join(sys.argv[1:])
search = ''
# No command-line arguments
if not sys.argv[1:]:
# We do not want to accept empty inputs :)
while search == '':
search = raw_input('Enter songname / lyrics / artist.. or whatever\n> ')
search = qp(search)
downloaded = query_and_download(search)
# Command-line arguments detected!
else:
# No flags at all are specified
if not search_uses_flags(argument_string, '-s', '-i', '-f', '-p', '-q'):
search = qp(' '.join(sys.argv[1:]))
downloaded = query_and_download(search)
# No input flags are specified
elif not search_uses_flags(argument_string, '-s', '-i', '-f'):
# Default to -s
lyrics = argument_string.replace('-p', '').replace('-q', '')
search = qp(lyrics)
downloaded = query_and_download(search, not search_uses_flags('-p'), search_uses_flags('-q'))
# Some input flags are specified
else:
# Lots of parser-building fun!
import argparse
parser = argparse.ArgumentParser(description='Instantly download any song!')
parser.add_argument('-p', action='store_false', dest='has_prompt', help="Turn off download prompts")
parser.add_argument('-q', action='store_true', dest='is_quiet',
help="Run in quiet mode. Automatically turns off prompts.")
parser.add_argument('-s', action='store', dest='song', nargs='+', help='Download a single song.')
parser.add_argument('-l', action='store', dest='songlist', nargs='+',
help='Download a list of songs, with lyrics separated by a comma (e.g. "i tried so hard and got so far, blackbird singing in the dead of night, hey shawty it\'s your birthday).')
parser.add_argument('-f', action='store', dest='file', nargs='+',
help='Download a list of songs from a file input. Each line in the file is considered one song.')
# Parse and check arguments
results = parser.parse_args()
song_list = []
if results.song:
song_list.append(qp(' '.join(results.song)))
if results.songlist:
songs = ' '.join(results.songlist)
song_list.extend([qp(song) for song in songs.split(',')])
if results.file:
with open(results.file[0], 'r') as f:
songs = f.readlines()
# strip out any empty lines
songs = filter(None, (song.rstrip() for song in songs))
# strip out any new lines
songs = [qp(song.strip()) for song in songs if song]
song_list.extend(songs)
prompt = True if results.has_prompt else False
quiet = True if results.is_quiet else False
downloads = []
for song in song_list:
downloads.append(query_and_download(song, prompt, quiet))
print('Downloaded: %s' % ', '.join(downloads))
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('Done downloading, now converting ...')
if __name__ == '__main__':
main()