uploadvideos.py 2.81 KB
# -*- coding: utf-8 -*-
# @Time    :  2020/12/10 23:41
# @Author  :  Young Lee
# @Email   :  young_lee2017@163.com

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from jumppage import jump_page


def get_uploded(driver):
    """ 获取已上传视频列表 """
    expanded_bar = "//div[contains(@class, 'treeBox')]//span[text()='手动添加录像资源']/../../preceding-sibling::span"
    if "expanded" not in driver.find_element_by_xpath(expanded_bar).get_attribute("class"):
        driver.find_element_by_xpath(expanded_bar).click()
        time.sleep(1)
    elements = driver.find_elements_by_css_selector(".el-tree-node__children span[class='tree-label']")

    return [element.text for element in elements]


def upload_videos(driver, videos_info):
    """上传指定目录的视频文件

    :param videos_info: 视频信息 {name: path, ...}
    :return: 已上传任务名称列表
    """
    add_bar = "//div[contains(@class, 'treeBox')]//span[text()='手动添加录像资源']/../following-sibling::span"
    jump_page(driver, "资源管理", "视频设备")
    uploded_videos = get_uploded(driver)
    videos_name = list(videos_info.keys())
    if [ele for ele in videos_name if ele in uploded_videos] == videos_name:
        print("***无需上传任何视频文件***")
        return videos_name

    driver.find_element_by_xpath(add_bar).click()
    input_element = driver.find_element_by_css_selector("input[type='file']")
    for video_name, video_path in videos_info.items():
        if video_name in uploded_videos:
            print(f"{video_name} 已存在,无需上传!")
            continue
        print(f"开始上传 --> {video_path}...")
        input_element.send_keys(video_path)
        driver.find_element_by_xpath("//span[text()='上 传']").click()
        try:
            WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.CLASS_NAME, "is-success"))
            )
        except Exception:
            print(f"{video_name}似乎没有上传成功!")
        finally:
            # 清除上传列表
            video_display = driver.find_element_by_class_name("el-upload-list__item")
            video_display.click()
            time.sleep(1)
            video_display.send_keys(Keys.DELETE)
            time.sleep(1)
    driver.find_element_by_xpath("//span[text()='上 传']/../preceding-sibling::button[1]").click()
    upload_list = get_uploded(driver)
    return list(set(videos_info.keys()) & set(upload_list))


if __name__ == "__main__":
    from unitinit import *
    from login import login_fx
    login_fx(driver, login_url, user, passwd)
    print(upload_videos(driver, videos_info))
    driver.quit()