Skip to content

Commit 4635807

Browse files
authored
More Py3 changes
1 parent 218c33e commit 4635807

File tree

9 files changed

+68
-32
lines changed

9 files changed

+68
-32
lines changed

lib/openscrapers/modules/cache.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ def get(function, duration, *args):
3232
cache_result = cache_get(key)
3333
if cache_result:
3434
if _is_cache_valid(cache_result['date'], duration):
35-
return ast.literal_eval(cache_result['value'].encode('utf-8'))
35+
try:
36+
result = ast.literal_eval(cache_result['value'].encode('utf-8'))
37+
except:
38+
result = ast.literal_eval(cache_result['value'])
39+
return result
3640

3741
fresh_result = repr(function(*args))
3842
if not fresh_result:
@@ -42,7 +46,11 @@ def get(function, duration, *args):
4246
return None
4347

4448
cache_insert(key, fresh_result)
45-
return ast.literal_eval(fresh_result.encode('utf-8'))
49+
try:
50+
result = ast.literal_eval(fresh_result.encode('utf-8'))
51+
except:
52+
result = ast.literal_eval(fresh_result)
53+
return result
4654
except:
4755
log_utils.error()
4856
return None

lib/openscrapers/modules/client.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@
4343
except ImportError:
4444
from urllib.parse import urlencode, quote_plus
4545

46+
try:
47+
# Python 2 forward compatibility
48+
range = xrange
49+
except NameError:
50+
pass
51+
4652
from openscrapers.modules import cache
4753
from openscrapers.modules import dom_parser
4854
from openscrapers.modules import log_utils
4955
from openscrapers.modules import workers
5056

51-
5257
def request(url, close=True, redirect=True, error=False, proxy=None, post=None, headers=None, mobile=False, XHR=False,
5358
limit=None, referer=None, cookie=None, compression=True, output='', timeout='30', ignoreSsl=False,
5459
flare=True, ignoreErrors=None):
@@ -135,7 +140,9 @@ def http_response(self, request, response):
135140

136141
if isinstance(post, dict):
137142
# Gets rid of the error: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
138-
for key, value in post.iteritems():
143+
try: iter_items = post.iteritems()
144+
except: iter_items = post.items()
145+
for key, value in iter_items:
139146
try:
140147
post[key] = value.encode('utf-8')
141148
except:
@@ -391,7 +398,10 @@ def _get_result(response, limit=None):
391398

392399
def parseDOM(html, name='', attrs=None, ret=False):
393400
if attrs:
394-
attrs = dict((key, re.compile(value + ('$' if value else ''))) for key, value in attrs.iteritems())
401+
try:
402+
attrs = dict((key, re.compile(value + ('$' if value else ''))) for key, value in attrs.iteritems())
403+
except:
404+
attrs = dict((key, re.compile(value + ('$' if value else ''))) for key, value in attrs.items())
395405
results = dom_parser.parse_dom(html, name, attrs, ret)
396406

397407
if ret:
@@ -417,7 +427,7 @@ def _replaceHTMLCodes(txt):
417427

