Last active
March 2, 2016 20:45
-
-
Save speg/5d23cf2f65f97441c6a0 to your computer and use it in GitHub Desktop.
Simple Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
""" | |
Image capture command: | |
imagesnap -w 5 -t 120 | |
fswatch -0 -d -Ee "\.jpg.+" --event Created . | xargs -0 -n 1 -I {} sips --resampleHeight 250 {} --out processed | |
""" | |
import http.server | |
import socketserver | |
import os | |
import json | |
PORT = 4040 | |
Handler = http.server.SimpleHTTPRequestHandler | |
class SpyHandler(Handler): | |
def do_GET(self): | |
if self.path.endswith('.json'): | |
self.send_response(200) | |
self.send_header('Content-type', 'application/json') | |
self.end_headers() | |
path = self.translate_path('images/processed') | |
scan = os.scandir(path) # os.scandir is new in Python 3.5 – you can use other methods (listdir) if you don't want to use Python 3.5 | |
files = filter(lambda x: x.name.endswith('.jpg'), scan) | |
files = sorted(files, key=lambda x: x.stat().st_mtime) | |
output = json.dumps([f.name for f in files[-30:]]) | |
self.wfile.write(bytes(output, "UTF-8")) | |
else: | |
super().do_GET() | |
httpd = socketserver.TCPServer(("", PORT), SpyHandler) | |
print("Spying on port {}".format(PORT)) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment