diff bin/vidref @ 941:1d9547f90737

vidref can take snapshots and serve them back Ignore-this: 9e89659abc5f36aa56a08391e84e52e
author drewp@bigasterisk.com
date Thu, 13 Jun 2013 00:46:52 +0000
parents b0337e6f68f1
children b281ee32c785
line wrap: on
line diff
--- a/bin/vidref	Thu Jun 13 00:16:42 2013 +0000
+++ b/bin/vidref	Thu Jun 13 00:46:52 2013 +0000
@@ -2,14 +2,16 @@
 from run_local import log
 from twisted.internet import gtk2reactor
 gtk2reactor.install()
-from twisted.internet import reactor
+from twisted.internet import reactor, defer
 import gobject
 gobject.threads_init()
 import gtk
-import sys, logging, optparse
+import sys, logging, optparse, json
 sys.path.append(".")
+from light9 import networking
 from light9.vidref.main import Gui
-
+from light9.vidref.replay import snapshotDir
+import cyclone.web, cyclone.httpclient, cyclone.websocket
 
  # find replay dirs correctly. show multiple
  # replays. trash. reorder/pin. dump takes that are too short; they're
@@ -27,8 +29,38 @@
 logging.getLogger("restkit.client").setLevel(logging.WARN)
 
 
+class Snapshot(cyclone.web.RequestHandler):
+    @defer.inlineCallbacks
+    def post(self):
+        # save next pic
+        # return /snapshot/path
+        try:
+            outputFilename = yield self.settings.gui.snapshot()
+
+            assert outputFilename.startswith(snapshotDir())
+            out = networking.vidref.path(
+                "snapshot/%s" % outputFilename[len(snapshotDir()):].lstrip('/'))
+            
+            self.write(json.dumps({'snapshot': out}))
+            self.set_header("Location", out)
+            self.set_status(303)
+        except Exception as e:
+            import traceback
+            traceback.print_exc()
+            raise
+
+class SnapshotPic(cyclone.web.StaticFileHandler):
+    pass
+
         
-start=Gui()
+gui = Gui()
+
+port = networking.vidref.port
+reactor.listenTCP(port, cyclone.web.Application(handlers=[
+    (r'/snapshot', Snapshot),
+    (r'/snapshot/(.*)', SnapshotPic, {"path": snapshotDir()}),
+    ], debug=True, gui=gui))
+log.info("serving on %s" % port)
 
 reactor.run()