418428
def randomagent():
419429
BR_VERS = [
420-
['%s.0' % i for i in xrange(18, 50)],
430+
['%s.0' % i for i in range(18, 50)],
421431
['37.0.2062.103', '37.0.2062.120', '37.0.2062.124', '38.0.2125.101', '38.0.2125.104', '38.0.2125.111',
422432
'39.0.2171.71', '39.0.2171.95', '39.0.2171.99', '40.0.2214.93', '40.0.2214.111', '40.0.2214.115',
423433
'42.0.2311.90', '42.0.2311.135', '42.0.2311.152', '43.0.2357.81', '43.0.2357.124', '44.0.2403.155',
@@ -449,11 +459,11 @@ def __init__(self):
449459
def get(self, netloc, ua, timeout):
450460
threads = []
451461

452-
for i in range(0, 15):
462+
for i in list(range(0, 15)):
453463
threads.append(workers.Thread(self.get_cookie, netloc, ua, timeout))
454464
[i.start() for i in threads]
455465

456-
for i in range(0, 30):
466+
for i in list(range(0, 30)):
457467
if self.cookie is not None:
458468
return self.cookie
459469
time.sleep(1)

lib/openscrapers/modules/dom_parser.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ def __get_dom_elements(item, name, attrs):
5454
this_list = re.findall(pattern, item, re.M | re.S | re.I)
5555
else:
5656
last_list = None
57-
58-
for key, value in attrs.iteritems():
57+
try:
58+
iter_items = attrs.iteritems()
59+
except:
60+
iter_items = attrs.items()
61+
for key, value in iter_items:
5962
value_is_regex = isinstance(value, re_type)
6063
value_is_str = isinstance(value, basestring)
6164
pattern = '''(<{tag}[^>]*\s{key}=(?P<delim>['"])(.*?)(?P=delim)[^>]*>)'''.format(tag=name, key=key)
@@ -105,10 +108,12 @@ def parse_dom(html, name='', attrs=None, req=False, exclude_comments=False):
105108
if attrs is None:
106109
attrs = {}
107110
name = name.strip()
108-
109-
if isinstance(html, unicode) or isinstance(html, DomMatch):
111+
try:
112+
if isinstance(html, unicode):
113+
html = [html]
114+
except: pass
115+
if isinstance(html, DomMatch):
110116
html = [html]
111-
112117
elif isinstance(html, str):
113118
try:
114119
html = [html.decode("utf-8")] # Replace with chardet thingy
@@ -117,9 +122,10 @@ def parse_dom(html, name='', attrs=None, req=False, exclude_comments=False):
117122
html = [html.decode("utf-8", "replace")]
118123
except:
119124
html = [html]
125+
120126
elif not isinstance(html, list):
121127
return ''
122-
128+
123129
if not name:
124130
return ''
125131

lib/openscrapers/modules/getSum.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except ImportError:
99
from html.parser import HTMLParser
1010

11+
from openscrapers.modules.utils import byteify
1112
from openscrapers.modules import log_utils
1213

1314
headers = {
@@ -54,7 +55,7 @@ def findSum(self, text, type=None):
5455
links = re.compile(self._magnet_regex).findall(text)
5556
if links:
5657
for link in links:
57-
link = str(replaceHTMLCodes(link).encode('utf-8').split('&tr')[0])
58+
link = str(byteify(replaceHTMLCodes(link)).split('&tr')[0])
5859
link = "magnet:" + link if not link.startswith('magnet') else link
5960
if link in self.links:
6061
continue
@@ -177,7 +178,7 @@ def get_video(text):
177178
match = re.compile(pattern).findall(text)
178179
links = []
179180
for url in match:
180-
links.append(url.encode('utf-8'))
181+
links.append(byteify(url))
181182
return links
182183

183184

lib/openscrapers/modules/jsunfuck.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,19 @@ def cfunfuck(fuckedup):
220220
fuck = re.findall(r's,t,o,p,b,r,e,a,k,i,n,g,f,\s*(\w+=).*?:\+?\(?(.*?)\)?\}', fuckedup)
221221
fucks = re.findall(r'(\w+)\.\w+([\+\-\*\/]=)\+?\(?(.*?)\)?;', fuckedup)
222222
endunfuck = fuck[0][0].split('=')[0]
223-
unfuck = JSUnfuck(fuck[0][1]).decode()
223+
try:
224+
unfuck = JSUnfuck(fuck[0][1]).decode()
225+
except:
226+
unfuck = JSUnfuck(fuck[0][1])
224227
unfuck = re.sub(r'[\(\)]', '', unfuck)
225228
unfuck = fuck[0][0] + unfuck
226229
exec (unfuck)
227230

228231
for fucker in fucks:
229-
unfucker = JSUnfuck(fucker[2]).decode()
232+
try:
233+
unfucker = JSUnfuck(fucker[2]).decode()
234+
except:
235+
unfucker = JSUnfuck(fucker[2])
230236
unfucker = re.sub(r'[\(\)]', '', unfucker)
231237
unfucker = fucker[0] + fucker[1] + unfucker
232238
exec (unfucker)
@@ -238,7 +244,10 @@ def main():
238244
with open(sys.argv[1]) as f:
239245
start_js = f.read()
240246

241-
print JSUnfuck(start_js).decode()
247+
try:
248+
print JSUnfuck(start_js).decode()
249+
except:
250+
print JSUnfuck(start_js)
242251

243252

244253
if __name__ == '__main__':

lib/openscrapers/modules/log_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def log(msg, caller=None, level=LOGNOTICE):
3535
debug_enabled = control.setting('debug.enabled')
3636
debug_log = control.setting('debug.location')
3737

38-
print DEBUGPREFIX + ' Debug Enabled?: ' + str(debug_enabled)
39-
print DEBUGPREFIX + ' Debug Log?: ' + str(debug_log)
38+
print( DEBUGPREFIX + ' Debug Enabled?: ' + str(debug_enabled))
39+
print( DEBUGPREFIX + ' Debug Log?: ' + str(debug_log))
4040

4141
if control.setting('debug.enabled') != 'true':
4242
return
@@ -50,9 +50,11 @@ def log(msg, caller=None, level=LOGNOTICE):
5050

5151
if caller is not None and level == LOGERROR:
5252
msg = 'From func name: %s.%s() Line # :%s\n msg : %s'%(caller[0], caller[1], caller[2], msg)
53-
54-
if isinstance(msg, unicode):
55-
msg = '%s (ENCODED)' % (msg.encode('utf-8'))
53+
try:
54+
if isinstance(msg, unicode):
55+
msg = '%s (ENCODED)' % (msg.encode('utf-8'))
56+
except:
57+
pass
5658

5759
if not control.setting('debug.location') == '0':
5860
log_file = os.path.join(LOGPATH, 'openscrapers.log')

lib/openscrapers/modules/regex.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import xbmc
2424
import xbmcaddon
2525

26-
profile = functions_dir = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile').decode('utf-8'))
26+
profile = functions_dir = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile'))
2727

2828
try:
2929
from sqlite3 import dbapi2 as database
3030
except:
3131
from pysqlite2 import dbapi2 as database
3232

33+
from openscrapers.modules.utils import byteify
3334
from openscrapers.modules import client
3435
from openscrapers.modules import control
3536

@@ -93,7 +94,7 @@ def resolve(regex):
9394

9495
url = regex.split('<regex>', 1)[0].strip()
9596
url = client.replaceHTMLCodes(url)
96-
url = url.encode('utf-8')
97+
url = byteify(url)
9798

9899
r = getRegexParsed(regexs, url)
99100

lib/openscrapers/modules/source_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ def strip_domain(url):
273273
if url.lower().startswith('http') or url.startswith('/'):
274274
url = re.findall('(?://.+?|)(/.+)', url)[0]
275275
url = client.replaceHTMLCodes(url)
276-
url = url.encode('utf-8')
276+
try:
277+
url = url.encode('utf-8')
278+
except:
279+
pass
277280
return url
278281
except:
279282
log_utils.error()

lib/openscrapers/modules/workers.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55

66
import threading
77

8-
98
class Thread(threading.Thread):
109
def __init__(self, target, *args):
1110
self._target = target
1211
self._args = args
13-
threading.Thread.__init__(self)
14-
15-
def run(self):
16-
self._target(*self._args)
12+
threading.Thread.__init__(self, target=self._target, args=self._args)

0 commit comments

Comments
 (0)