Skip to content

Commit

Permalink
Merge pull request #21 from ddd354/add-deluge-support
Browse files Browse the repository at this point in the history
Add deluge support
  • Loading branch information
ddd354 authored Sep 23, 2020
2 parents e75e803 + 09a8288 commit 5bcd258
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
JavOneStop which is a small tool that helps users rename, parse, generate nfo, organize jav video files and
communicate with Emby to add actresses images.

## [Unreleased]
### 新增
- 增加deluge磁链下载支持

## [0.8.0] - 2020-09-17
### 新增
- 支持sqlite数据库后端
Expand Down
61 changes: 61 additions & 0 deletions JavHelper/core/deluge_downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import requests
import json
from datetime import datetime
import re
from blitzdb.document import DoesNotExist
from time import sleep
from traceback import print_exc

from JavHelper.core.backend_translation import BackendTranslation
from JavHelper.core.ini_file import return_default_config_string

if return_default_config_string('db_type') == 'sqlite':
from JavHelper.model.jav_manager import SqliteJavManagerDB as JavManagerDB
else:
from JavHelper.model.jav_manager import BlitzJavManagerDB as JavManagerDB


class DelugeDownloader:
@staticmethod
def create_deluge_cookies(url: str, secret: str):
res = requests.post(url, json={'id': 0, 'method': 'auth.login', 'params': [secret]})
if res.json().get('result'):
return res.cookies
else:
raise Exception('cannot connect to deluge server {} with secret {}'.format(url, secret))

def __init__(self):
self.deluge_address = '{}json'.format(return_default_config_string('deluge_address'))
deluge_secret = return_default_config_string('deluge_secret')
self.cookies = self.create_deluge_cookies(self.deluge_address, deluge_secret)
self.translate_map = BackendTranslation()

def handle_jav_download(self, car: str, magnet: str):
db_conn = JavManagerDB()
try:
jav_obj = dict(db_conn.get_by_pk(car))
except (DoesNotExist, TypeError) as e:
# typeerror to catch dict(None)
jav_obj = {'car': car}

retry_num = 0
e = None

# send download info to aria2
try:
res = requests.post(self.deluge_address, json={
'id': 0, 'method': 'core.add_torrent_magnet', 'params': [magnet, {}]
}, cookies=self.cookies).json()

if res.get('result'):
# if everything went well, update stat
jav_obj['stat'] = 4
db_conn.upcreate_jav(jav_obj)
return jav_obj
else:
raise Exception(res.get('error', {}).get('message'))
except Exception as _e:
print_exc()
e = _e

return {'error': self.translate_map['oof_general_failure'].format(car=car, retry_num=retry_num, e=e)}
13 changes: 12 additions & 1 deletion JavHelper/core/ini_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

DEFAULT_INI = resource_path('settings.ini')
DEFAULT_UPDATE_MAPPING = {
'magnet_downloader': ["下载设置", "下载器"],
'deluge_address': ["下载设置", "deluge地址"],
'deluge_secret': ["下载设置", "deluge密钥"],
'aria_address': ["Aria2设置", "Aria2地址"],
'aria_port': ["Aria2设置", "Aria2端口"],
'aria_token': ["Aria2设置", "Aria2 Token"],
Expand All @@ -25,6 +28,9 @@
'db_type': ["其他设置", "数据库类型"],
}
DEFAULT_UPDATE_VALUE_MAPPING = {
'magnet_downloader': "aria2",
'deluge_address': "",
'deluge_secret': "",
'aria_address': "",
'aria_port': "",
'aria_token': "",
Expand All @@ -42,7 +48,7 @@
'display_language': 'cn',
'remove_string': '',
'ikoa_dmmc_server': '',
'db_type': 'blitz'
'db_type': 'sqlite'
}

def verify_ini_file(ini_file_name=DEFAULT_INI):
Expand Down Expand Up @@ -110,6 +116,10 @@ def return_config_string(field_path: list, config=None):
def recreate_ini(ini_file_name=DEFAULT_INI):
config_settings = configparser.RawConfigParser()
print('正在重写ini...')
config_settings.add_section("下载设置")
config_settings.set("下载设置", "下载器", "aria2")
config_settings.set("下载设置", "deluge地址", "")
config_settings.set("下载设置", "deluge密钥", "")
config_settings.add_section("Aria2设置")
config_settings.set("Aria2设置", "Aria2地址", "")
config_settings.set("Aria2设置", "Aria2端口", "")
Expand Down Expand Up @@ -159,5 +169,6 @@ def recreate_ini(ini_file_name=DEFAULT_INI):
config_settings.set("其他设置", "ikoa_dmmc", "")
config_settings.set("其他设置", "扫描文件类型", "mp4、mkv、avi、wmv、iso、rmvb、MP4")
config_settings.set("其他设置", "重命名中的标题长度(50~150)", "50")
config_settings.set("其他设置", "数据库类型", "sqlite")
config_settings.write(open(ini_file_name, "w", encoding='utf-8-sig'))
print('写入ini文件成功')
2 changes: 1 addition & 1 deletion JavHelper/static/js/webHelper.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions JavHelper/static/webHelper/configurator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ const JavConfigurator = (props) => {
"type": "string",
"title": t('set_javlib_url_title')
},
"magnet_downloader": {
"type": "string",
"title": t('magnet_downloader'),
"enum": ["aria2", "deluge"]
},
"deluge_address": {
"type": "string",
"title": t('deluge_address'),
},
"deluge_secret": {
"type": "string",
"title": t('deluge_secret'),
},
"aria_address": {
"type": "string",
"title": t('set_aria2_url_title')
Expand Down Expand Up @@ -139,6 +152,12 @@ const JavConfigurator = (props) => {
},
"db_type": {
"ui:widget": "radio"
},
"magnet_downloader": {
"ui:widget": "radio"
},
"deluge_address": {
"ui:description": t('emby_addr_tip'),
},
};

