forked from DiMartinoXBMC/plugin.video.torrenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Content.py
256 lines (221 loc) · 7.87 KB
/
Content.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
# -*- coding: utf-8 -*-
'''
Torrenter plugin for Kodi
Copyright (C) 2012 DiMartino
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import abc
import urllib
import urllib2
import cookielib
import re
from StringIO import StringIO
import gzip
import HTMLParser
import Localization
class Content:
__metaclass__ = abc.ABCMeta
searchIcon = '/icons/video.png'
sourceWeight = 1
cookieJar = None
baseurl = ''
def isLabel(self):
return 'Should search on ruhunt?'
def isPages(self):
return False
def isSort(self):
return False
def isScrappable(self):
return True
def isInfoLink(self):
return False
def isSearchOption(self):
return True
category_dict = {
'sites': ('[B]by Site[/B]',),
'search': ('[B]Search[/B]',),
'movies': ('Movies',),
'rus_movies': ('Russian Movies',),
'tvshows': ('TV Shows',),
'cartoons': ('Cartoons',),
'hot': ('Most Recent',),
'top': ('Top All Time',),
'anime': ('Anime',),
'year': {'year': 'by Year', },
'genre': {'genre': 'by Genre',
'action': ('Action',),
'comedy': ('Comedy',),
'documentary': ('Documentary',),
'drama': ('Drama',),
'fantasy': ('Fantasy',),
'horror': ('Horror',),
'romance': ('Romance',),
'thriller': ('Thriller',),
}
}
for y in range(2015, 1970, -1):
category_dict['year'][str(y)] = (str(y), '/top/y/%s/' % str(y))
def get_contentList(self, category, subcategory=None, apps_property=None):
'''
Retrieve keyword from the input and return a list of tuples:
filesList.append((
int(weight),
int(seeds),
str(title),
str(link),
str(image),
))
'''
return
def has_category(self, category, subcategory=None):
has_category = False
if not subcategory or subcategory == True:
if category in self.category_dict.keys():
has_category = True
else:
if category in self.category_dict:
cat_con = self.category_dict[category]
if isinstance(cat_con, dict):
if subcategory in cat_con.keys():
has_category = True
return has_category
def get_url(self, category, subcategory, apps_property):
page=None
sort=None
if apps_property:
page=apps_property.get('page')
sort=apps_property.get('sort')
if not subcategory or subcategory == True or category == 'search':
get = self.category_dict[category]
else:
get = self.category_dict[category][subcategory]
if category == 'search': get = (get[0], get[1] % urllib.quote_plus(subcategory.encode('utf-8')))
property = self.get_property(category, subcategory)
if not page or page == 1:
url = self.baseurl + get[1]
elif property:
page_url = property['page'] % (property['second_page'] + ((page - 2) * property['increase']))
url = self.baseurl + str(page_url)
if property and property.get('sort'):
sort_dict=property['sort'][sort]
if sort_dict.get('url_after'):
page_url = sort_dict['url_after']
url = url + page_url
return url
def get_property(self, category, subcategory=None):
has_property = False
property = {}
if not subcategory or subcategory == True:
if category in self.category_dict.keys():
try:
property = self.category_dict[category][2]
if isinstance(property, dict):
has_property = True
except:
pass
else:
if category in self.category_dict:
cat_con = self.category_dict[category]
if isinstance(cat_con, dict):
if subcategory in cat_con.keys():
try:
property = cat_con[subcategory][2]
if isinstance(property, dict):
has_property = True
except:
pass
if has_property:
if category == 'search': property['page'] = property['page'] % urllib.quote_plus(
subcategory.encode('utf-8'))
return property
def makeRequest(self, url, data={}, headers=[]):
self.cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar))
opener.addheaders = headers
if 0 < len(data):
encodedData = urllib.urlencode(data)
else:
encodedData = None
response = opener.open(url, encodedData)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO(response.read())
f = gzip.GzipFile(fileobj=buf)
response = f.read()
else:
response = response.read()
return response
htmlCodes = (
('&', '&'),
('<', '<'),
('>', '>'),
('"', '"'),
("'", '''),
(' ', ' ',),
('"', '«', ),
('"', '»', ),
('·', '·',),
('e', 'é',),
('e', 'è',),
('&', '&',),
('u', 'ù',),
('u', 'ú',),
('o', 'ô',),
('u', 'û'),
('-', '–'),
)
stripPairs = (
('<p>', '\n'),
('<li>', '\n'),
('<br>', '\n'),
('<.+?>', ' '),
('</.+?>', ' '),
( ' ', ' ',),
('«', '"',),
('»', '"', ),
('–', '-'),
)
def unescape(self, string):
try:
pars = HTMLParser.HTMLParser()
return pars.unescape(string)
except:
return string
def stripHtml(self, string):
for (html, replacement) in self.stripPairs:
string = re.sub(html, replacement, string)
return string
def translate(self, category, subcategory=None):
if not subcategory:
if isinstance(self.category_dict.get(category), dict):
return self.localize(self.category_dict.get(category).get(category))
else:
return self.localize(self.category_dict.get(category)[0])
else:
return self.localize(self.category_dict.get(category).get(subcategory)[0])
def localize(self, string):
if string:
try:
return Localization.localize(string)
except:
return string
else:
return 'Empty string'
def sizeConvert(self, sizeBytes):
if long(sizeBytes) >= 1024 * 1024 * 1024:
size = str(long(sizeBytes) / (1024 * 1024 * 1024)) + 'GB'
elif long(sizeBytes) >= 1024 * 1024:
size = str(long(sizeBytes) / (1024 * 1024)) + 'MB'
elif sizeBytes >= 1024:
size = str(long(sizeBytes) / 1024) + 'KB'
else:
size = str(long(sizeBytes)) + 'B'
return size