from concurrent.futures import ThreadPoolExecutor
import os
import os.path as path
import sys
import datetime
import urllib
import urllib.parse
import urllib.request

work_path = sys.path[0]
# 设备列表
devices_file = "devices.txt"
devices_file_path = path.join(work_path, devices_file)
# 下载目录
tmp_dir = "download"
tmp_path = path.join(work_path, tmp_dir)

server = "http://127.0.0.1:18183/video"


# server = "http://httpbin.org/anything/video"

def check_or_mk_tmp_dir():
    if not path.exists(tmp_path):
        os.mkdir(tmp_path)


def check_or_create_devices_file():
    if not path.exists(devices_file_path):
        with open(devices_file_path, mode="w", encoding="utf8") as f:
            f.write("# 设备编码 一行一个\n")


def read_devices_file():
    check_or_create_devices_file()
    devices = []
    with open(devices_file_path, mode="r", encoding="utf8") as f:
        while True:
            line = f.readline()
            if not line:
                break
            line = line.strip()
            if line.startswith("#") or len(line) == 0:
                continue
            else:
                devices.append(line)

    print(f"读取设备数量: {len(devices)}")
    return devices


def tasks(device: str, start_time: str, end_time: str):
    params = {
        "start_time": start_time,
        "end_time": end_time,
        "device_id": device,
        "useDownload": True,
    }
    url_params = urllib.parse.urlencode(params)
    url = urllib.parse.urljoin(server, f"?{url_params}")
    start = datetime.datetime.now()
    start_str = start.strftime("%Y-%m-%d %H:%M:%S.%f")
    print(f"{start_str} 开始下载: {url}")
    file_path = f"{tmp_path}/{device}_{start_time}_{end_time}.mp4"
    urllib.request.urlretrieve(url, file_path)
    end = datetime.datetime.now()
    print(
        f"{device} {start_time}-{end_time}: 下载用时: {(end - start).total_seconds()} 秒")
    stats = os.stat(file_path)
    print(f"文件 {file_path} 大小: {stats.st_size}")


if __name__ == '__main__':
    check_or_mk_tmp_dir()
    check_or_create_devices_file()

    print(work_path)
    # workers = os.cpu_count()
    workers = 32

    print(f"最大并发数: {workers}")

    with ThreadPoolExecutor(max_workers=workers) as worker:
        devices = read_devices_file()

        # day = datetime.datetime.today()
        day = datetime.date(year=2024, month=3, day=11)
        # 开始时间
        start = datetime.time(hour=8, minute=11, second=15)
        # 结束时间
        end = datetime.time(hour=8, minute=11, second=30)

        start_time = datetime.datetime.combine(day, start).strftime(
            "%Y%m%d%H%M%S")
        end_time = datetime.datetime.combine(day, end).strftime("%Y%m%d%H%M%S")
        for device in devices:
            worker.submit(tasks, device, start_time, end_time)
