-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshazam2dl.py
executable file
·223 lines (182 loc) · 7.55 KB
/
shazam2dl.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
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import urllib
import HTMLParser
import urllib2
import re
import sys
import json
import os
import time
import subprocess
import shazam_api
import shazam_parser
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
import pprint
import shutil
import optparse
from os import listdir, remove
from os.path import isfile, join, getsize
####################
### Global Vars
###################
fb_login = ""
fb_pass = ""
dl_dir = ""
get_all = False
html_parser = HTMLParser.HTMLParser()
pp = pprint.PrettyPrinter(indent=4)
FNULL = open('/dev/null' , 'w')
def normalize_str(elem):
norm = re.sub(r'/', '-', elem)
norm = re.sub(r'[/",;\?]+', '', norm)
norm = re.sub(r'&', 'and', norm)
norm = re.sub(r'\(.+\)', '', norm)
norm = norm.strip()
if norm[0:3] == "by ":
norm = norm[3:]
return norm
def add_proper_headers(http_req, accept, referer, cookie = ""):
http_req.add_header('User-Agent', "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0")
http_req.add_header('Accept', accept)
http_req.add_header('Accept-Language', "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3")
http_req.add_header('Connection', "keep-alive")
http_req.add_header('Referer', referer)
if cookie != "":
http_req.add_header('Cookie', cookie)
def get_shazam_tags(fat_cookie, uid_cookie):
my_tags = []
req = urllib2.Request("http://www.shazam.com/fragment/myshazam?size=large")
add_proper_headers( req,
accept = "application/json, text/javascript, */*; q=0.01",
referer = "http://www.shazam.com/myshazam",
cookie = "fat=" + fat_cookie + "; uid=" + uid_cookie + ";"
)
resp = urllib2.urlopen(req).read()
json_content = json.loads(resp)['feed']
if not get_all:
all_tags = shazam_parser.parse( html_parser.unescape(json_content) )
delay_to_keep = 60*30
else:
print 'Getting all shazam tags ...'
all_tags = []
tmp_resp = resp
tmp_json = json_content
tmp_tags = shazam_parser.parse( html_parser.unescape(json_content) )
next_url = "http://www.shazam.com" + str(json.loads(resp)['previous']) + '&size=large'
while True:
all_tags += tmp_tags
if len(next_url) < 200:
break
else:
req = urllib2.Request(next_url)
add_proper_headers( req,
accept = "application/json, text/javascript, */*; q=0.01",
referer = "http://www.shazam.com/myshazam",
cookie = "fat=" + fat_cookie + "; uid=" + uid_cookie + ";"
)
tmp_resp = urllib2.urlopen(req).read()
tmp_json = json.loads(tmp_resp)['feed']
tmp_tags = shazam_parser.parse( html_parser.unescape(tmp_json) )
next_url = "http://www.shazam.com" + str(json.loads(tmp_resp)['previous']) + '&size=large'
delay_to_keep = float("inf")
print 'Done ! (' + str(len(all_tags)) + ' tags)' + '\n'
for tag in all_tags:
artist = normalize_str(tag[0])
title = normalize_str(tag[1])
delay = tag[2]
filename = title + '-' + artist + ".mp3"
if { 'artist' : artist, 'title' : title } not in my_tags and filename not in already_dl and delay < delay_to_keep:
my_tags += [{ 'artist' : artist, 'title' : title, 'filename' : filename }]
return my_tags
def get_youtube_links(title, artist):
links = []
youtube_search = "http://gdata.youtube.com/feeds/api/videos?q=" + urllib.quote(title + " " + artist) + "&v=2&max-results=4&start-index=1&category=Music&format=5&fields=entry(id,title,link[@rel=%27alternate%27],author(name),yt:statistics,media:group(media:content),media:group(media:thumbnail))&alt=json-in-script&callback=define&key=AI39si6WqJb0Pbzi0lVyf3RMFS0DKg23xVBcBXDwnkzZGEMWP_JKsMQ47-6P8rV4BRkre3YNjhvXuSJ6dWeInyweJ2vmlOBJtw".encode('utf-8')
req = urllib2.Request(youtube_search)
add_proper_headers(req, "*/*", "http://www.shazam.com/myshazam")
resp = urllib2.urlopen(req)
json_resp = json.loads( resp.readlines()[1][7:-2] )
for entry in json_resp['feed']['entry']:
links += [entry['link'][0]['href']]
return links
def download_mp3(youtube_link, filename):
print ' + Try to download from ' + youtube_link
try:
filename_tmp = '.'.join(filename.split('.')[:-1]) + '.%(ext)s'
subprocess.call(["/usr/bin/youtube-dl", "--quiet", "--extract-audio", '--output='+filename_tmp, '--audio-format=mp3', youtube_link])
shutil.move(filename, dl_dir + 'to-valid-' + filename )
return True
except Exception,e:
print str(e)
return False
def send_report(status,to):
from_addr = "mmobox <shazam2dl@mmobox.fr>"
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "[shazam2dl] Download Report"
body = ""
for song in status:
body += song['artist'] + ' || ' + song['title'] + ' (' + song['status'] + ')\n'
msg.attach( MIMEText(body) )
smtp = smtplib.SMTP("localhost")
smtp.sendmail(from_addr, to, msg.as_string() )
smtp.close()
####################
## Parse Args
###################
usage = "usage: %prog [options] facebook-login facebook-password target-dir [all]"
parser = optparse.OptionParser(usage=usage)
opt, args = parser.parse_args()
if len(args) != 3 and len(args) != 4:
parser.print_help()
sys.exit(1)
else:
fb_login = args[0]
fb_pass = args[1]
dl_dir = args[2]
if len(args) == 4 and args[3] == "all":
get_all = True
####################
## MAIN
###################
uid_cookie, fat_cookie = shazam_api.get_api_cookie(fb_login, fb_pass)
already_dl = [ f for f in listdir(unicode(dl_dir)) if isfile(join(dl_dir,f)) ]
tags = get_shazam_tags(fat_cookie, uid_cookie)
status = []
if len(tags) == 0:
print 'Noting to do ...'
sys.exit(0)
for tag in tags:
dl_result = False
title = unicode(tag['title']).encode('utf-8')
artist = unicode(tag['artist']).encode('utf-8')
print "######## " + title + " -- " + artist
youtube_links = get_youtube_links(title, artist)
for link in youtube_links:
dl_result = download_mp3( link, tag['filename'] )
if dl_result == True:
print ' + Download succeed !'
break
else:
print ' + Failed ! Try again ...'
if dl_result == True:
print ' + Reencoding ...'
reenc = subprocess.call(["/usr/bin/lame", dl_dir + 'to-valid-' + tag['filename'], dl_dir + tag['filename']], stdout=FNULL, stderr=FNULL)
os.remove( dl_dir + 'to-valid-' + tag['filename'] )
print ' + Writing mp3 tags ...'
eyed3 = subprocess.call(["/usr/local/bin/eyeD3", "-a", artist, "-t", title, dl_dir + tag['filename']], stdout=FNULL, stderr=FNULL)
eyed3 = subprocess.call(["/usr/local/bin/eyeD3", "--to-v1.1", dl_dir + tag['filename']], stdout=FNULL, stderr=FNULL)
status += [{'artist' : artist, 'title' : title, 'status' : 'OK'}]
else:
status += [{'artist' : artist, 'title' : title, 'status' : '*FAILED*'}]
print " + IMPOSSIBLE TO DOWNLOAD !"
print "+ Sending report !"
######################
## SEND REPORT
#####################
send_report(status,["michael.molho@gmail.com"])