-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
96 lines (75 loc) · 2.18 KB
/
util.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
#!/usr/bin/env python
# coding: utf-8
import time
import urllib
import urllib2
import cookielib
import functools
HEADERS = {
'User-Agent': 'User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 '
'(KHTML, like Gecko) Chrome/13.0.772.0 Safari/535.1',
'Referer': 'http://www.cuevana.tv/',
'Accept': 'text/html,application/xhtml+xml,application/xml;'}
RETRY_TIMES = 5
def retry(callback):
"""
Retry decorator.
"""
@functools.wraps(callback)
def deco(*args, **kwargs):
tried = 0
while tried < RETRY_TIMES:
try:
return callback(*args, **kwargs)
except Exception, error:
tried += 1
time.sleep(1)
error = 'Can\'t download\nerror: "%s"\n args: %s' % \
(error, str(args) + str(kwargs))
raise Exception(error)
return deco
class UrlOpen(object):
"""
An url opener with cookies support.
"""
def __init__(self):
self.setup_cookies()
@retry
def __call__(self, url, data=None, filename=None, handle=False):
if data:
request = urllib2.Request(url, urllib.urlencode(data), HEADERS)
else:
request = urllib2.Request(url, headers=HEADERS)
rc = self.opener.open(request)
# return file handler only
if handle:
return rc
local = None
if filename:
local = open(filename, 'wb')
ret = ''
while True:
buffer = rc.read(1024)
if buffer == '':
break
if local:
local.write(buffer)
else:
ret += buffer
if local:
local.close()
return
return ret
def setup_cookies(self):
"""
Setup cookies in urllib2.
"""
jar = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(jar)
self.opener = urllib2.build_opener(handler)
def add_headers(self, headers):
"""
Add new headers.
`headers' argument has to be a diccionary.
"""
self.opener.addheaders.extend(headers.items())