任务管理进行时

This commit is contained in:
ngfchl
2022-09-25 14:38:47 +08:00
parent 433c324c6f
commit 3fd0be860b
2 changed files with 74 additions and 2 deletions

View File

@@ -12,7 +12,9 @@ import aip
import cloudscraper
import dateutil.parser
import opencc
import qbittorrentapi
import requests
import transmission_rpc
from django.db.models import QuerySet
from lxml import etree
from pypushdeer import PushDeer
@@ -23,7 +25,7 @@ from wxpusher import WxPusher
from auto_pt.models import Notify, OCR
from pt_site.models import MySite, SignIn, TorrentInfo, SiteStatus, Site
from ptools.base import TorrentBaseInfo, PushConfig, CommonResponse, StatusCodeEnum
from ptools.base import TorrentBaseInfo, PushConfig, CommonResponse, StatusCodeEnum, DownloaderCategory
def cookie2dict(source_str: str):
@@ -375,6 +377,55 @@ class PtSpider:
msg=site.name + (' 信息导入成功!' if result[1] else ' 信息更新成功! ') + passkey_msg
)
@staticmethod
def get_torrent_info_from_downloader(torrent_info: TorrentInfo):
"""
通过种子信息,到下载器查询任务信息
:param torrent_info:
:return:
"""
downloader = torrent_info.downloader
if not downloader:
return CommonResponse.error(
msg='此种子未推送到下载器!'
)
if downloader.category == DownloaderCategory.Transmission:
try:
tr_client = transmission_rpc.Client(host=downloader.host,
port=downloader.port,
username=downloader.username,
password=downloader.password)
torrent = tr_client.get_torrents(ids=torrent_info.hash_string)
except Exception as e:
return CommonResponse.error(
msg='下载无法连接,请检查下载器是否正常?!'
)
elif downloader.category == DownloaderCategory.qBittorrent:
try:
qb_client = qbittorrentapi.Client(
host=downloader.host,
port=downloader.port,
username=downloader.username,
password=downloader.password,
# 仅返回简单JSON
# SIMPLE_RESPONSES=True
)
qb_client.auth_log_in()
torrent = qb_client.torrents_info(hashes=torrent_info.hash_string)
except Exception as e:
return CommonResponse.error(
msg='下载无法连接,请检查下载器是否正常?'
)
# if downloader.category == DownloaderCategory.qBittorrent:
# pass
else:
return CommonResponse.error(
msg='下载不存在,请检查下载器是否正常?'
)
return CommonResponse.success(
data=torrent
)
@staticmethod
def download_img(image_url):
"""

View File

@@ -1,4 +1,5 @@
# Create your views here.
import datetime
import logging
import time
from concurrent.futures.thread import ThreadPoolExecutor
@@ -7,7 +8,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore
from pt_site.UtilityTool import PtSpider, MessageTemplate, FileSizeConvert
from pt_site.models import MySite
from pt_site.models import MySite, TorrentInfo
from ptools.base import StatusCodeEnum
job_defaults = {
@@ -135,6 +136,26 @@ try:
删除过期种子
"""
start = time.time()
torrent_info_list = TorrentInfo.objects.all().filter(downloader__isnull=False)
for torrent_info in torrent_info_list:
expire_time = torrent_info.sale_expire
if '无限期' in expire_time:
# ToDo 先更新种子信息,然后再判断
continue
if expire_time.endswith(':'):
expire_time += '00'
time_now = datetime.datetime.now()
expire_time_parse = datetime.datetime.strptime(expire_time, '%Y-%m-%d %H:%M:%S')
if time_now >= expire_time_parse:
if not torrent_info.downloader:
# 未推送到下载器,跳过或删除?
continue
if pt_spider.get_torrent_info_from_downloader(torrent_info).code == StatusCodeEnum.OK.code:
# todo 设定任务规则:
# 免费到期后,下载完毕的种子是删除还是保留?
# 未下载完成的,是暂停还是删除?
torrent_info.delete()
end = time.time()
pt_spider.send_text(
'> {} 任务运行成功!耗时:{}{} \n'.format('签到', end - start, time.strftime("%Y-%m-%d %H:%M:%S")))