EE-downloader-v3/download_worker.py

153 lines
4.0 KiB
Python
Raw Normal View History

import requests
import json
import glob
import sys
import os
from multiprocessing import Process, Pool
import logging
logger = logging.getLogger()
logger.setLevel('DEBUG')
from EagleEyev3 import *
from settings import config
import sqlite3
con = sqlite3.connect('instance/project.db')
def run(args):
# iterate through the first argument, appending the second and third to each iteration
args = [([i], args[1], args[2]) for i in args[0]]
pool.map(download, args, 1)
def download(args):
# explode arguments into variable names
row, een_obj, cam = args
row = row[0]
camera_device_id = row[2]
start = row[3]
end = row[4]
start = datetime.fromisoformat(start)
os.makedirs(f"videos/{camera_device_id}/{start.year}/{start.month}/{start.day}/", exist_ok=True)
fname = f"videos/{camera_device_id}/{start.year}/{start.month}/{start.day}/{camera_device_id}_{start}-{end}.mp4"
save_result = cam.save_video_to_file(url=row[1], filename=fname)
match save_result['response_http_status']:
case 200:
save_status = 'done'
case 400 | 404 | 409:
save_status = 'client failure'
case 401 | 403:
save_status = 'auth failure'
case 500 | 502 | 503 | 504:
save_status = 'cloud failure'
case _:
save_status = 'unknown failure'
new_cur = con.cursor()
new_cur.execute("update download set status = ?, error = ? where download.id == ?;", (save_status, save_result['response_http_status'], row[0]))
con.commit()
return (save_result['response_http_status'], row[0])
if __name__ == '__main__':
pool = Pool(64)
print("starting up...")
een = EagleEyev3(config)
logging.info(f"EagleEyev3 version: {een.__version__}")
cur = con.cursor()
if een:
if een.refresh_token == None or een.refresh_token == '':
# if you get out of sync, pull the refresh_token from the db to get the loop started
result = cur.execute("select user.refresh_token from user where user.email = ?;", ("mcotton@mcottondesign.com",))
for row in result:
een.refresh_token = row[0]
else:
# een object and refresh_token appear to be good
pass
else:
logging.error('een object is None')
een.login_tokens(code=None, cascade=True, refresh_token=een.refresh_token)
result = cur.execute("select user.refresh_token, user.id from user where user.email = ?;", (een.current_user['email'],))
for row in result:
print(f"found user {een.current_user['email']}, updating refresh_token")
print(row)
print(een.refresh_token)
print(f"update user set refresh_token = {een.refresh_token} where id == {row[1]};")
cur.execute("update user set refresh_token = ? where id == ?;", (een.refresh_token, row[1]))
con.commit()
een.get_list_of_cameras()
# iterate through all the cameras this user has access to
for current_camera in een.cameras:
# create directories if they don't already exists
os.makedirs(f"videos/{current_camera.id}/", exist_ok=True)
# r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*1), end_timestamp=een.time_now())
sql = '''SELECT
download.id,
download.url,
camera.device_id,
download.start_timestamp,
download.end_timestamp,
download.status
FROM
camera
JOIN download ON download.camera_id = camera.id
JOIN USER ON camera.user_id = user.id
WHERE
download.status == ?
AND camera.device_id == ?
ORDER BY download.start_timestamp
LIMIT 10000;'''
results = cur.execute(sql, ('new', current_camera.id))
run([results, een, current_camera])
pool.close()
print("Shutting down...")