7 from xml.dom
import minidom
15 from pyechonest
import artist, catalog, config, playlist
19 sys.path.append(
"7digital-python/lib/")
22 """ API Key from echonest """ 27 """API key from 7 digital""" 31 """API key from last.fm""" 35 """"*********************************************************************************""" 38 config.ECHO_NEST_API_KEY=
"SFXNKMTRAZ3ULHK6U " 41 DIGITAL7_API_KEY =
'7dbpa63h3y3d' 43 """The use of last.fm API""" 44 API_KEY =
"bd1cb09de31188b43aa46f39b8e40614" 45 API_SECRET =
"d2537b0ce8bc859a6068833c5e2a72a7" 47 """Authentication, username and password""" 49 password_hash = pylast.md5(
"123456")
50 network = pylast.LastFMNetwork(api_key = API_KEY, api_secret =
51 API_SECRET, username = username, password_hash = password_hash)
55 ***This method is from get_preview_url.py by Thierry Bertin-Mahieux*** 56 Do a simple request to the 7digital API 57 We assume we don't do intense querying, this function is not robust 58 Return the answer as na xml document 60 stream = urllib2.urlopen(url)
61 xmldoc = minidom.parse(stream).documentElement
67 ***This method is from get_preview_url.py by Thierry Bertin-Mahieux*** 68 Search for an artist + title using 7digital search API 69 Return None if there is a problem, or tuple (title,trackid) 71 url =
'http://api.7digital.com/1.2/track/search?' 72 url +=
'oauth_consumer_key='+DIGITAL7_API_KEY
75 query = artistname +
' ' + query
76 query = urllib2.quote(query)
79 status = xmldoc.getAttribute(
'status')
82 resultelem = xmldoc.getElementsByTagName(
'searchResult')
83 if len(resultelem) == 0:
85 track = resultelem[0].getElementsByTagName(
'track')[0]
86 tracktitle = track.getElementsByTagName(
'title')[0].firstChild.data
87 trackid = int(track.getAttribute(
'id'))
88 return (tracktitle,trackid)
92 ***This method is from get_preview_url.py by Thierry Bertin-Mahieux*** 93 Ask for the preview to a particular track, get the XML answer 94 After calling the API with a given track id, 95 we get an XML response that looks like: 97 <response status="ok" version="1.2" xsi:noNamespaceSchemaLocation="http://api.7digital.com/1.2/static/7digitalAPI.xsd"> 99 http://previews.7digital.com/clips/34/6804688.clip.mp3 103 We parse it for the URL that we return, or '' if a problem 105 url =
'http://api.7digital.com/1.2/track/preview?redirect=false' 106 url +=
'&trackid='+str(trackid)
107 url +=
'&oauth_consumer_key='+DIGITAL7_API_KEY
109 status = xmldoc.getAttribute(
'status')
112 urlelem = xmldoc.getElementsByTagName(
'url')[0]
113 preview = urlelem.firstChild.nodeValue
119 mp3file = urllib2.urlopen(file_url)
122 local_file = open(file_name,
"wb")
124 local_file.write(mp3file.read())
153 tag_classification = [
"wake me up"]
166 track = network.track
169 for tags_name
in tag_classification:
170 tags = network.search_for_tag(tags_name)
172 tags_search = tags.get_next_page()
173 tag_file_name = tags_name +
"_tags_results.txt" 178 print(
"successfully written in the file")
180 tag_data = open(tag_file_name,
'wb')
182 os.makedirs(str(tags_name))
183 os.chdir(
'./' + str(tags_name) +
'/')
186 for each_letter
in tags_search:
188 tag_data.write(
"%s\n" % each_letter)
190 os.makedirs(str(each_letter))
191 os.chdir(
'./' + str(each_letter) +
'/')
194 tag_object = network.get_tag(each_letter)
195 tracks_results = tag_object.get_top_tracks()
197 track_file_name = str(each_letter) +
"_results.txt" 198 tag_track_file = open(track_file_name,
'wb')
200 print(track_file_name)
203 for each_line
in tracks_results:
204 tag_track_file.write(
"%s%s\n" % each_line)
206 tag_track_file.close()
208 artist_file_name = str(each_letter) +
"_artist.txt" 209 title_file_name = str(each_letter) +
"_title.txt" 213 newdata = open(track_file_name)
214 artist_data = open(artist_file_name,
'wb')
215 title_data = open(title_file_name,
'wb')
219 for each_row
in newdata:
221 (artist,title) = each_row.split(
'-',1)
222 title = title.strip()
223 title = title.rstrip(
'0')
225 artist_data.write(
"%s\n" % artist)
226 title_data.write(
"%s\n" % title)
234 if trackid
is not None:
240 file_name = tracktitle +
u'.wav' 241 file_name = file_name.replace(
u'/',
u' ')
247 path_name =
'./' + file_name
259 os.chdir(parent_path)
261 os.chdir(parent_path)
def download_file(file_url, file_name)
def get_preview_from_trackid(trackid)
def get_trackid_from_text_search(title, artistname='')