forked from madwind/flexget_qbittorrent_mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iyuu_auto_reseed.py
164 lines (145 loc) · 6.54 KB
/
iyuu_auto_reseed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import hashlib
import time
from json import JSONDecodeError
from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils import json
from loguru import logger
from requests import RequestException
from .ptsites.executor import Executor
class PluginIYUUAutoReseed():
schema = {
'type': 'object',
'properties': {
'iyuu': {'type': 'string'},
'user-agent': {'type': 'string'},
'show_detail': {'type': 'boolean'},
'limit': {'type': 'integer'},
'passkeys': {
'type': 'object',
'properties': {
}
}
},
'additionalProperties': False
}
def prepare_config(self, config):
config.setdefault('iyuu', '')
config.setdefault('version', '1.10.9')
config.setdefault('limit', 999)
config.setdefault('show_detail', False)
config.setdefault('passkeys', {})
return config
def on_task_input(self, task, config):
config = self.prepare_config(config)
passkeys = config.get('passkeys')
limit = config.get('limit')
show_detail = config.get('show_detail')
torrent_dict, torrents_hashes = self.get_torrents_data(task, config)
try:
data = {
'sign': 'IYUU419Tada6a99f2a6591cc51e656ca4458317648931830',
'version': config['version']
}
sites_response = task.requests.get('http://api.iyuu.cn/index.php?s=App.Api.Sites', timeout=60,
params=data).json()
if sites_response.get('ret') != 200:
raise plugin.PluginError(
'http://api.iyuu.cn/index.php?s=App.Api.Sites: {}'.format(sites_response)
)
sites_json = self.modify_sites(sites_response['data']['sites'])
reseed_response = task.requests.post('http://api.iyuu.cn/index.php?s=App.Api.Infohash',
json=torrents_hashes,
timeout=60).json()
if reseed_response.get('ret') != 200:
raise plugin.PluginError(
'http://api.iyuu.cn/index.php?s=App.Api.Infohash Error: {}'.format(reseed_response)
)
reseed_json = reseed_response['data']
except (RequestException, JSONDecodeError) as e:
raise plugin.PluginError(
'Error when trying to send request to iyuu: {}'.format(e)
)
entries = []
site_limit = {}
if sites_json and reseed_json:
for info_hash, seeds_data in reseed_json.items():
client_torrent = torrent_dict[info_hash]
for torrent in seeds_data['torrent']:
site = sites_json.get(str(torrent['sid']))
if not site:
continue
site_name = self._get_site_name(site['base_url'])
passkey = passkeys.get(site_name)
if not passkey:
if show_detail:
logger.info(
'no passkey, skip site: {}, title: {}'.format(site_name, client_torrent['title']))
continue
if not site_limit.get(site_name):
site_limit[site_name] = 1
else:
if site_limit[site_name] >= limit:
logger.info(
'site_limit:{} >= limit: {}, skip site: {}, title: {}'.format(
site_limit[site_name],
limit,
site_name,
client_torrent['title'])
)
continue
site_limit[site_name] = site_limit[site_name] + 1
torrent_id = str(torrent['torrent_id'])
entry = Entry(
title=client_torrent['title'],
torrent_info_hash=torrent['info_hash']
)
entry['autoTMM'] = client_torrent['qbittorrent_auto_tmm']
entry['category'] = client_torrent['qbittorrent_category']
entry['savepath'] = client_torrent['qbittorrent_save_path']
entry['paused'] = 'true'
entry['class_name'] = site_name
Executor.build_reseed(entry, config, site, passkey, torrent_id)
if show_detail:
logger.info(
'accept site: {}, title: {}, url: {}'.format(site_name, client_torrent['title'],
entry.get('url', None)))
if entry.get('url'):
entries.append(entry)
return entries
def get_torrents_data(self, task, config):
torrent_dict = {}
torrents_hashes = {}
hashes = []
for entry in task.all_entries:
entry.reject('torrent form client')
if 'up' in entry['qbittorrent_state'].lower() and 'pause' not in entry['qbittorrent_state'].lower():
torrent_dict[entry['torrent_info_hash']] = entry
hashes.append(entry['torrent_info_hash'])
list.sort(hashes)
hashes_json = json.dumps(hashes, separators=(',', ':'))
sha1 = hashlib.sha1(hashes_json.encode("utf-8")).hexdigest()
torrents_hashes['hash'] = hashes_json
torrents_hashes['sha1'] = sha1
torrents_hashes['sign'] = config['iyuu']
torrents_hashes['timestamp'] = int(time.time())
torrents_hashes['version'] = config['version']
return torrent_dict, torrents_hashes
def modify_sites(self, sites_json):
sites_dict = {}
for site in sites_json:
site['download_page'] = site['download_page'].replace('{}', '{torrent_id}')
if site['base_url'] == 'pt.upxin.net':
site['base_url'] = 'pt.hdupt.com'
sites_dict[str(site['id'])] = site
return sites_dict
def _get_site_name(self, base_url):
domain = base_url.split('.')
site_name = domain[-2]
if site_name == 'edu':
site_name = domain[-3]
return site_name
@event('plugin.register')
def register_plugin():
plugin.register(PluginIYUUAutoReseed, 'iyuu_auto_reseed', api_ver=2)