From a1276e0c1b4a92788b481b0cfa5878d0fd58958b Mon Sep 17 00:00:00 2001 From: zhangshu Date: Tue, 20 Aug 2024 23:10:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edownload=5Fvideo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/DownloadUtils.py | 9 +++----- download_video.py | 7 ++++++ download_video_config.json | 3 ++- sftp.py | 45 ++++++++++++++++++++++++++++++++++++++ sftp_config.json | 20 +++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 sftp.py create mode 100644 sftp_config.json diff --git a/common/DownloadUtils.py b/common/DownloadUtils.py index c5e480a..8786e40 100644 --- a/common/DownloadUtils.py +++ b/common/DownloadUtils.py @@ -52,17 +52,14 @@ class DownloadUtil: ys.download(output_path=mp3OutPutPath, filename=fileName, mp3=True) @func_set_timeout(60) - def downloadOne(videoId): + def downloadOne(videoId, rootPath): video: Video = VideoService.queryOneByVideoId(videoId=videoId) channel: Channel = ChannelService.queryOneByChannelId(video.channelId) # 开始下载 Logger.info("开始下载...{}".format(videoId)) # 配置下载地址 /mnt/youtube_mysql - mainPath = "/mnt/youtube_mysql/srt/main" - tmpPath = "/mnt/youtube_mysql/srt/tmp" - # TODO 删除测试代码 - mainPath = "E:/code/tmp/main" - tmpPath = "E:/code/tmp/tmp" + mainPath = "{}/main".format(rootPath) + tmpPath = "{}/tmp".format(rootPath) storePath = "{}/{}/{}-{}".format( mainPath, channel.region, channel.channelId, channel.channelTitle) diff --git a/download_video.py b/download_video.py index d456092..a1c9d25 100644 --- a/download_video.py +++ b/download_video.py @@ -8,6 +8,7 @@ from sqlalchemy import create_engine from entity.DownloadInfoEntity import DownloadInfo from service.DownloadInfoService import DownloadInfoService from common.YoutubeUtils import YouTubeUtil +from common.DownloadUtils import DownloadUtil import operator import argparse @@ -36,10 +37,16 @@ if __name__ == "__main__": Logger.info("连接mysql成功") # 获取一个未下载的video + downloadRootPath = data['download_root_path'] + if downloadRootPath == "" or downloadRootPath == None: + Logger.error("downloadRootPath配置错误...") + exit + Logger.info(f"downloadRootPaht: '{downloadRootPath}'") downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish() if downloadInfo != None: Logger.info("开始下载videoId:{}".format(downloadInfo.videoId)) # TODO 下载字幕文件或音频文件 + DownloadUtil.downloadOne(downloadInfo.videoId, downloadRootPath) # 重新获取下一个下载任务 downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish() else: diff --git a/download_video_config.json b/download_video_config.json index f6e2a36..4f99d62 100644 --- a/download_video_config.json +++ b/download_video_config.json @@ -9,5 +9,6 @@ "log": { "dir": "./logs", "fileName": "download_video" - } + }, + "download_root_path": "E:/code/tmp/main" } \ No newline at end of file diff --git a/sftp.py b/sftp.py new file mode 100644 index 0000000..1807930 --- /dev/null +++ b/sftp.py @@ -0,0 +1,45 @@ +import os +import shutil +import paramiko +import argparse +import Contant +from LoggerUtils import Logger, initLogger +import configparser +import requests +import time +import json + +if __name__ == "__main__": + # 读取配置文件 + with open('search_video_config.json', 'r', encoding='utf-8') as f: + # 使用json.load()方法读取文件内容 + data = json.load(f) + # 初始化日志 + Contant.logDir = data['log']['dir'] + Contant.logFileName = data['log']['fileName'] + initLogger(Contant.logDir, Contant.logFileName) + + # 设置sftp client + hostname = data['sftp']['hostname'] + port = data['sftp']['port'] + username = data['sftp']['username'] + password = data['sftp']['password'] + + Logger.info("host:{},port:{},username:{},password:{}".format( + hostname, port, username, password)) + + ssh_client = paramiko.SSHClient() + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + sftp_client = None # 设置默认值 + ssh_client.connect(hostname, port, username, password) + # 创建SFTP客户端 + sftp_client = ssh_client.open_sftp() + Logger.info("SFTP客户端已经建立:{}".format(sftp_client)) + + remote_root = "/Inbound/YouTube Captions" + local_root = data['sftp']['local'] + Logger.info("remote_root:{},local_root:{}".format(remote_root, local_root)) + + # 遍历所有文件,然后上传 + + diff --git a/sftp_config.json b/sftp_config.json new file mode 100644 index 0000000..0a4cc72 --- /dev/null +++ b/sftp_config.json @@ -0,0 +1,20 @@ +{ + "mysql": { + "host": "47.108.20.249", + "port": "3306", + "username": "root", + "password": "casino888!", + "database": "youtube" + }, + "log": { + "dir": "./logs", + "fileName": "sftp" + }, + "sftp": { + "local": "E:/code/tmp/main", + "hostname": "filetransfer.blackrock.com", + "port": "22", + "username": "ftp_yunbo", + "password": "s8v{8SJr" + } +} \ No newline at end of file