From cde1251e25bf36e5b33dd2c873d53493ab5d046a Mon Sep 17 00:00:00 2001 From: ngfchl Date: Thu, 29 Sep 2022 12:39:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=A7=8D=E5=AD=90=E7=9A=84?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=8E=A7=E5=88=B6=EF=BC=8C=E6=9A=82=E5=81=9C?= =?UTF-8?q?/=E7=BB=A7=E7=BB=AD/=E5=BC=BA=E5=88=B6=E7=BB=A7=E7=BB=AD/?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=87=AA=E5=8A=A8=E7=AE=A1=E7=90=86/?= =?UTF-8?q?=E8=B6=85=E7=BA=A7=E5=81=9A=E7=A7=8D/=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C/=E6=89=8B=E5=8A=A8=E6=B1=87=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_pt/urls.py | 1 + auto_pt/views.py | 42 ++++++++++-- templates/auto_pt/downloading.html | 100 +++++++++++++++++++++++------ 3 files changed, 120 insertions(+), 23 deletions(-) diff --git a/auto_pt/urls.py b/auto_pt/urls.py index 6a10d06..e5f6fd3 100644 --- a/auto_pt/urls.py +++ b/auto_pt/urls.py @@ -15,6 +15,7 @@ urlpatterns = [ path(r'page_downloading', views.page_downloading, name='page_downloading'), path(r'get_downloader', views.get_downloader, name='get_downloader'), path(r'downloading', views.get_downloading, name='downloading'), + path(r'control_torrent', views.control_torrent, name='control_torrent'), path(r'torrent_info_page', views.render_torrents_page, name='torrent_info_page'), path(r'get_torrent_info_list', views.get_torrent_info_list, name='get_torrent_info_list'), path(r'do_sql', views.do_sql, name='do_sql'), diff --git a/auto_pt/views.py b/auto_pt/views.py index f86392e..a75957e 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -10,11 +10,9 @@ import qbittorrentapi from django.http import JsonResponse from django.shortcuts import render -from pt_site import views as tasks -from pt_site.UtilityTool import FileSizeConvert from pt_site.models import SiteStatus, MySite, Site, Downloader, TorrentInfo from pt_site.views import scheduler, pt_spider -from ptools.base import CommonResponse, StatusCodeEnum, DownloaderCategory, TorrentBaseInfo +from ptools.base import CommonResponse, StatusCodeEnum, DownloaderCategory def add_task(request): @@ -110,6 +108,10 @@ def get_downloading(request): try: qb_client.auth_log_in() torrents = qb_client.torrents_info() + transfer = qb_client.transfer_info() + main_data = qb_client.sync_maindata() + print(transfer) + print(json.dumps(main_data)) for torrent in torrents: # 时间处理 # 添加于 @@ -147,7 +149,7 @@ def get_downloading(request): torrent['uploaded'] = '' if torrent['uploaded'] == 0 else torrent['uploaded'] torrent['upspeed'] = '' if torrent['upspeed'] == 0 else torrent['upspeed'] torrent['dlspeed'] = '' if torrent['dlspeed'] == 0 else torrent['dlspeed'] - print(torrents) + print(len(torrents)) return JsonResponse(CommonResponse.success(data=torrents).to_dict(), safe=False) except Exception as e: print(e) @@ -156,6 +158,38 @@ def get_downloading(request): ).to_dict(), safe=False) +def control_torrent(request): + ids = request.POST.get('ids') + command = request.POST.get('command') + downloader_id = request.POST.get('downloader_id') + print(request.POST) + # print(command, type(ids), downloader_id) + downloader = Downloader.objects.filter(id=downloader_id).first() + qb_client = qbittorrentapi.Client( + host=downloader.host, + port=downloader.port, + username=downloader.username, + password=downloader.password, + SIMPLE_RESPONSES=True + ) + try: + qb_client.auth_log_in() + # qb_client.torrents.resume() + # 根据指令字符串定位函数 + command_exec = getattr(qb_client.torrents, command) + print(command_exec) + command_exec(torrent_hashes=ids.split(',')) + # 延缓2秒等待操作生效 + time.sleep(2) + except Exception as e: + print(e) + return JsonResponse(CommonResponse.success(data={ + 'ids': ids.split(','), + 'command': command, + 'downloader_id': downloader_id + }).to_dict(), safe=False) + + def import_from_ptpp(request): if request.method == 'GET': return render(request, 'auto_pt/import_ptpp.html') diff --git a/templates/auto_pt/downloading.html b/templates/auto_pt/downloading.html index 1ded367..c893b90 100644 --- a/templates/auto_pt/downloading.html +++ b/templates/auto_pt/downloading.html @@ -29,22 +29,29 @@ {# :name="downloader.id"#} :id="downloader.id">
- - 查看 + + 操作 - 继续 - 暂停 - 强制继续 - 删除 - 限速 - 分类 - 更改路径 - 自动管理 - 重新校验 - 复制链接 - 复制HASH + 继续 + 强制继续 + 暂停 + 自动管理 + 超级做种 + 重新校验 + 重新汇报 + + 操作 + + 删除 + 分类 + 限速 + {# 更改路径#} + + + {# 复制链接#} + {# 复制HASH#} 暂停刷新 {# 开始刷新#} { + console.log(item['hash'], index) + ids.push(item['hash']) + }) + let data = new FormData() + data.append('ids', ids) + data.append('command', command) + data.append('downloader_id', this.downloader_id) + axios.post( + "{% url "control_torrent" %}", + data + ).then(res => { + console.log(res.data) + this.get_downloading(this.downloader_id) + this.$message({ + type: 'success', + message: '指令发送成功!!' + }) + }).catch(res => { + this.$message({ + type: 'warning', + message: '指令发送失败!!' + }) + }) + }, get_downloader() { axios.get( "{% url "get_downloader" %}" @@ -340,7 +393,7 @@ this.downloaders = res.data.data this.downloader_id = this.downloaders[0].id this.get_downloading(this.downloader_id) - console.log(this.torrents) + console.log(this.torrents.length) this.loading = false } else { this.loading = false @@ -382,10 +435,19 @@ } } ).then(res => { - if (res.data.code === 0) { - console.log(res.data.data) + console.log(res.data.data.length) this.torrents = res.data.data + this.$message({ + type: 'success', + message: '任务加载成功!!' + }) + } else { + console.log(res.data.data) + this.$message({ + type: 'warning', + message: '任务加载出错!!' + }) } }) },