Expand Down
14 changes: 12 additions & 2 deletions JavHelper/static/webHelper/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const resources = {

// configurator
'db_type': 'DB Type',
'magnet_downloader': 'Magnet Downloader Type',
'deluge_address': 'Deluge server address',
'deluge_secret': 'Deluge secret (don\'t support username / password)',

'scrape_all': 'Scrape All',
'local': 'Local',
Expand Down Expand Up @@ -108,11 +111,13 @@ const resources = {
'download_magnet_button': 'Download',
'download_web_button': 'Web Download',
'download_iframe_button': 'Downloader',

// Console logs
'log_search_web_jav': 'Searching: ',
'log_switch_jav_set': 'Change jav set to: ',
'log_error': 'Error: ',
'log_change_website': 'change website source to: ',
'log_aria2_download': 'aria2 downloadeding: ',
'log_magnet_download': 'Magnet link added to downloader: ',
'log_idmm_download': 'idmm downloader job added: ',
'log_update_jav_stat': 'Update DB Stat: ',
'log_filter': 'filter on: ',
Expand Down Expand Up @@ -212,6 +217,9 @@ const resources = {

// configurator
'db_type': '数据库类型',
'magnet_downloader': '磁链下载器类型',
'deluge_address': 'Deluge服务器地址和端口',
'deluge_secret': 'Deluge secret (暂不支持用户名密码类型)',


'local': '本地',
Expand All @@ -237,11 +245,13 @@ const resources = {
'download_magnet_button': '下载',
'download_web_button': '网页下载',
'download_iframe_button': '下载器',

// Console logs
'log_search_web_jav': '搜索: ',
'log_switch_jav_set': '更改搜索页面: ',
'log_error': '发生错误: ',
'log_change_website': '更换网站来源: ',
'log_aria2_download': '下载添加至aria2: ',
'log_magnet_download': '磁链下载成功添加至下载器: ',
'log_idmm_download': '下载添加至idmm下载器',
'log_update_jav_stat': '更新数据库文档: ',
'log_filter': '启用过滤器: ',
Expand Down
4 changes: 2 additions & 2 deletions JavHelper/static/webHelper/javMagnetButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const JavMagnetButton = ({ car, download_link, setJavStat, type }) => {
setLoading(false);
})
} else if (isLoading) {
fetch('/jav_browser/download_via_aria',
fetch('/jav_browser/download_magnet',
{method: 'post',
body: JSON.stringify({
"car": _car,
Expand All @@ -53,7 +53,7 @@ const JavMagnetButton = ({ car, download_link, setJavStat, type }) => {
} else {
fetch(`/local_manager/update_car_ikoa_stat?car=${jsonData.success.car}&stat=4`)
.then(() => {
console.log(t('log_aria2_download'), jsonData.success.car);
console.log(t('log_magnet_download'), jsonData.success.car);
setJavStat(4);
})
setLoading(false);
Expand Down
3 changes: 3 additions & 0 deletions JavHelper/static/webHelper/webHelper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class App extends Component {
console.log('initialized');
// load ini from file
fetch(`/directory_scan/read_local_ini?filter_dict={
'magnet_downloader': ["下载设置", "下载器"],
'deluge_address': ["下载设置", "deluge地址"],
'deluge_secret': ["下载设置", "deluge密钥"],
'aria_address': ["Aria2设置", "Aria2地址"],
'aria_port': ["Aria2设置", "Aria2端口"],
'aria_token': ["Aria2设置", "Aria2 Token"],
Expand Down
17 changes: 10 additions & 7 deletions JavHelper/views/jav_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from JavHelper.core.javdb import javdb_set_page, javdb_search
from JavHelper.core.jav777 import jav777_set_page
from JavHelper.core.OOF_downloader import OOFDownloader
from JavHelper.core.deluge_downloader import DelugeDownloader
from JavHelper.core.backend_translation import BackendTranslation
from JavHelper.core.aria2_handler import verify_aria2_configs_exist
from JavHelper.core.ini_file import return_default_config_string
Expand Down Expand Up @@ -220,20 +221,22 @@ def oof_quota():
except FileNotFoundError:
return jsonify({'error': BackendTranslation()['oof_cookies_not_found']}), 500



@jav_browser.route('/download_via_aria', methods=['POST'])
def download_via_aria():
@jav_browser.route('/download_magnet', methods=['POST'])
def download_magnet():
req_data = json.loads(request.get_data() or '{}')
car = req_data.get('car')
magnet = req_data.get('magnet')

if not car or not magnet:
return jsonify({'error': 'required fields are not found in posted json'}), 400

oof_downloader = OOFDownloader()

jav_obj = oof_downloader.handle_jav_download(car, magnet)
if return_default_config_string('magnet_downloader') == 'aria2':
_downloader = OOFDownloader()
else:
# use deluge client
_downloader = DelugeDownloader()

jav_obj = _downloader.handle_jav_download(car, magnet)
if not jav_obj.get('error'):
return jsonify({'success': jav_obj})
else:
Expand Down

0 comments on commit 5bcd258

Please sign in to comment.