Skip to content

Instantly share code, notes, and snippets.

@TonsOfFun
Created January 14, 2019 00:13
Show Gist options
  • Save TonsOfFun/82ea4da13403ce9f1095337927856d45 to your computer and use it in GitHub Desktop.
Save TonsOfFun/82ea4da13403ce9f1095337927856d45 to your computer and use it in GitHub Desktop.
Camera Capture
# USAGE
# python save_key_events.py --output output
# import the necessary packages
from greenthumb.note import Note
from uuid import getnode as get_mac
from io import BytesIO
from picamera import PiCamera
import datetime
import imutils
from time import sleep
# initialize the video stream and allow the camera sensor to
# warmup
frame = BytesIO()
camera = PiCamera()
camera.resolution = (3280, 2464)
print("[INFO] warming up camera...")
sleep(2.0)
profile = '@greenthumb/cam' + hex(get_mac())[-4:]
print("[INFO] capturing image...")
camera.capture(frame, 'jpeg')
print("[INFO] closing camera...")
camera.close()
Note(host='http://greenthumb-rails.herokuapp.com',
title='This is an automated upload',
frame=frame,
user_email='[email protected]',
user_token='hmcizxCzRymyA2rWgqTa').create_note(profile=profile)
import json
import re
import requests
class Note:
def __init__(self,
host=None,
title='',
body='',
frame=None,
user_email=None,
user_token=None):
self.host = host
self.title = title
self.body = body
self.frame = frame
self.user_email = user_email
self.user_token = user_token
def get_presigned_post(self):
get_presigned_post_url = self.host + '/notes/new.json?user_email=' + self.user_email + '&user_token=' + self.user_token
response = requests.get(get_presigned_post_url)
presigned_post_json = json.loads(response.text)
form_data = presigned_post_json['s3']['form-data']
url = presigned_post_json['s3']['url']
return url, form_data
def post_file(self, url, form_data):
files = {
'file': ('test.jpg', self.frame.getvalue(), 'image/jpeg')
}
response = requests.post(url, files=files, data=form_data)
url_re = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
return re.findall(url_re, response.text)[0].split('<')[0]
def create_note(self, file_url=None, profile=''):
print("[INFO] getting presigned request...")
url, form_data = self.get_presigned_post()
print("[INFO] uploading file...")
file_url = self.post_file(url, form_data)
form_data = {
'note[title]': self.title,
'note[body]': self.body + ' ' + profile,
'note[file_url]': file_url,
'user_email': self.user_email,
'user_token': self.user_token
}
print("[INFO] creating note...")
response = requests.post(self.host + '/notes', data=form_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment