Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/cameras_gimbals/siyi-download/siyi-download.py
Views: 1799
#!/usr/bin/env python312"""3Downloads files from a Siyi camera connected via ethernet45AP_FLAKE8_CLEAN6"""78#9# utility to download files from a siyi camera10#1112import os13import json14from argparse import ArgumentParser15from urllib.request import urlopen, urlretrieve16from urllib.parse import urlencode17from urllib.error import URLError, HTTPError18from enum import Enum1920# prefix string for text outout to user21prefix_str = "siyi-download.py: "22ip_address_default = "192.168.144.25"232425# media types26class MediaTypes(Enum):27IMAGE = 028VIDEO = 1293031# media type strings. used to display to user32MEDIA_TYPE_STR = ["image", "video"]333435# get URL for list of directories36def get_dirlist_url(ip_address, media_type):37params = {'media_type': media_type}38return f"http://{ip_address}:82/cgi-bin/media.cgi/api/v1/getdirectories?" + urlencode(params)394041# get URL for list of files in a directory42def get_filelist_url(ip_address, media_type, dir_path):43params = {44'media_type': str(media_type),45'path': dir_path,46'start': 0,47'count': 99948}49return f"http://{ip_address}:82/cgi-bin/media.cgi/api/v1/getmedialist?" + urlencode(params)505152# download files from camera53def download_files(ip_address, dest_dir):5455# repeat for images and videos56for media_type in [mt.value for mt in MediaTypes]:5758# display output to user59print(prefix_str + f"downloading {MEDIA_TYPE_STR[media_type]} files")6061# download list of directories in JSON format62dir_list_url = get_dirlist_url(ip_address, media_type)63with urlopen(dir_list_url) as get_dir_url:64dir_dict = json.load(get_dir_url)6566# check that the request succeeded67if (not dir_dict['success']):68exit(prefix_str + "failed to get list of directories")6970# check response includes 'data'71if ('data' not in dir_dict.keys()):72exit(prefix_str + "could not get list of directories, no 'data' in response")73dir_dict_data = dir_dict['data']7475# check response includes 'directories'76if ('directories' not in dir_dict_data.keys()):77exit(prefix_str + "could not get list of directories, no 'directories' in response")78dir_dict_data_directories = dir_dict_data['directories']7980# create list of directories from 'path' values81dir_list = []82for dir in dir_dict_data_directories:83if ('path' in dir.keys()):84dir_list.append(dir['path'])85print(prefix_str + f"{len(dir_list)} directories")8687# get list of files in each directory88for dir_path in dir_list:89filenames_url = get_filelist_url(ip_address, media_type, dir_path)90with urlopen(filenames_url) as get_filenames_url:91filename_dict = json.load(get_filenames_url)9293# check that the request succeeded94if (not filename_dict['success']):95exit(prefix_str + "failed to get list of files")9697# check response includes 'data'98if ('data' not in filename_dict.keys()):99exit(prefix_str + "could not get list of files, no 'data' in response")100filename_dict_data = filename_dict['data']101102# check response includes 'list'103if ('list' not in filename_dict_data.keys()):104exit(prefix_str + "could not get list of files, no 'list' in response")105filename_dict_data_list = filename_dict_data['list']106print(prefix_str + f"{len(filename_dict_data_list)} files")107108# download each image109for fileinfo in filename_dict_data_list:110if ('name' not in fileinfo.keys() or 'url' not in fileinfo.keys()):111exit(prefix_str + "could not get list of files, no 'name' or 'url' in response")112filename = fileinfo['name']113file_url = fileinfo['url']114115# correct incorrect ip address in returned url116file_url_fixed = file_url.replace(ip_address_default, ip_address)117118# download file119print(prefix_str + f"downloading {filename} from {file_url_fixed}")120dest_filename = os.path.join(dest_dir, filename)121try:122urlretrieve(file_url_fixed, dest_filename)123except (URLError, HTTPError) as e:124print(prefix_str + f"failed to download {filename}: {e}")125126127# main function128def main():129parser = ArgumentParser(description=__doc__)130parser.add_argument("--ipaddr", default=ip_address_default, help="IP address of camera")131parser.add_argument("--dest", default=".", help="destination directory where downloaded files will be saved")132args = parser.parse_args()133134# check destination directory exists135if not os.path.exists(args.dest):136exit(prefix_str + "invalid destination directory")137138# download files139download_files(args.ipaddr, args.dest)140141142# main143if __name__ == "__main__":144main()145146147