[转载]使用python上传图片到 yeelink

时间:2021-08-14 04:55:52
#/bin/env python
# -*-coding:utf=8 -*-
import os,time,subprocess,shlex
import urllib2
def upload_yeelink(image_name, log_file):
#自己设备的 upload url
url = 'http://api.yeelink.net/v1.0/device/XXX/sensor/XXXX/photos'
length = os.path.getsize(image_name)
image_data = open(image_name, 'rb')
request = urllib2.Request(url, data=image_data)
request.add_header('U-ApiKey', '14765d*********880398486d08f9c')
request.add_header('Content-Length', '%d' % length)
res = urllib2.urlopen(request).read().strip()
log_file.write(res + '\n') if __name__ == '__main__':
images_path = os.path.join(os.getcwd(), 'image')
log = open(os.path.join(os.getcwd(), 'output.log'),'w+')
if not os.path.exists(images_path):
os.makedirs(images_path)
com_line = 'fswebcam -d /dev/video0 -r 320x240 --bottom-banner --title "%s" --no-timestamp %s/%s.jpg'
while True:
time_now = time.strftime('%Y-%m-%d-%H-%M-%S')
com_line_now = com_line % (time_now, images_path, time_now)
subprocess.call(shlex.split(com_line_now), stdout=log, stderr=log)
upload_yeelink('%s/%s.jpg' % (images_path, time_now), log)
print com_line_now
time.sleep(11)

代码来自于 :https://github.com/coolbit-in/yeelink_photos_upload