-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
55 lines (40 loc) · 1.42 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import print_function
from os import getenv, path
from subprocess import call
import sys
import dropbox
DROPBOX_IMAGE_NAME = '/img.jpeg'
FSWEBCAM_CONFIG_NAME = 'fswebcam.config'
def error(*objs):
print(*objs, file=sys.stderr)
def generate(file_name='image.jpeg'):
directory = path.dirname(path.realpath(__file__))
config_path = path.join(directory, FSWEBCAM_CONFIG_NAME)
image_path = path.join(directory, file_name)
call('fswebcam --config {config} {image}'.format(config=config_path, image=image_path), shell=True)
return image_path
def upload(file_path):
access_token = getenv('DROPBOX_TOKEN')
if not access_token:
raise ValueError('DROPBOX_TOKEN not set')
client = dropbox.client.DropboxClient(access_token)
# Get metadata of the current version of image
try:
file_metadata = client.metadata(DROPBOX_IMAGE_NAME)
except dropbox.rest.ErrorResponse:
file_metadata = {}
parent_rev = file_metadata.get('rev')
# Upload new version of image
file_obj = open(file_path, 'rb')
try:
response = client.put_file(full_path=DROPBOX_IMAGE_NAME, file_obj=file_obj, parent_rev=parent_rev)
print('uploaded: ', response)
except dropbox.rest.ErrorResponse as e:
error('error: ', e.body)
finally:
file_obj.close()
def main():
file_path = generate()
upload(file_path)
if __name__ == '__main__':
main()