-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcookies.py
32 lines (28 loc) · 894 Bytes
/
cookies.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
from http.cookiejar import MozillaCookieJar
from http.cookiejar import LoadError
def tryFixCookieFile(filename):
print("Trying to fix cookie file")
f = open(filename)
lines = ['# Netscape HTTP Cookie File\n']
for l in f:
lines.append(l)
f.close()
f = open(filename,'w')
for l in lines:
print(l,file=f,end='')
f.close()
def tryLoadCookies(cookies: MozillaCookieJar):
try:
cookies.load(ignore_expires=True)
for cookie in cookies:
cookie.expires = 3107626352
return True
except LoadError:
print("Cookie has incorrect format, must have comment containing # Netscape on the top")
return False
def loadCookies(filename):
cookies = MozillaCookieJar(filename=filename)
if not tryLoadCookies(cookies):
tryFixCookieFile(filename)
tryLoadCookies(cookies)
return cookies