new_track_data.py
Go to the documentation of this file.
1 
2 
3 import os
4 import sys
5 import pickle
6 
7 from xml.dom import minidom
8 import urllib2 #call url function
9 import time
10 import sqlite3
11 import cPickle
12 
13 import pyechonest # import echonest API
14 import pylast # import last.fm API
15 from pyechonest import artist, catalog, config, playlist # How it works instead of pyechonest
16 
17 
18 #change the path to 7-digital python library
19 sys.path.append("7digital-python/lib/")
20 import py7digital
21 
22 """ API Key from echonest """
23 #Your API Key: SFXNKMTRAZ3ULHK6U
24 #Your Consumer Key: 54a06c9bd235d47f787d8cf614577a94
25 #Your Shared Secret: aiPUaTToTpixW4Ttaf4O9A
26 
27 """API key from 7 digital"""
28 # oauth_consumer_key=7dbpa63h3y3d
29 # oauth_consumer_secret=zz48d4epsqmrsuvp
30 
31 """API key from last.fm"""
32 # Your API Key is bd1cb09de31188b43aa46f39b8e40614
33 # Your secret is d2537b0ce8bc859a6068833c5e2a72a7
34 
35 """"*********************************************************************************"""
36 
37 # Echo Nest API key
38 config.ECHO_NEST_API_KEY="SFXNKMTRAZ3ULHK6U "
39 
40 # 7 digital API key
41 DIGITAL7_API_KEY = '7dbpa63h3y3d'
42 
43 """The use of last.fm API"""
44 API_KEY = "bd1cb09de31188b43aa46f39b8e40614"
45 API_SECRET = "d2537b0ce8bc859a6068833c5e2a72a7"
46 
47 """Authentication, username and password"""
48 username = "QMYading"
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)
52 
53 def url_call(url):
54  """
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
59  """
60  stream = urllib2.urlopen(url)
61  xmldoc = minidom.parse(stream).documentElement
62  stream.close()
63  return xmldoc
64 
65 def get_trackid_from_text_search(title,artistname=''):
66  """
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)
70  """
71  url = 'http://api.7digital.com/1.2/track/search?'
72  url += 'oauth_consumer_key='+DIGITAL7_API_KEY
73  query = title
74  if artistname != '':
75  query = artistname + ' ' + query
76  query = urllib2.quote(query)
77  url += '&q='+query
78  xmldoc = url_call(url)
79  status = xmldoc.getAttribute('status')
80  if status != 'ok':
81  return None
82  resultelem = xmldoc.getElementsByTagName('searchResult')
83  if len(resultelem) == 0:
84  return None
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)
89 
91  """
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:
96 
97  <response status="ok" version="1.2" xsi:noNamespaceSchemaLocation="http://api.7digital.com/1.2/static/7digitalAPI.xsd">
98  <url>
99  http://previews.7digital.com/clips/34/6804688.clip.mp3
100  </url>
101  </response>
102 
103  We parse it for the URL that we return, or '' if a problem
104  """
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
108  xmldoc = url_call(url)
109  status = xmldoc.getAttribute('status')
110  if status != 'ok':
111  return ''
112  urlelem = xmldoc.getElementsByTagName('url')[0]
113  preview = urlelem.firstChild.nodeValue
114  return preview
115 
116  #function that downloads a file
117 def download_file(file_url, file_name):
118  # open the url
119  mp3file = urllib2.urlopen(file_url)
120 
121  # open the local file for writing
122  local_file = open(file_name, "wb")
123  # write to file
124  local_file.write(mp3file.read())
125  local_file.close()
126 
127 
128 
129 
130 # get artist object
131 # artist_object = network.get_artist("System of a Down")
132 
133 # get the mbid for the tracks
134 # print(artist_object.get_mbid())
135 
136 # print(os.getcwd()) #print current working directory
137 # artist_object.shout("Artist object created successful!")
138 
139 # get track object
140 # track_object = network.get_track("Iron Maiden", "The Nomad")
141 # track_object.add_tags(("awesome", "favorite")) # add tags to the tracks
142 
143 #get get_album object
144 #album_object = network.get_album("Adele","21")
145 
146 # get tags object
147 #os.chdir('./NEW_Tagsets/') # change current working directory
148 #tag_classification = ["Happy", "Sad", "Angry", "Relax","Sport", "Study","Entertainment","Travelling"]
149 #tag_classification = ["Happy", "Sad", "Angry", "Relax","Sport", "Study","Entertainment","Travel"]
150 #tag_classification = ["Sad", "Angry", "Relax","Sport", "Study","Entertainment","Travel"]
151 #tag_classification = ["Travel"]
152 
153 tag_classification = ["wake me up"]
154 
155 #tag_classification = ["Passionate","Rollicking","Literate","Humorous","Aggressive"]
156 #tag_classification = ["Joy","Fun","Cheerful","Pleasing","Pleasant"]
157 #tag_classification = ["Romantic","Love"]
158 
159 #tag_classification = ["silly", "campy", "quirky", "whimsical", "witty", "wry"]
160 #tag_classification = ["fiery","tense","anxious","intense", "volatile","visceral"]
161 #tag_classification = ["poignant","wistful", "bittersweet", "autumnal", "brooding"]
162 #tag_classification = ["rousing", "confident","boisterous", "rowdy"]
163 #tag_classification = ["sweet", "amiable","good natured"]
164 
165 
166 track = network.track
167 
168 # search for similar tag according to the seeds in tag_classification
169 for tags_name in tag_classification:
170  tags = network.search_for_tag(tags_name)
171  # filtering the results
172  tags_search = tags.get_next_page()
173  tag_file_name = tags_name + "_tags_results.txt"
174 
175  # print the tag name, i.e happy_tag_results.txt
176  print(tag_file_name)
177 
178  print("successfully written in the file")
179 
180  tag_data = open(tag_file_name, 'wb') # open and write the file for top tags
181 
182  os.makedirs(str(tags_name))
183  os.chdir('./' + str(tags_name) + '/')
184 
185  # write the similar with a topic
186  for each_letter in tags_search:
187  # store the each lexical into the file. i.e 'happy' 'very happy'
188  tag_data.write("%s\n" % each_letter)
189  # create a directory with tag name
190  os.makedirs(str(each_letter))
191  os.chdir('./' + str(each_letter) + '/')
192 
193  # store each lexical tag as a tag_object
194  tag_object = network.get_tag(each_letter)
195  tracks_results = tag_object.get_top_tracks()
196 
197  track_file_name = str(each_letter) + "_results.txt"
198  tag_track_file = open(track_file_name,'wb') # open and write the file for tracks with tag
199 
200  print(track_file_name)
201 
202  # write the tracks result with special tags
203  for each_line in tracks_results:
204  tag_track_file.write("%s%s\n" % each_line)
205 
206  tag_track_file.close()
207 
208  artist_file_name = str(each_letter) + "_artist.txt"
209  title_file_name = str(each_letter) + "_title.txt"
210  #id_file_name = str(each_letter) + "_id.txt"
211 
212  # open the track info file
213  newdata = open(track_file_name)
214  artist_data = open(artist_file_name,'wb')
215  title_data = open(title_file_name,'wb')
216  #id_file = open(id_file_name,"wb")
217 
218 
219  for each_row in newdata:
220 
221  (artist,title) = each_row.split('-',1)
222  title = title.strip() #delete the extra space
223  title = title.rstrip('0') #get rid of the 0 at the end
224 
225  artist_data.write("%s\n" % artist)
226  title_data.write("%s\n" % title)
227 
228  print artist
229  print title
230 
231  try:
232  (tracktitle,trackid) = get_trackid_from_text_search(title,artist)
233 
234  if trackid is not None:
235  # fetch the url
236  audio_url = get_preview_from_trackid(trackid)
237  print(audio_url)
238  # fetch the audio and save in the correct folder
239 
240  file_name = tracktitle + u'.wav'
241  file_name = file_name.replace(u'/', u' ')
242  #file_name = t.title + u'.mp3'
243  print file_name
244  print("downloading")
245 
246  # download_path = os.path.join(file_path, file_name)
247  path_name = './' + file_name
248  mp3 = download_file(audio_url, path_name)
249  except:
250  pass
251 
252 
253 
254  # id_file_name.close()
255  artist_data.close()
256  title_data.close()
257  newdata.close()
258  parent_path = '../'
259  os.chdir(parent_path)
260 
261  os.chdir(parent_path)
262 
263 tag_data.close()
264 
265 # check the directory, write to the current directory,
266 # if it doens't exist, create a new one
def download_file(file_url, file_name)
def get_preview_from_trackid(trackid)
def get_trackid_from_text_search(title, artistname='')
def url_call(url)
int len