-
Notifications
You must be signed in to change notification settings - Fork 2
/
torss.py
471 lines (390 loc) · 15 KB
/
torss.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -*- coding: utf-8 -*-
"""
A script to rss and parse the info page of a torrent to get IMDb id,
add torrent to the qbit client with this IMDb id as a tag.
"""
import re
import argparse
import requests
from http.cookies import SimpleCookie
from torcp.tmdbparser import TMDbNameParser
import qbittorrentapi
import configparser
import feedparser
import datetime
import time
from sqlalchemy import Column, String, Integer, Float, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from urllib.parse import urlparse
from humanbytes import HumanBytes
# from lxml import etree
DOWNLOAD_URL_RE = [
r'download\.php\?id=(\d+)&downhash=(\w+)',
r'download\.php\?id=(\d+)&passkey=(\w+)',
]
class configData():
interval = 3
qbServer = ''
tmdb_api_key = ''
qbPort = ''
qbUser = ''
qbPass = ''
addPause = False
dryrun = False
CONFIG = configData()
def readConfig():
config = configparser.ConfigParser()
config.read('config.ini')
if 'TMDB' in config:
CONFIG.tmdb_api_key = config['TMDB'].get('api_key', '')
if 'QBIT' in config:
CONFIG.qbServer = config['QBIT'].get('server_ip', '')
CONFIG.qbPort = config['QBIT'].get('port', '')
CONFIG.qbUser = config['QBIT'].get('user', '')
CONFIG.qbPass = config['QBIT'].get('pass')
CONFIG.addPause = config['QBIT'].getboolean('pause', False)
CONFIG.dryrun = config['QBIT'].getboolean('dryrun', False)
ModelBase = declarative_base()
class MediaItem(ModelBase):
__tablename__ = 'media_list_table'
id = Column(Integer, primary_key=True)
# site = Column(String(64))
title = Column(String(255))
originalTitle = Column(String(255))
librarySectionID = Column(String(16))
audienceRating = Column(Float)
guid = Column(String(64))
key = Column(String(64))
imdb = Column(String(32))
tmdb = Column(String(32))
tvdb = Column(String(32))
doubanid = Column(String(32))
site = Column(String(32))
linkid = Column(String(32))
location0 = Column(String(255))
locationdirname = Column(String(255))
def __repr__(self):
return '<PlexItem %r>' % self.title
class RSSHistory(ModelBase):
__tablename__ = 'rss_history_table'
id = Column(Integer, primary_key=True)
# site = Column(String(64))
title = Column(String(255))
# imdbstr = Column(String(32))
# 初始化数据库连接:
engine = create_engine('sqlite:///instance/medialist.db')
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
SESSION = DBSession()
ModelBase.metadata.create_all(engine)
def rssGetDetailAndDownload(rsslink):
feed = feedparser.parse(rsslink)
rssFeedSum = 0
rssAccept = 0
for item in feed.entries:
rssFeedSum += 1
if not hasattr(item, 'id'):
print('!! No id')
continue
if not hasattr(item, 'title'):
print('!! No title')
continue
if existsRssHistory(item.title):
# print(" >> exists in rss history, skip")
continue
print("%d: %s (%s)" % (rssFeedSum, item.title,
datetime.datetime.now().strftime("%H:%M:%S")))
saveRssHistory(item.title)
if ARGS.title_regex:
if not re.search(ARGS.title_regex, item.title, re.I):
print(' >> TITLE_REGEX not match.')
continue
if ARGS.title_not_regex:
if re.search(ARGS.title_not_regex, item.title, re.I):
print(' >> TITLE_NOT_REGEX not match.')
continue
imdbstr = ''
if ARGS.cookie:
if hasattr(item, 'link'):
match, imdbstr, downlink, title = parseDetailPage(
item.link, ARGS.cookie)
if not match:
# print(' >> Info page regex not match.')
continue
if ARGS.exclude_no_imdb and (not imdbstr):
print(' >> Without IMDb')
continue
siteIdStr = getSiteId(item.link, imdbstr)
if hasattr(item, 'links') and len(item.links) > 1:
rssDownloadLink = item.links[1]['href']
rssSize = item.links[1]['length']
print(' %s (%s), %s' %
(imdbstr, HumanBytes.format(int(rssSize)), rssDownloadLink))
r = checkDupAddTor(item.title, rssDownloadLink,
imdbstr, siteIdStr, forceDownload=False)
print(' >> %d ' % r)
if r == 201:
# Download
rssAccept += 1
# print('Sleeping for %d seconds' % ARGS.sleep)
time.sleep(ARGS.sleep)
print('Total: %d, Accepted: %d (%s)' %
(rssFeedSum, rssAccept, datetime.datetime.now().strftime("%H:%M:%S")))
def validDownloadlink(downlink):
keystr = ['passkey', 'downhash', 'totheglory.im/dl/',
'totheglory.im/rssdd.php', 'download.php?hash=']
return any(x in downlink for x in keystr)
def searchTMDb(TmdbParser, title, imdb):
if imdb:
TmdbParser.parse(title, useTMDb=True, hasIMDbId=imdb)
else:
TmdbParser.parse(title, useTMDb=True)
return TmdbParser.tmdbid
def existsRssHistory(torname):
q = SESSION.query(RSSHistory.id).filter(RSSHistory.title == torname)
exists = SESSION.query(q.exists()).scalar()
return exists
def saveRssHistory(torname):
SESSION.add(RSSHistory(title=torname))
SESSION.commit()
def checkDatabaseExists(torTMDb):
q = SESSION.query(MediaItem.id).filter(MediaItem.tmdb == torTMDb)
exists = SESSION.query(q.exists()).scalar()
return exists
def checkDupAddTor(torname, downloadLink, imdbstr, siteIdStr, forceDownload=False):
if not torname:
return 400
if (not CONFIG.qbServer):
print("qBittorrent not set, skip")
return 400
if (not CONFIG.tmdb_api_key):
print("tmdb_api_key not set, skip")
return 400
p = TMDbNameParser(CONFIG.tmdb_api_key, '')
torTMDb = searchTMDb(p, torname, imdbstr)
if torTMDb > 0:
exists = checkDatabaseExists(torTMDb)
# exists = session.query(exists().where(
# MediaItem.tmdb == torTMDb)).scalar()
if (exists) and (not forceDownload):
return 202
else:
# print("Download: " + request.json['torname'] + " "+request.json['downloadlink'])
if downloadLink:
if not validDownloadlink(downloadLink):
print(" >> Not valid torrent downlink: %s ( %s) " %
(torname, downloadLink))
return 205
if not CONFIG.dryrun:
print(" >> Added: " + torname)
if not addQbitWithTag(downloadLink.strip(), imdbstr, siteIdStr):
return 400
else:
print(" >> DRYRUN: " + torname +
"\n >> " + downloadLink)
return 201
else:
# if CONFIG.download_no_imdb:
# if not addQbitWithTag(request.json['downloadlink'].strip(), imdbstr):
# abort(400)
return 203
def addQbitWithTag(downlink, imdbtag, siteIdStr=None):
qbClient = qbittorrentapi.Client(
host=CONFIG.qbServer, port=CONFIG.qbPort, username=CONFIG.qbUser, password=CONFIG.qbPass)
try:
qbClient.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
if not qbClient:
return False
try:
# curr_added_on = time.time()
if siteIdStr and ARGS.siteid_folder:
result = qbClient.torrents_add(
urls=downlink,
is_paused=CONFIG.addPause,
save_path=siteIdStr,
# download_path=download_location,
# category=timestamp,
tags=[imdbtag],
use_auto_torrent_management=False)
else:
result = qbClient.torrents_add(
urls=downlink,
is_paused=CONFIG.addPause,
tags=[imdbtag],
use_auto_torrent_management=False)
# breakpoint()
if 'OK' in result.upper():
pass
# print(' >> Torrent added.')
else:
print(' >> Torrent not added! something wrong with qb api ...')
except Exception as e:
print(' >> Torrent not added! Exception: '+str(e))
return False
return True
# def findConfig(infoUrl):
# hostnameList = urllib.parse.urlparse(infoUrl).netloc.split('.')
# abbrev = hostnameList[-2] if len(hostnameList) >= 2 else ''
# return next(filter(lambda ele: ele['host'] == abbrev, SITE_CONFIGS), None)
def tryFloat(fstr):
try:
f = float(fstr)
except:
f = 0.0
return f
def abbrevHostloc(url):
hostnameList = urlparse(url).netloc.split('.')
if len(hostnameList) == 2:
sitename = hostnameList[0]
elif len(hostnameList) == 3:
sitename = hostnameList[1]
else:
sitename = ''
SITE_ABBRES = [('chdbits', 'chd'), ('pterclub', 'pter'), ('audiences', 'aud'),
('lemonhd', 'lhd'), ('keepfrds', 'frds'), ('ourbits', 'ob'), ('springsunday', 'ssd')]
# result = next((i for i, v in enumerate(SITE_ABBRES) if v[0] == sitename), "")
abbrev = [x for x in SITE_ABBRES if x[0] == sitename]
return abbrev[0][1] if abbrev else sitename
def getSiteId(detailLink, imdbstr):
siteAbbrev = abbrevHostloc(detailLink)
if (siteAbbrev == "ttg"):
m = re.search(r"t\/(\d+)", detailLink, flags=re.A)
else:
m = re.search(r"id=(\d+)", detailLink, flags=re.A )
sid = m[1] if m else ""
if imdbstr:
sid = sid + "_" + imdbstr
return siteAbbrev + "_" + sid
def parseDetailPage(pageUrl, pageCookie):
cookie = SimpleCookie()
cookie.load(pageCookie)
cookies = {k: v.value for k, v in cookie.items()}
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
'Content-Type': 'text/html; charset=UTF-8'
}
r = requests.get(pageUrl, headers=headers, cookies=cookies)
r.encoding = r.apparent_encoding
doc = r.text
topTitle = ''
mt = re.search(r'id=\"top\"[^>]*>([^<\n]*)', doc, flags=re.A)
if mt:
topTitle = mt[1].replace(" ", "")
print("Detail page title: " + topTitle)
if ARGS.info_regex:
if not re.search(ARGS.info_regex, doc, flags=re.A):
print(' >> INFO_REGEX not match.')
return False, '', '', ''
if ARGS.info_not_regex:
if re.search(ARGS.info_not_regex, doc, flags=re.A):
print(' >> INFO_NOT_REGEX not match.')
return False, '', '', ''
if ARGS.min_imdb:
imdbval = 0
m1 = re.search(r'IMDb.*?([0-9.]+)\s*/\s*10', doc, flags=re.A)
if m1:
imdbval = tryFloat(m1[1])
doubanval = 0
m2 = re.search(r'豆瓣评分.*?([0-9.]+)/10', doc, flags=re.A)
if m2:
doubanval = tryFloat(m2[1])
if imdbval < 1 and doubanval < 1:
ratelist = [x[1] for x in re.finditer(
r'Rating:.*?([0-9.]+)\s*/\s*10\s*from', doc, flags=re.A)]
if len(ratelist) >= 2:
doubanval = tryFloat(ratelist[0])
imdbval = tryFloat(ratelist[1])
elif len(ratelist) == 1:
#TODO: 不分辨douban/imdb了
doubanval = tryFloat(ratelist[0])
imdbval = doubanval
# rate1 = re.search(r'Rating:.*?([0-9.]+)\s*/\s*10\s*from', doc, flags=re.A)
# if rate1:
# imdbval = tryFloat(rate1[1])
print(" >> IMDb: %s, douban: %s" % (imdbval, doubanval))
if (imdbval < ARGS.min_imdb) and (doubanval < ARGS.min_imdb):
print(" >> MIN_IMDb not match")
return False, '', '', ''
imdbstr = ''
# imdbRe = r'IMDb(链接)\s*(\<.[!>]*\>)?.*https://www\.imdb\.com/title/tt(\d+)'
imdbRe = r'www\.imdb\.com\/title\/(tt\d+)'
m1 = re.search(imdbRe, doc, flags=re.A)
if m1:
imdbstr = m1[1]
for reUrl in DOWNLOAD_URL_RE:
if re.search(reUrl, doc, flags=re.A):
break
downlink = ''
m2 = re.search(reUrl, doc, flags=re.A)
if m2:
downlink = m2[0]
if downlink:
parsed_uri = urlparse(pageUrl)
downlink = '{uri.scheme}://{uri.netloc}/{relink}'.format(
uri=parsed_uri, relink=downlink)
return True, imdbstr, downlink, topTitle
def loadArgs():
parser = argparse.ArgumentParser(
description='A script to rss pt site, add torrent to qbit with IMDb id as a tag.'
)
parser.add_argument('-R', '--rss', help='the rss link.')
parser.add_argument(
'-s', '--single-page', help='fetch single torrent in detail page.')
parser.add_argument(
'-c', '--cookie', help='the cookie to the detail page.')
parser.add_argument('--title-regex', help='regex to match the rss title.')
parser.add_argument('--title-not-regex',
help='regex to not match the rss title.')
parser.add_argument(
'--info-regex', help='regex to match the info/detail page.')
parser.add_argument('--info-not-regex',
help='regex to not match the info/detail page.')
parser.add_argument('--sleep', type=int,
help='sleep between each request of info page.')
parser.add_argument('--add-pause',
action='store_true',
help='Add torrent in PAUSE state.')
parser.add_argument('--exclude-no-imdb',
action='store_true',
help='Do not download without IMDb')
parser.add_argument('--min-imdb', type=int,
help='filter imdb greater than <MIN_IMDb>.')
parser.add_argument('--siteid-folder', action='store_true',
help='make Site_Id_Imdb parent folder.')
parser.add_argument('--init-rss-history', action='store_true',
help='Init/Empty rss history table.')
global ARGS
ARGS = parser.parse_args()
if not ARGS.sleep:
ARGS.sleep = 5
def initRssHistory():
print("Init Database....")
num_rows_deleted = SESSION.query(RSSHistory).delete()
SESSION.commit()
print("%d rows deleted" % num_rows_deleted)
return num_rows_deleted
def main():
loadArgs()
readConfig()
if ARGS.init_rss_history:
initRssHistory()
if ARGS.rss:
rssGetDetailAndDownload(ARGS.rss)
elif ARGS.single_page:
if ARGS.cookie:
match, imdbstr, downlink, title = parseDetailPage(
ARGS.single_page, ARGS.cookie)
if not downlink:
print("Error: download link not found")
return
siteIdStr = getSiteId(ARGS.single_page, imdbstr)
print(imdbstr, downlink)
r = checkDupAddTor(title, downlink, imdbstr, siteIdStr, forceDownload=False)
print(' >> %d ' % r)
# r = addQbitWithTag(downlink, imdbstr)
if __name__ == '__main__':
main()