|
#!/usr/bin/env python3 |
|
import asyncio |
|
import requests |
|
import base64 |
|
import sys |
|
import random |
|
|
|
from blinkpy.blinkpy import Blink |
|
from blinkpy.auth import Auth |
|
from blinkpy.helpers.util import json_load |
|
from os import path |
|
from datetime import datetime |
|
|
|
##### put your camera(s) and PrusaConnect tokens in this table for upload |
|
# you get these tokens from the PrusaConnect UI |
|
# this should be in a config file somewhere like /etc |
|
upload_list = [ |
|
('<prusa-connect-token-1>', '<Camera 1 Name>'), |
|
('<prusa-connect-token-2>', '<Camera 2 Name>'), |
|
('<prusa-connect-token-3>', '<Camera 3 Name>'), |
|
] |
|
|
|
DATETIME = str(datetime.now()) |
|
|
|
async def upload(file_path, prusa_connect_token): |
|
URL = 'https://webcam.connect.prusa3d.com/c/snapshot' |
|
##### put anything unique to you here |
|
FINGERPRINT = base64.b64encode(b'<any unique value here>') |
|
|
|
headers = { |
|
'Content-Type': 'image/jpg', |
|
'fingerprint': FINGERPRINT, |
|
'token': prusa_connect_token, |
|
} |
|
|
|
response = requests.put(URL, headers=headers, data=open(file_path, mode='rb')) |
|
|
|
if response.ok: |
|
print('Image sent successfully at ' + DATETIME) |
|
else: |
|
print('Error sending response at' + DATETIME + ': {}', response.text) |
|
|
|
|
|
async def start(): |
|
global camera_name |
|
blink = Blink() |
|
script_dir = path.dirname(__file__) |
|
##### blink auth credentials are pulled form the blink.creds file in the same folder |
|
# as this tool, I forget how I generated that file. It is a json dump of the |
|
# return value form "auth" in blinkpy. |
|
cred_path = path.join(script_dir, 'blink.creds') |
|
auth = Auth(await json_load(cred_path)) |
|
blink.auth = auth |
|
await blink.start() |
|
|
|
names = blink.cameras.keys() |
|
print('Available cameras: ' + ', '.join(names)) |
|
|
|
for token, cam_name in upload_list: |
|
print('Using the camera "%s"' % cam_name) |
|
if cam_name in blink.cameras: |
|
selected_camera = blink.cameras[cam_name] |
|
await selected_camera.snap_picture() # Take a new picture with the camera |
|
await blink.refresh() # Get new information from server |
|
# I put the temp image files in /tmp by token, should use real temp-file os calls |
|
image_tmp_path = "/tmp/blink-" + token + "-image.jpg" |
|
await selected_camera.image_to_file(image_tmp_path) |
|
await upload(image_tmp_path, token) |
|
else: |
|
print('Camera not found: "' + cam_name + '"') |
|
return blink |
|
|
|
blink = asyncio.run(start()) |
|
|