Commits

clach04 committed b829262 Draft

Initial single file serve, showing QR code to make it easier for phones/tablets to download served file.

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+# -*- coding: us-ascii -*-
+# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
+#
+"""Serve a single file, showing a QR code of the shared file URL.
+Uses Google chart API to generate URL and then opens that URL (which
+requires an internet connection).
+
+Alternatives are PIL/SVG generators:
+
+  * http://code.google.com/p/pyqrcode/
+
+or javascript generator opened in local browser:
+
+  * http://davidshimjs.github.io/qrcodejs/
+  * https://github.com/jeromeetienne/jquery-qrcode
+"""
+
+import os
+import sys
+import webbrowser
+import urllib
+
+import woof
+
+
+def gen_url(url, image_size=547):
+    """Construct QR generator google URL with max size, from:
+    
+    https://chart.googleapis.com/chart? - All infographic URLs start with this root URL, followed by one or more parameter/value pairs. The required and optional parameters are specific to each image; read your image documentation.
+        chs - Size of the image in pixels, in the format <width>x<height>
+        cht - Type of image: 'qr' means QR code.
+        chl - The data to encode. Must be URL-encoded.
+    
+    See https://google-developers.appspot.com/chart/infographics/docs/overview
+    """
+    url = urllib.quote(url)
+    image_size_str = '%dx%d' % (image_size, image_size)
+    result = 'https://chart.googleapis.com/chart?cht=qr&chs=%s&chl=%s' % (image_size_str, url)
+    return result
+
+
+def main(argv=None):
+    if argv is None:
+        argv = sys.argv
+
+    filename = argv[1]
+
+    ip_addr = woof.find_ip()
+    port = 8080
+    url_str = "http://%s:%s/" % (ip_addr, port)
+    print url_str
+    url_str = gen_url(url_str)
+    print url_str
+    webbrowser.open(url_str)
+    woof.serve_files(filename, port=port)
+    
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())