musixmatch-master/musixmatch.py
Go to the documentation of this file.
1 import os
2 import sys
3 
4 import musixmatch
5 import musixmatch.ws
6 import json
7 sys.path.append("7digital-python/lib/")
8 import py7digital
9 
10 import urllib2 #call url function
11 from xml.dom import minidom
12 
13 
14 #apikey = '8496dd1c715c69a74ef9fcde1716cf4a'
15 newapikey = '82be3ea3f79ea404d45f47607c103eff'
16 
17 #chart = musixmatch.ws.track.chart.get(country='it', apikey=apikey)
18 
19 #lyrics = musixmatch.ws.matcher.lyrics.get(q_track='Someone like you', q_artist='Adele',format ='json',apikey=newapikey)
20 
21 song_wakeMeUp = musixmatch.ws.track.search(q_lyrics = 'wake me up',f_lyrics_language='en',s_track_rating='desc',format ='json',apikey=newapikey)
22 
23 
24 #print lyrics
25 
26 #print lyrics['body']
27 
28 #print pprint(song_wakeMeUp)
29 
30 
31 #for each_line in lyrics['body']:
32 # print each_line
33 
34 #print 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
35 
36 #for each_item in chart['body']['track_list']:
37 # print each_item
38 
39 # 7 digital API key
40 DIGITAL7_API_KEY = '7dbpa63h3y3d'
41 
42 def url_call(url):
43  """
44  ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
45  Do a simple request to the 7digital API
46  We assume we don't do intense querying, this function is not robust
47  Return the answer as na xml document
48  """
49  stream = urllib2.urlopen(url)
50  xmldoc = minidom.parse(stream).documentElement
51  stream.close()
52  return xmldoc
53 
54 
55 def download_file(file_url, file_name):
56  # open the url
57  mp3file = urllib2.urlopen(file_url)
58 
59  # open the local file for writing
60  local_file = open(file_name, "wb")
61  # write to file
62  local_file.write(mp3file.read())
63  local_file.close()
64 
65 """"*********************************************************************************"""
66 
67 def get_trackid_from_text_search(title,artistname=''):
68  """
69  ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
70  Search for an artist + title using 7digital search API
71  Return None if there is a problem, or tuple (title,trackid)
72  """
73  url = 'http://api.7digital.com/1.2/track/search?'
74  url += 'oauth_consumer_key='+DIGITAL7_API_KEY
75  query = title
76  if artistname != '':
77  query = artistname + ' ' + query
78  query = urllib2.quote(query)
79  url += '&q='+query
80  xmldoc = url_call(url)
81  status = xmldoc.getAttribute('status')
82  if status != 'ok':
83  return None
84  resultelem = xmldoc.getElementsByTagName('searchResult')
85  if len(resultelem) == 0:
86  return None
87  track = resultelem[0].getElementsByTagName('track')[0]
88  tracktitle = track.getElementsByTagName('title')[0].firstChild.data
89  trackid = int(track.getAttribute('id'))
90  return (tracktitle,trackid)
91 
92 
94  """
95  ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
96  Ask for the preview to a particular track, get the XML answer
97  After calling the API with a given track id,
98  we get an XML response that looks like:
99 
100  <response status="ok" version="1.2" xsi:noNamespaceSchemaLocation="http://api.7digital.com/1.2/static/7digitalAPI.xsd">
101  <url>
102  http://previews.7digital.com/clips/34/6804688.clip.mp3
103  </url>
104  </response>
105 
106  We parse it for the URL that we return, or '' if a problem
107  """
108  url = 'http://api.7digital.com/1.2/track/preview?redirect=false'
109  url += '&trackid='+str(trackid)
110  url += '&oauth_consumer_key='+DIGITAL7_API_KEY
111  xmldoc = url_call(url)
112  status = xmldoc.getAttribute('status')
113  if status != 'ok':
114  return ''
115  urlelem = xmldoc.getElementsByTagName('url')[0]
116  preview = urlelem.firstChild.nodeValue
117  return preview
118 
119 
120 for each_song in song_wakeMeUp['body']['track_list']:
121  print each_song['track']['track_name'] + '\t' + each_song['track']['artist_name']
122 
123  (tracktitle, trackid) = get_trackid_from_text_search(each_song['track']['track_name'],each_song['track']['artist_name'])
124 
125  audio_url = get_preview_from_trackid(trackid)
126 
127  file_name = tracktitle + u'.mp3'
128  file_name = file_name.replace(u'/', u' ')
129 
130  #file_name = t.title + u'.mp3'
131  print file_name
132  print("downloading")
133  # download_path = os.path.join(file_path, file_name)
134  path_name = './' + file_name
135  mp3 = download_file(audio_url, path_name)
136 
def download_file(file_url, file_name)
def get_preview_from_trackid(trackid)
def get_trackid_from_text_search(title, artistname='')
int len