Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use YQL to return track data upon receiving a tweet with #Nowplaying #openhackindia
import cgi
import datetime
import wsgiref.handlers
import urllib2
from BeautifulSoup import BeautifulSoup
from google.appengine.ext import webapp
class MainPage(webapp.RequestHandler):
def post(self):
tweet = self.request.get('tweet')
if (tweet.split('#nowplaying')[0] == ''):
song = tweet.split('#nowplaying')[1].trim()
else:
song = tweet.split('#nowplaying')[0].trim()
YQLQuery = urllib2.quote('select ','')
YQLQuery = YQLQuery + '*'
YQLQuery = YQLQuery + urllib2.quote(' from music.track.search where keyword=','')
YQLQuery = YQLQuery + '"'+ urllib2.quote(song,'') +'"'
url = 'http://query.yahooapis.com/v1/public/yql?q='+YQLQuery + '&diagnostics=true&env='
url = url + urllib2.quote('store://datatables.org/alltableswithkeys','')
request = urllib2.Request(url)
request.add_header('User-Agent','LWLater 1.0')
opener = urllib2.build_opener()
YQLResp = opener.open(request).read()
soupXml = BeautifulSoup(YQLResp)
track = soupXml.findAll('track')[0]
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(track)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment