-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.py
34 lines (25 loc) · 775 Bytes
/
init.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
import cookielib
import urllib2
import logging
import atexit
cookie_store = "cookies.txt"
logger = logging.getLogger('init')
def init():
logging.basicConfig(level=logging.DEBUG)
cj = cookielib.MozillaCookieJar(cookie_store)
try:
cj.load()
logger.info('cookies loaded')
except:
logger.info('no cookies present')
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = (
('User-agent',
'Mozilla/5.0 (X11; Linux x86_64; rv:46.0.1) Gecko/20100101 Firefox/46.0.1'),)
urllib2.install_opener(opener)
state = {'cookiejar': cj}
atexit.register(save_cookies, state)
return state
def save_cookies(state):
state['cookiejar'].save()
logging.info('cookies saved')