Created
July 1, 2015 08:45
-
-
Save ivlevdenis/bbddfa3d541d5b93cdd3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from base64 import b64encode | |
from datetime import datetime | |
from os.path import join | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
from kivy.network.urlrequest import UrlRequest | |
from subprocess import check_call | |
SNAPSHOT_API = u'/player/{0}/snapshot' | |
class Snapshot(object): | |
"""Snapshot class""" | |
def __init__(self, server, storage, uuid, success_callback=None): | |
self.on_success = success_callback if hasattr(success_callback, '__call__') else None | |
self.uuid = uuid | |
self.storage = join(storage, 'snapshots') | |
def _success_send_snapshot(self, req, results): | |
if self.success_callback is not None: | |
self.success_callback() | |
def do_shot(self): | |
new_fn = join(self.storage, datetime.now().strftime('%d.%m.%Y-%H:%M') + '.png') | |
snapshot_cmd = ['/usr/bin/raspi2png', '--pngname', new_fn] | |
result = check_call(snapshot_cmd) | |
if result == 0: | |
self.send(new_fn) | |
def send(self, fn): | |
"""Send snapshot image to server""" | |
body = self.make_json_post(fn) | |
url = SNAPSHOT_API.format(self.uuid) | |
resp = UrlRequest(url, on_success=self._success_send_snapshot, req_body=body) | |
def make_json_post(self, fn, data=None): | |
post_body = dict() | |
post_body['data'] = dict() | |
post_body['file'] = 'data:image/png;base64,' + b64encode(open(fn, 'rb').read()) | |
if data is not None and isinstance(data, dict): | |
post_body['data'] = data | |
return json.dumps(post_body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment