-
Notifications
You must be signed in to change notification settings - Fork 7
/
promotion.py
314 lines (278 loc) · 9.39 KB
/
promotion.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division, absolute_import
from builtins import * # noqa pylint: disable=unused-import, redefined-builtin
import logging
from flexget import plugin
from flexget.event import event
import requests
from bs4 import BeautifulSoup
import re
log = logging.getLogger('promotion')
class Filter_Promotion(object):
"""
Detect torrent's *current* promotion status.
Only support sites based on NexusPHP
Support sites (tested):
HDChina TJUPT NYPT Ourbits BYRBT NPUBits MTeam TTG...
Example::
promotion:
action: accept
cookie: * your cookie here *
username: * your username here *
promotion: free/twoupfree/halfdown/twouphalfdown/thirtypercent/none
not_hr: yes [optional]
"""
schema = {'type': 'object',
'properties': {
'action': {
'type': 'string',
'enum': ['accept', 'reject'],
'default': 'accept',
},
'cookie': {
'type': 'string',
},
'username': {
'type': 'string',
},
'promotion': {
"oneOf": [{"type": "array"}, {"type": "string"}],
# 'type': 'array',
# 'enum': ['free', 'twoupfree', 'halfdown', 'twouphalfdown', 'thirtypercent', 'none'],
'default': ['free'],
},
'not_hr': {
'type': 'boolean',
'enum': [True, False],
'default': False,
},
},
}
# Run later to avoid unnecessary lookups
@plugin.priority(115)
def on_task_filter(self, task, config):
# some time flexget do strange things, do this to prevent exception
if not task.entries:
return False
# check some details first
##check entry's link field
if not task.entries[0].get('link'):
log.critical('link not found, plz add "other_fields: [link]" to rss plugin config')
return False
##`not_hr` is only available for certain sites
if config['not_hr']:
if not re.findall('ourbits|totheglory', task.entries[0].get('link')):
log.critical('`not_hr` parameter is not available for this site')
return False
for entry in task.entries:
link = entry.get('link')
if config['action'] == 'accept':
if self.detect_promotion_status(link, config):
entry.accept('Entry `%s` is `%s`' % (entry['title'], config['promotion']), remember=True)
else:
entry.reject('Entry `%s` is not `%s`' % (entry['title'], config['promotion']))
def detect_promotion_status(self, link, config):
log.verbose('start to detect %s promotion status' % link)
cookie = config['cookie']
username = config['username']
# get detail page
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'accept-encoding': 'gzip, deflate',
'cookie': cookie,
}
try:
r = requests.get(link, headers=headers, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
response = r.text
log.verbose('get page succeed')
except:
log.critical('get page failed, please check connection')
try:
log.info(response)
except:
log.info(r.status_code)
finally:
return False
# assert login status
try:
assert username in response
log.verbose('cookie is valid')
except:
log.critical('cookie is expired or username not right, response is logged')
log.info(response)
return False
# assert torrent id
try:
assert '没有该ID的种子' not in response
assert '你没有该权限!' not in response
# log.verbose('torrent id is valid')
except:
log.critical('torrent id is not valid, torrent {} does not exist'.format(link))
log.info(response)
return False
# get details_dict
if "hdchina.org" in link:
details_dict = self.analyze_hdc_detail(response)
elif "tjupt.org" in link:
details_dict = self.analyze_tju_detail(response)
elif "ourbits.club" in link:
details_dict = self.analyze_ob_detail(response)
elif "npupt.com" in link:
details_dict = self.analyze_npu_detail(response)
elif "bt.byr.cn" in link:
details_dict = self.analyze_byr_detail(response)
elif "totheglory.im" in link:
details_dict = self.analyze_ttg_detail(response)
elif "chdbits.co" in link:
details_dict = self.analyze_chd_detail(response)
else:
details_dict = self.analyze_nexusphp_detail(response)
# process h&r
if config['not_hr'] and details_dict['is_hr']:
return False
# return accept or reject according to config['promotion']
if details_dict['promotion'] == config['promotion']:
return True
else:
return False
def analyze_hdc_detail(self, response):
convert = {
'Free': 'free',
'2X Free': 'twoupfree',
'50%': 'halfdown',
'2X 50%': 'twouphalfdown', # never seen, key maybe wrong
'30%': 'thirtypercent',
}
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h2', id="top")[0]
promotion_element = topic_element.img
if promotion_element:
promotion = convert[promotion_element['alt']]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
def analyze_nexusphp_detail(self, response):
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h1', id="top")[0]
promotion_element = topic_element.b
if promotion_element:
promotion = promotion_element.font['class'][0]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
def analyze_byr_detail(self, response):
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h1', id="share")[0]
promotion_element = topic_element.b
if promotion_element:
promotion = promotion_element.font['class'][0]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
def analyze_tju_detail(self, response):
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h1', id="top")[0]
promotion_element = topic_element.font
if promotion_element:
promotion = promotion_element['class'][0]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
def analyze_ob_detail(self, response):
details_dict = {}
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h1', id="top")[0]
promotion_element = topic_element.b
if promotion_element:
promotion = promotion_element.font['class'][0]
log.verbose('torrent promotion status is {}'.format(promotion))
details_dict['promotion'] = promotion
else:
log.verbose('torrent has no promotion')
details_dict['promotion'] = 'none'
hr_element = topic_element.img
if hr_element:
log.verbose('torrent is h&r')
details_dict['is_hr'] = True
else:
log.verbose('torrent is not h&r')
details_dict['is_hr'] = False
return details_dict
def analyze_npu_detail(self, response):
convert = {
'Free': 'free',
'2X Free': 'twoupfree',
'50%': 'halfdown',
'2X 50%': 'twouphalfdown',
'30%': 'thirtypercent',
}
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('div', class_="jtextfill")[0]
promotion_element = topic_element.span.img
if promotion_element:
promotion = convert[promotion_element['alt']]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
def analyze_ttg_detail(self, response):
convert = {
'free': 'free',
'half': 'halfdown',
'30': 'thirtypercent',
}
details_dict = {}
soup = BeautifulSoup(response, 'html.parser')
promotion_element = soup.find_all('img', class_="topic", src=re.compile(r".*pic/ico_.*"))
if promotion_element:
promotion_raw = re.findall(r'.*pic/ico_(.*).gif', promotion_element[0]['src'])[0]
try:
promotion = convert[promotion_raw]
log.verbose('torrent promotion status is {}'.format(promotion))
except:
promotion = ''
log.warning('torrent promotion status is {}, unsupported'.format(promotion_raw))
details_dict['promotion'] = promotion
else:
log.verbose('torrent has no promotion')
details_dict['promotion'] = 'none'
hr_element = soup.find_all('img', alt='Hit & Run')
if hr_element:
log.verbose('torrent is h&r')
details_dict['is_hr'] = True
else:
log.verbose('torrent is not h&r')
details_dict['is_hr'] = False
return details_dict
def analyze_chd_detail(self, response):
convert = {
'Free': 'free',
'2X Free': 'twoupfree',
'50%': 'halfdown',
'2X 50%': 'twouphalfdown',
'30%': 'thirtypercent',
}
soup = BeautifulSoup(response, 'html.parser')
topic_element = soup.find_all('h1', id="top")[0]
promotion_element = topic_element.img
if promotion_element:
promotion = convert[promotion_element['alt']]
log.verbose('torrent promotion status is {}'.format(promotion))
return {'promotion': promotion}
else:
log.verbose('torrent has no promotion')
return {'promotion': 'none'}
@event('plugin.register')
def register_plugin():
plugin.register(Filter_Promotion, 'promotion', api_ver=2)