Mercurial > code > home > repos > homeauto
view service/starArduino/barcodePlayer.py @ 400:1b303e7542a3
start the next python rewrite of rfid pm_532 reader. Nim didn'
Ignore-this: b6a98b3f0f1c4147bedcc5d2bcfec226
t go well with asynchttpserer adn hreaind
author | drewp@bigasterisk.com |
---|---|
date | Wed, 27 Feb 2019 10:23:36 -0800 |
parents | ca380454d176 |
children |
line wrap: on
line source
#!bin/python """ receives POSTs about barcodes that are scanned, plays songs on mpd """ from __future__ import division import cyclone.web, cyclone.httpclient, sys, json, urllib from twisted.python import log as twlog from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks sys.path.append("/my/proj/homeauto/lib") from cycloneerr import PrettyErrorHandler from logsetup import log from pymongo import Connection mpdPaths = Connection("bang", 27017)['barcodePlayer']['mpdPaths'] class Index(PrettyErrorHandler, cyclone.web.RequestHandler): def get(self): self.write("barcode player. POST to /barcodeScan") class BarcodeScan(PrettyErrorHandler, cyclone.web.RequestHandler): @inlineCallbacks def post(self): code = json.loads(self.request.body) if not code['code'].startswith("music "): raise ValueError("this service only knows music barcodes, not %r" % code) rows = list(mpdPaths.find({'_id' : int(code['code'].split()[1])})) if not rows: raise ValueError("code %r unknown" % code) song = rows[0]['mpdPath'] post = "http://star:9009/addAndPlay/%s" % urllib.quote(song, safe='') result = (yield cyclone.httpclient.fetch( method="POST", url=post)).body log.info("post result: %r", result) self.write(result) if __name__ == '__main__': app = cyclone.web.Application([ (r'/', Index), (r'/barcodeScan', BarcodeScan), ], ) twlog.startLogging(sys.stdout) reactor.listenTCP(9011, app) reactor.run()