checking for duplicates and existing files

database
Mark Cotton 2023-09-20 21:25:32 -05:00
parent c5a8280b19
commit 58d6332748
2 changed files with 27 additions and 17 deletions

8
app.py
View File

@ -410,10 +410,14 @@ def camera_list_of_videos(esn=None):
for v in camera.videos: for v in camera.videos:
try: try:
check_video = Download.query.filter(Download.camera_id == db_camera.id)\ check_video = Download.query.filter(Download.camera_id == db_camera.id)\
.filter(Download.start_timestamp == v['startTimestamp'])\ .filter(Download.start_timestamp == datetime.fromisoformat(v['startTimestamp']))\
.first() .first()
if check_video is None: if check_video is None:
logging.debug(f"creating a new record for {db_camera.id} {v['startTimestamp']}")
new_video = Download(url=v['mp4Url'],\ new_video = Download(url=v['mp4Url'],\
camera_id=db_camera.id,\ camera_id=db_camera.id,\
start_timestamp=datetime.fromisoformat(v['startTimestamp']),\ start_timestamp=datetime.fromisoformat(v['startTimestamp']),\
@ -422,7 +426,7 @@ def camera_list_of_videos(esn=None):
error=None) error=None)
new_video.save() new_video.save()
except Exception as e: except Exception as e:
logging.warn(f"saving videos to db: {e}") logging.warn(f"Exception saving videos to db: {e}")
values = { values = {
"current_user": een.current_user, "current_user": een.current_user,

View File

@ -54,27 +54,33 @@ def download(args):
fname = f"{path}/{camera_device_id}_{start}-{end}.mp4" fname = f"{path}/{camera_device_id}_{start}-{end}.mp4"
if os.path.isfile(fname) == False:
save_result = cam.save_video_to_file(url=row[1], filename=fname)
save_code = save_result['response_http_status']
save_result = cam.save_video_to_file(url=row[1], filename=fname) match save_code:
case 200:
match save_result['response_http_status']: save_status = 'done'
case 200: case 400 | 404 | 409:
save_status = 'done' save_status = 'client failure'
case 400 | 404 | 409: case 401 | 403:
save_status = 'client failure' save_status = 'auth failure'
case 401 | 403: case 500 | 502 | 503 | 504:
save_status = 'auth failure' save_status = 'cloud failure'
case 500 | 502 | 503 | 504: case _:
save_status = 'cloud failure' save_status = 'unknown failure'
case _:
save_status = 'unknown failure' else:
# file exists, don't do anything and mark as done
save_status = 'already exists'
save_code = None
new_cur = con.cursor() new_cur = con.cursor()
new_cur.execute("update download set status = ?, error = ? where download.id == ?;", (save_status, save_result['response_http_status'], row[0])) new_cur.execute("update download set status = ?, error = ? where download.id == ?;", (save_status, save_code, row[0]))
con.commit() con.commit()
return (save_result['response_http_status'], row[0]) return (save_code, row[0])