-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
174 lines (155 loc) · 6.39 KB
/
main.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
from lxml import html
from couchpotato.core.helpers.encoding import tryUrlencode, toUnicode
from couchpotato.core.helpers.variable import tryInt, getIdentifier
from couchpotato.core.logger import CPLog
from couchpotato.core.media._base.providers.torrent.base import TorrentProvider
from couchpotato.core.media.movie.providers.base import MovieProvider
import traceback
import re
import time
import requests
import sys
import PTN
log = CPLog(__name__)
class TorrentDetails(object):
def __init__(self, seeders, leechers, downlink, torrentname, filesize, quality, resolution, imdb, group, detail, score):
self.seeders = seeders
self.leechers = leechers
self.downlink = downlink
self.torrentname = torrentname
self.filesize = filesize
self.quality = quality
self.resolution = resolution
self.imdb = imdb
self.group = group
self.detail = detail
self.score = score
class PirateTheNet(TorrentProvider, MovieProvider):
default_search_params = {
'sort': 'browsedate',
'skw': 'showall',
'compression': 'unraredonly',
'packs': 'torrentsonly',
'titleonly': 'true',
'subscriptions': 'showall',
'visibility': 'aliveonly',
'visiblecategories': 'Action,Adventure,Animation,Biography,Comedy,Crime,Documentary,Drama,Eastern,Family,Fantasy,'
'History,Holiday,Horror,Kids,Musical,Mystery,Romance,Sci-Fi,Short,Sports,Thriller,War,Western',
'hiddenqualities': 'FLAC,MP3',
'order': 'DESC',
'action': 'torrentstable',
'bookmarks': 'showall',
'viewtype': 1,
'page': 1
}
urls = {
'baseurl': 'http://piratethenet.org',
'login': 'http://piratethenet.org/takelogin.php',
'login_check': 'http:/piratethenet.org/index.php',
'search': 'http://piratethenet.org/torrentsutils.php'
}
session = requests.Session()
def _searchOnTitle(self, title, movie, quality, results):
if not self.session.cookies or not self.session.passkey:
try:
r = self.session.post(self.urls['login'], data=self.getLoginParams(), verify=False)
except requests.RequestException as e:
log.info('Error while logging in to PtN: %s', e)
passkey = re.search('passkey=([\d\w]+)"', r.text)
if passkey:
self.session.passkey = passkey.group(1)
else:
log.info('PtN cookie info invalid')
search_params = self.default_search_params.copy()
search_params['searchstring'] = title
# SKW?
if(self.conf('skw')):
search_params['skw'] = 'skwonly'
log.info("Searching for SKALiWAGZ only")
try:
r = self.session.get(self.urls['search'], params=search_params)
except requests.RequestException as e:
log.info('Error searching ptn: %s' % e)
log.info(str(tree))
tree = html.fromstring(r.content)
# Get number of seeders
seederList = tree.xpath('//a[@alt="Number of Seeders"]/text()')
# Get number of leechers (doesn't work)
leecherList = tree.xpath('//span[@alt="Number of Leechers"]/text()')
# Get torrent size
sizeList = tree.xpath('//span[@alt="Torrent size"]/text()')
# Get download links and IMDB ID
tDownload = tree.xpath('//a//@href')
downloadList = []
imdbDict = {}
imdbCount = 0
imdbList = []
maxRun = 0
detailList = []
for dl in tDownload:
if maxRun >= 20:
break
if "redir.php?url=http://imdb.com/title/" in dl:
imdbDict.update({str(imdbCount) : dl[-9:]})
imdbCount += 1
if "download" in dl:
downloadList.append(self.urls['baseurl']+dl)
maxRun += 1
imdbList.append(imdbDict[str(imdbCount-1)])
if "detail.php" in dl and "&" not in dl:
detailList.append(self.urls['baseurl']+dl[:7]+'s'+dl[7:])
# Get torrent name
tID = tree.xpath('//a[@class="lightview"]/text()')
idList = []
maxRun = 0
for dl in tID:
if maxRun >= 20:
break
if len(dl) > 5:
idList.append(dl)
maxRun += 1
torrentList = []
maxRun = min(len(seederList),maxRun)
maxRun = min(len(downloadList),maxRun)
maxRun = min(len(idList),maxRun)
maxRun = min(len(sizeList),maxRun)
maxRun = min(len(detailList),maxRun)
for i in range(0,maxRun):
tmp = PTN.parse(idList[i])
torrentdata = TorrentDetails(0,0,'','','','','','','','',0)
torrentdata.seeders = int(seederList[i])
torrentdata.leechers = 0
torrentdata.downlink = str(downloadList[i])
torrentdata.torrentname = str(idList[i])
torrentdata.size = str(sizeList[i])
torrentdata.detail = str(detailList[i])
if("quality" in tmp):
torrentdata.quality = str(tmp["quality"])
if("resolution" in tmp):
torrentdata.resolution = str(tmp["resolution"])
if("group" in tmp):
torrentdata.group = str(tmp["group"])
if(self.conf('pref_skw')):
if(str(tmp["group"]) == 'SKALiWAGZ'):
torrentdata.score += 1000
torrentdata.imdb = str(imdbList[i])
if(torrentdata.resolution == quality["custom"]["quality"] and torrentdata.imdb == movie["identifiers"]["imdb"]):
torrentList.append(torrentdata)
torrentdata.score += self.conf('extra_score')
log.info('Found %d torrents' % len(torrentList))
for torrent in torrentList:
log.info('PirateTheNet found ' + torrent.torrentname)
results.append({
'leechers' : torrent.leechers,
'seeders' : torrent.seeders,
'name' : torrent.torrentname,
'url' : torrent.downlink,
'detail_url' : torrent.detail,
'id' : '',
'size' : self.parseSize(torrent.size)
})
def getLoginParams(self):
return {
'password': str(self.conf('password')),
'loginkey': str(self.conf('key'))
}