Last active
August 27, 2024 18:47
-
-
Save Jakatut/4b5328f78fbf9bb72d0d94d1d5de7d19 to your computer and use it in GitHub Desktop.
Python script for downloading videos from Blink cloud servers via blinkpy. Videos are sorted to a directory by the date within the video file name.
This file contains hidden or 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
from datetime import datetime | |
import os | |
from blinkpy.blinkpy import Blink | |
from blinkpy.auth import Auth | |
from dotenv import load_dotenv | |
def getLastReadTime() -> str: | |
with open(os.getenv('BASE_BLINK_STORAGE_PATH') + os.getenv('LAST_READ_TIME_FILE_NAME')) as file: | |
last_read = file.read() | |
print("last read: " + last_read) | |
if not last_read: | |
# There would most likely not be any new videos at the current time so exit the program and try again later. | |
saveNewLastReadTime() | |
exit() | |
return last_read | |
def downloadVideos(blink: Blink, last_read: str, camera_name: str): | |
print(f'Downloading videos for camera {camera_name}') | |
camera_storage_path = os.getenv('BASE_BLINK_STORAGE_PATH') + f'{camera_name}/' | |
os.makedirs(camera_storage_path, exist_ok=True) | |
# since in the format of 2018/07/04 09:34 (Y/m/d H:M) | |
blink.download_videos(camera_storage_path, since=f'{last_read}', delay=2, camera=camera_name) | |
sortVideos(camera_name, camera_storage_path) | |
def sortVideos(camera_name: str, video_base_storage_path: str): | |
# After all are downloaded, iterate over the files in the cameras directroy, pull the date (YYYY-MM-DD) | |
# and create a directory for that date if one does not already exist. | |
# Then, move the videos in the original download path to the directory for that videos specific day. | |
for _, _, files in os.walk(video_base_storage_path): | |
for file in files: | |
# Change from format g8t1-9000-0301-5s9b-2022-02-14t01-18-43-00-00.mp4 to 2022-02-14 | |
video_date = file.replace(camera_name.lower() + '-', '').replace("-00-00.mp4", '').split(sep='t')[0] | |
# Move the videos to their correct location. | |
os.makedirs(video_base_storage_path + video_date, exist_ok=True) | |
os.replace(video_base_storage_path + file, video_base_storage_path + video_date + '/' + file) | |
# Only get top level dirs, don't go recursively. | |
break | |
def saveNewLastReadTime(): | |
# Save the last read time to files. | |
last_read_time_file = open(os.getenv('BASE_BLINK_STORAGE_PATH') + 'last_read_time.txt', 'w') | |
last_read_time_file.write(datetime.now().strftime("%Y/%m/%d %H:%M")) | |
def setup() -> Blink: | |
load_dotenv() | |
# Initialize our Blink system. | |
blink = Blink() | |
auth = Auth({"username": os.getenv("BLINK_USERNAME"), "password": os.getenv("BLINK_PASSWORD")}, no_prompt=True) | |
opts = {"retries": 10, "backoff": 2} | |
auth.session = auth.create_session(opts=opts) | |
blink.auth = auth | |
blink.start() | |
auth.send_auth_key(blink, os.getenv('BLINK_AUTH_KEY')) | |
blink.setup_post_verify() | |
return blink | |
def run(): | |
print("Setup...") | |
blink = setup() | |
print() | |
print("Checking last read time...") | |
last_read = getLastReadTime() | |
print() | |
print("Downloading videos..") | |
# Iterate over each camera and store the videos in the camera's directory. | |
for camera_name, _ in blink.cameras.items(): | |
downloadVideos(blink, last_read, camera_name) | |
print() | |
print("Saving new read time...") | |
saveNewLastReadTime() | |
print() | |
print("Done!") | |
run() |
This file contains hidden or 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
BLINK_USERNAME= | |
BLINK_PASSWORD= | |
BLINK_CREDENTIALS_PATH= | |
BASE_BLINK_STORAGE_PATH= | |
BLINK_AUTH_KEY= | |
LAST_READ_TIME_FILE_NAME= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment