Created
September 30, 2018 20:23
-
-
Save optilude/9252286fee6bfb7c5da97334f800c66b to your computer and use it in GitHub Desktop.
Arlo backup (Python 2.7)
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 sys | |
import os | |
import logging | |
import argparse | |
import datetime | |
from datetime import timedelta, date | |
from Arlo import Arlo | |
parser = argparse.ArgumentParser(description='Back up Arlo videos') | |
parser.add_argument('--username', help="Arlo username") | |
parser.add_argument('--password', help="Arlo password") | |
parser.add_argument('--days', metavar="7", type=int, default=7, help="Days in the past to parse") | |
parser.add_argument('path', help="Directory to write files to") | |
args = parser.parse_args() | |
if not args.username or not args.password or not args.path: | |
parser.print_usage() | |
sys.exit(1) | |
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) | |
logging.info("Logging in to Arlo") | |
arlo = Arlo(args.username, args.password) | |
today = (date.today() - timedelta(days=0)).strftime("%Y%m%d") | |
seven_days_ago = (date.today() - timedelta(days=args.days)).strftime("%Y%m%d") | |
# Get all of the recordings for a date range. | |
logging.info("Fetching library for {} to {}".format(seven_days_ago, today)) | |
library = arlo.GetLibrary(seven_days_ago, today) | |
# Iterate through the recordings in the library. | |
skipped = 0 | |
downloaded = 0 | |
for recording in library: | |
video_filename = os.path.join(args.path, datetime.datetime.fromtimestamp(int(recording['name']) // 1000).strftime('%Y-%m-%d %H-%M-%S') + ' ' + recording['uniqueId'] + '.mp4') | |
if os.path.exists(video_filename) and os.path.getsize(video_filename) > 0: | |
skipped += 1 | |
continue | |
stream = arlo.StreamRecording(recording['presignedContentUrl']) | |
with open(video_filename, 'w') as f: | |
for chunk in stream: | |
# Support both Python 2.7 and 3. | |
if sys.version[0] == '2': | |
f.write(chunk) | |
else: | |
f.buffer.write(chunk) | |
f.close() | |
logging.info('Downloaded video %s from %s.' % (video_filename, recording['createdDate'],)) | |
downloaded += 1 | |
timestamp = int(recording['name']) // 1000 | |
os.utime(video_filename, times=(timestamp, timestamp)) | |
interval = 1 | |
logging.info("Downloaded: {:d}, Skipped: {:d}".format(downloaded, skipped)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment