Created
March 3, 2024 02:54
-
-
Save codykonior/b6617e2ce0c42cc4d387c3e487b72abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import time | |
import datetime | |
import os.path | |
INTERVAL_TIME = 10 | |
HOST = 'https://...' | |
CAMERA_NAME = 'Mango' | |
# Set up an offline username/password in UniFi with Protect read permissions | |
USERNAME = '...' | |
PASSWORD = '...' | |
requests.packages.urllib3.disable_warnings() | |
session = requests.Session() | |
login = session.post(HOST + '/api/auth/login', data={'username': USERNAME, 'password': PASSWORD, 'remember': True}, verify=False) | |
if login.status_code != 200: | |
raise | |
start_time = int(time.mktime(datetime.date.today().timetuple())) * 1000 | |
end_time = int(time.mktime((datetime.date.today() + datetime.timedelta(days=1)).timetuple())) * 1000 | |
while True: | |
list_motion_videos = session.get(f"{HOST}/proxy/protect/api/events", params={ 'start': start_time, 'end': end_time, 'orderDirection': 'DESC', 'types': 'motion'}) | |
if list_motion_videos.status_code != 200: | |
raise | |
list_motion_videos = json.loads(list_motion_videos.content) | |
mango_videos = [x for x in list_motion_videos if x["description"]["messageKeys"][0]["text"] == CAMERA_NAME] | |
print(f"From {datetime.datetime.fromtimestamp(start_time / 1000)} to {datetime.datetime.fromtimestamp(end_time / 1000)} there are {len(mango_videos):,} Mango videos") | |
for mango in mango_videos: | |
name = f'Mango {datetime.datetime.fromtimestamp(mango["start"] / 1000).strftime("%Y-%m-%d %H%M%S")}.mp4' | |
if not os.path.exists(name): | |
file_motion = session.get(f'{HOST}/proxy/protect/api/video/export', params={'camera': mango["camera"], 'channel': 0, 'start': mango['start'], 'end': mango['end']}) | |
if file_motion.status_code != 200: | |
throw | |
print(f'File [{name}] will be [{file_motion.headers["Content-Length"]:,}] bytes long') | |
file = open(name, "wb") | |
file.write(file_motion.content) | |
file.close() | |
start_time = start_time - 86400000 | |
end_time = end_time - 86400000 | |
if datetime.datetime.fromtimestamp(start_time / 1000) < datetime.datetime(2024, 2, 14): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment