view musixmatch-master/musixmatch.py @ 10:6840f77b83aa

commit
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Sun, 21 Apr 2013 10:55:35 +0200
parents 8c29444cb5fd
children
line wrap: on
line source
import os 
import sys

import musixmatch 
import musixmatch.ws 
import json
sys.path.append("7digital-python/lib/")
import py7digital

import urllib2 #call url function
from xml.dom import minidom


#apikey = '8496dd1c715c69a74ef9fcde1716cf4a'
newapikey = '82be3ea3f79ea404d45f47607c103eff'

#chart = musixmatch.ws.track.chart.get(country='it', apikey=apikey)
	
#lyrics = musixmatch.ws.matcher.lyrics.get(q_track='Someone like you', q_artist='Adele',format ='json',apikey=newapikey)

song_wakeMeUp = musixmatch.ws.track.search(q_lyrics = 'wake me up',f_lyrics_language='en',s_track_rating='desc',format ='json',apikey=newapikey)


#print lyrics

#print lyrics['body']

#print pprint(song_wakeMeUp)


#for each_line in lyrics['body']:
#	print each_line

#print 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

#for each_item in chart['body']['track_list']:
#	print each_item 

# 7 digital API key
DIGITAL7_API_KEY = '7dbpa63h3y3d'

def url_call(url):
    """
    ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
    Do a simple request to the 7digital API
    We assume we don't do intense querying, this function is not robust
    Return the answer as na xml document
    """
    stream = urllib2.urlopen(url)
    xmldoc = minidom.parse(stream).documentElement
    stream.close()
    return xmldoc


def download_file(file_url, file_name):
    # open the url
    mp3file = urllib2.urlopen(file_url)   
    
    # open the local file for writing
    local_file = open(file_name, "wb")
    # write to file
    local_file.write(mp3file.read())
    local_file.close()

""""*********************************************************************************"""

def get_trackid_from_text_search(title,artistname=''):
    """
        ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
        Search for an artist + title using 7digital search API
        Return None if there is a problem, or tuple (title,trackid)
        """
    url = 'http://api.7digital.com/1.2/track/search?'
    url += 'oauth_consumer_key='+DIGITAL7_API_KEY
    query = title
    if artistname != '':
        query = artistname + ' ' + query
    query = urllib2.quote(query)
    url += '&q='+query
    xmldoc = url_call(url)
    status = xmldoc.getAttribute('status')
    if status != 'ok':
        return None
    resultelem = xmldoc.getElementsByTagName('searchResult')
    if len(resultelem) == 0:
        return None
    track = resultelem[0].getElementsByTagName('track')[0]
    tracktitle = track.getElementsByTagName('title')[0].firstChild.data
    trackid = int(track.getAttribute('id'))
    return (tracktitle,trackid)  


def get_preview_from_trackid(trackid):
    """
        ***This method is from get_preview_url.py by Thierry Bertin-Mahieux***
        Ask for the preview to a particular track, get the XML answer
        After calling the API with a given track id,
        we get an XML response that looks like:
        
        <response status="ok" version="1.2" xsi:noNamespaceSchemaLocation="http://api.7digital.com/1.2/static/7digitalAPI.xsd">
        <url>
        http://previews.7digital.com/clips/34/6804688.clip.mp3
        </url>
        </response>
        
        We parse it for the URL that we return, or '' if a problem
        """
    url = 'http://api.7digital.com/1.2/track/preview?redirect=false'
    url += '&trackid='+str(trackid)
    url += '&oauth_consumer_key='+DIGITAL7_API_KEY
    xmldoc = url_call(url)
    status = xmldoc.getAttribute('status')
    if status != 'ok':
        return ''
    urlelem = xmldoc.getElementsByTagName('url')[0]
    preview = urlelem.firstChild.nodeValue
    return preview   


for each_song in song_wakeMeUp['body']['track_list']:
	print each_song['track']['track_name'] + '\t' + each_song['track']['artist_name']
	
	(tracktitle, trackid) = get_trackid_from_text_search(each_song['track']['track_name'],each_song['track']['artist_name'])
	
	audio_url = get_preview_from_trackid(trackid)
    
	file_name = tracktitle + u'.mp3'
	file_name = file_name.replace(u'/', u' ')
    
	#file_name = t.title + u'.mp3'  
	print file_name
	print("downloading")                
 	# download_path = os.path.join(file_path, file_name)
	path_name = './' + file_name
	mp3 = download_file(audio_url, path_name)