Mercurial > hg > pmhd
view start.py @ 9:ed610a0bbf83
Add files
author | Yading Song <yading.song@eecs.qmul.ac.uk> |
---|---|
date | Sat, 20 Apr 2013 20:12:26 +0200 |
parents | |
children | 6840f77b83aa |
line wrap: on
line source
#!/usr/bin/env python # encoding: utf-8 """ Created by Yading Song on 2013-04-21 for Music Hack Day in Paris Copyright (c) 2013 . All rights reserved. """ import os,sys,optparse,signal,time,math import cherrypy as cp from cherrypy.lib import static import subprocess as sp from subprocess import Popen as spopen """"*********************************************************************************""" from xml.dom import minidom import urllib2 #call url function import os import sys import cPickle import pyechonest # import echonest API from pyechonest import artist, catalog, config, playlist # How it works instead of pyechonest import pylast # import last.fm API #change the path to 7-digital python library sys.path.append("./7digital-python/lib/") import py7digital # this is just a parser for command line options for the server. op = optparse.OptionParser() op.add_option('-u', '--user', action="store", dest="USER", default="devel", type="str") options, args = op.parse_args() CONFIG_FILE = "%(USER)s.cfg" %options.__dict__ if not os.path.isfile(CONFIG_FILE) : print >> sys.stderr, "Config file not found: %s" %CONFIG_FILE sys.exit(-1) # This is the server side of the actual Web-application class ServerExample: def __init__(self): self.inputs = [] def index(self): return "" @cp.expose def receiveinput(self, userinput): print "\n\n >>>The user entered the text: %(userinput)s\n\n" %locals() tags = network.search_for_tag(userinput) tags_search = tags.get_next_page() # suppose we have got the keywords that user typed in # preset html_head = '''<html> <body> Here are the music <br /> ''' html_tail = '''<script type="text/javascript" src="http://mediaplayer.yahoo.com/js"></script><br /> <a href="javascript: history.go(-1)">Back</a> </body> </html>''' content = '''<h> You have chosen <b> %s </b> music" </h>''' %userinput audio_url = "" # choose the top 3 tags with the key word for item in tags_search[1:3]: tag_object = network.get_tag(item) track_info = tag_object.get_top_tracks() # choose the top 3 tracks with the chosen key word for tracks in track_info[1:5]: track_detail = ''' %s%s ''' %tracks track_detail = track_detail.strip().rstrip('0') artist, title = track_detail.split('-') # the title and the artist have been found try: _, trackid = get_trackid_from_text_search(title,artist) if trackid is not None: audio_url = get_preview_from_trackid(trackid) content = content + '''<p><a href=" %s ">''' % audio_url + '''<b> %s </b>''' % artist + '''<b> %s </b> </a></p><br /> ''' % title except: try: (tracktitle,trackid) = get_trackid_from_text_search(title,'') if trackid is not None: audio_url = get_preview_from_trackid(trackid) content = content + '''<p><a href=" %s ">''' % audio_url + '''<b> %s </b>''' % artist + '''<b> %s </b> </a></p><br /> ''' % title except: pass return html_head, content, html_tail @cp.expose def testpage(self): return "<html> <body> You have accessed the test page </body> </html>" """"*********************************************************************************""" 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 an xml document """ stream = urllib2.urlopen(url) xmldoc = minidom.parse(stream).documentElement stream.close() return xmldoc """"*********************************************************************************""" 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 """"*********************************************************************************""" 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 getProcessPids(port,kill=False): '''Get the pid of the offending Python process given a port after an unsuccessful restart.''' print "Running lsof -i :"+str(port)," ...\n\n" command = "lsof -i :"+str(port) w = spopen(command,stdout=sp.PIPE,stderr=sp.PIPE,shell=True) se = w.stderr.readlines() result = w.stdout.readlines() exitcode = w.wait() if not result : print "getProcessPid:: Unable to obtain process pid. (lsof returned nothing. exitcode: %s)" %str(exitcode) return False import pprint pprint.pprint(result) # get heading: ix = None head = result[0].upper() if 'PID' in head: head = filter(lambda x: x != str(), head.split(' ')) head = map(lambda x: x.strip().replace(' ',''), head) if 'PID' in head : ix = head.index('PID') # get process pid pids = [] for line in result : if 'python' in line.lower() : line = filter(lambda x: x != str(), line.split(' ')) line = map(lambda x: x.strip().replace(' ',''), line) try : if ix : pids.append(int(line[ix])) else: numbers = filter(lambda x: x.isdigit(), line) pids.append(int(numbers[0])) except: print 'Error parsing lsof results.' return False print 'Pids found: ',pids # kill if specified if kill : pids_killed = [] import signal for pid in pids: print 'Killing process: ',pid try : os.kill(pid,signal.SIGKILL) pids_killed.append(pid) except : print 'Failed: ',pid if pids_killed : print 'Processes killed:',pids_killed,' Waiting 10 seconds...' import time time.sleep(10) return True return False def main(argv=None): # Configure and start cp.config.update(CONFIG_FILE) cp.config.update({'tools.staticdir.root': os.getcwd()}) cp.tree.mount(ServerExample(),script_name="/yading",config=CONFIG_FILE) port = int(cp.server.socket_port) ip = cp.server.socket_host print "Trying to bind: %(ip)s:%(port)s" %locals() getProcessPids(port,kill=True) cp.quickstart() if __name__ == "__main__": main()