Mercurial > code > home > repos > homeauto
changeset 831:bbecee3d287d
barcode player send commands to pympd
Ignore-this: 8500b91697c9b77ea1e24009f062d61
darcs-hash:20120418044932-312f9-0a1db0281966e714a05b52c251432c84b3eb4f7b.gz
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Tue, 17 Apr 2012 21:49:32 -0700 |
parents | 6860694eb19a |
children | 0489c17be95e |
files | service/starArduino/barcodePlayer.py |
diffstat | 1 files changed, 18 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/service/starArduino/barcodePlayer.py Tue Apr 17 21:49:12 2012 -0700 +++ b/service/starArduino/barcodePlayer.py Tue Apr 17 21:49:32 2012 -0700 @@ -5,31 +5,39 @@ from __future__ import division -import cyclone.web, cyclone.httpclient, sys, json +import cyclone.web, cyclone.httpclient, sys, json, urllib from twisted.python import log from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks sys.path.append("/my/proj/homeauto/lib") from cycloneerr import PrettyErrorHandler - - +from pymongo import Connection +mpdPaths = Connection("bang", 27017)['barcodePlayer']['mpdPaths'] class Index(PrettyErrorHandler, cyclone.web.RequestHandler): def get(self): - - self.set_header("Content-Type", "application/xhtml+xml") - self.write(open("index.html").read()) + self.write("barcode player. POST to /barcodeScan") class BarcodeScan(PrettyErrorHandler, cyclone.web.RequestHandler): @inlineCallbacks def post(self): - print json.loads(self.request.body) + code = json.loads(self.request.body) - song = "cd/Kindermusik-The_Best_of_the_Best/14.Fiddle-de-dee.ogg" + if not code['code'].startswith("music "): + raise ValueError("this service only knows music barcodes, not %r" % + code) - print (yield cyclone.httpclient.fetch(url="http://star:9009/addAndPlay/%s" % song, method="POST")).body + rows = list(mpdPaths.find({'_id' : int(code['code'].split()[1])})) + if not rows: + raise ValueError("code %r unknown" % code) + + song = rows[0]['mpdPath'] - self.write("ok") + post = "http://star:9009/addAndPlay/%s" % urllib.quote(song, safe='') + result = (yield cyclone.httpclient.fetch( + method="POST", url=post)).body + print result + self.write(result) if __name__ == '__main__':