-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
54 lines (48 loc) · 1.72 KB
/
script.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
#!/usr/bin/python3
import os
import requests
import datetime
import ffmpeg
import argparse
import subprocess
from PIL import Image
from dotenv import load_dotenv
from upload import upload_video
load_dotenv()
camera_snapshot_url = os.getenv('CAMERA_SNAPSHOT_URL')
parser = argparse.ArgumentParser(description='video-memory')
parser.add_argument('--output', type=str, default='./data',
help='define snapshots output folder')
args = parser.parse_args()
if not os.path.exists(args.output):
os.makedirs(args.output)
else:
now = datetime.datetime.now()
response = requests.get(camera_snapshot_url, stream=True)
if response.status_code == 200:
# Format "YYYY-MM-DD-HH-MM-SS.jpg"
filename = now.strftime('%Y-%m-%d-%H-%M-%S.jpg')
filepath = os.path.join(
args.output, now.strftime('%d-%m-%Y'), filename)
# Create date args.output
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
# Save file
with open(filepath, 'wb') as f:
f.write(response.content)
print('File saved ', filename)
try:
img = Image.open(os.path.join(args.output, now.strftime(
'%d-%m-%Y'), filename))
img.verify()
except Exception as e:
print(e)
os.remove(os.path.join(args.output, now.strftime(
'%d-%m-%Y'), filename))
else:
print('Error fetching snapshot.')
# Merge photos
if now.hour == 23 and now.minute >= 58 and now.second >= 0:
folder_path = os.path.join(args.output, now.strftime('%d-%m-%Y'))
subprocess.run(["python3", "compiler.py", '--source',
folder_path])