import requests
import wget


l_listurl = 'http://10.6.10.161/DCIM/L001'
r_listurl = 'http://10.6.10.162/DCIM/R001'

def download_file(url, fname):
    with requests.get(f'{url}/{fname}',stream=True) as r:
        r.raise_for_status()
        with open(f'/media/ops/wecssd/{fname}','wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)

def download_videos(url:str):
    # download files
    resp = requests.get(url)
    reply = resp.json()
    for fname in reply['files']:
        mov_reply = requests.get(f"{url}/{fname}?act=info")
        print(f"Downloading {fname}, duration {mov_reply.json()['dur']}")
        #wget.download(f"{url}/{fname}",f"/media/ops/wecssd/{fname}")
        #download_file(url, fname)


# get file names
download_videos(l_listurl)
download_videos(r_listurl)

