Browse Source

新增download_video

master
zhangshu 5 months ago
parent
commit
a1276e0c1b
  1. 9
      common/DownloadUtils.py
  2. 7
      download_video.py
  3. 3
      download_video_config.json
  4. 45
      sftp.py
  5. 20
      sftp_config.json

9
common/DownloadUtils.py

@ -52,17 +52,14 @@ class DownloadUtil:
ys.download(output_path=mp3OutPutPath, filename=fileName, mp3=True) ys.download(output_path=mp3OutPutPath, filename=fileName, mp3=True)
@func_set_timeout(60) @func_set_timeout(60)
def downloadOne(videoId): def downloadOne(videoId, rootPath):
video: Video = VideoService.queryOneByVideoId(videoId=videoId) video: Video = VideoService.queryOneByVideoId(videoId=videoId)
channel: Channel = ChannelService.queryOneByChannelId(video.channelId) channel: Channel = ChannelService.queryOneByChannelId(video.channelId)
# 开始下载 # 开始下载
Logger.info("开始下载...{}".format(videoId)) Logger.info("开始下载...{}".format(videoId))
# 配置下载地址 /mnt/youtube_mysql # 配置下载地址 /mnt/youtube_mysql
mainPath = "/mnt/youtube_mysql/srt/main" mainPath = "{}/main".format(rootPath)
tmpPath = "/mnt/youtube_mysql/srt/tmp" tmpPath = "{}/tmp".format(rootPath)
# TODO 删除测试代码
mainPath = "E:/code/tmp/main"
tmpPath = "E:/code/tmp/tmp"
storePath = "{}/{}/{}-{}".format( storePath = "{}/{}/{}-{}".format(
mainPath, channel.region, channel.channelId, channel.channelTitle) mainPath, channel.region, channel.channelId, channel.channelTitle)

7
download_video.py

@ -8,6 +8,7 @@ from sqlalchemy import create_engine
from entity.DownloadInfoEntity import DownloadInfo from entity.DownloadInfoEntity import DownloadInfo
from service.DownloadInfoService import DownloadInfoService from service.DownloadInfoService import DownloadInfoService
from common.YoutubeUtils import YouTubeUtil from common.YoutubeUtils import YouTubeUtil
from common.DownloadUtils import DownloadUtil
import operator import operator
import argparse import argparse
@ -36,10 +37,16 @@ if __name__ == "__main__":
Logger.info("连接mysql成功") Logger.info("连接mysql成功")
# 获取一个未下载的video # 获取一个未下载的video
downloadRootPath = data['download_root_path']
if downloadRootPath == "" or downloadRootPath == None:
Logger.error("downloadRootPath配置错误...")
exit
Logger.info(f"downloadRootPaht: '{downloadRootPath}'")
downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish() downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish()
if downloadInfo != None: if downloadInfo != None:
Logger.info("开始下载videoId:{}".format(downloadInfo.videoId)) Logger.info("开始下载videoId:{}".format(downloadInfo.videoId))
# TODO 下载字幕文件或音频文件 # TODO 下载字幕文件或音频文件
DownloadUtil.downloadOne(downloadInfo.videoId, downloadRootPath)
# 重新获取下一个下载任务 # 重新获取下一个下载任务
downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish() downloadInfo: DownloadInfo = DownloadInfoService.getOneNoFinish()
else: else:

3
download_video_config.json

@ -9,5 +9,6 @@
"log": { "log": {
"dir": "./logs", "dir": "./logs",
"fileName": "download_video" "fileName": "download_video"
} },
"download_root_path": "E:/code/tmp/main"
} }

45
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))
# 遍历所有文件,然后上传

20
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"
}
}
Loading…
Cancel
Save