forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixivBrowserFactory.py
68 lines (54 loc) · 1.95 KB
/
PixivBrowserFactory.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
# -*- coding: UTF-8 -*-
from mechanize import Browser
import mechanize
import cookielib
import socket
import PixivHelper
defaultCookieJar = None
defaultConfig = None
def getBrowser(config = None, cookieJar = None):
global defaultCookieJar
global defaultConfig
if config != None:
defaultConfig = config
if cookieJar != None:
defaultCookieJar = cookieJar
if defaultCookieJar == None:
PixivHelper.GetLogger().info("No default cookie jar available, creating... ")
defaultCookieJar = cookielib.LWPCookieJar()
browser = Browser(factory=mechanize.RobustFactory())
configureBrowser(browser, defaultConfig)
configureCookie(browser, defaultCookieJar)
return browser
def configureBrowser(browser, config):
if config == None:
PixivHelper.GetLogger().info("No config given")
return
global defaultConfig
if defaultConfig == None:
defaultConfig = config
if config.useProxy:
browser.set_proxies(config.proxy)
PixivHelper.GetLogger().info("Using Proxy: " + config.proxyAddress)
browser.set_handle_equiv(True)
#browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(config.useRobots)
browser.set_debug_http(config.debugHttp)
if config.debugHttp :
PixivHelper.GetLogger().info('Debug HTTP enabled.')
browser.visit_response
browser.addheaders = [('User-agent', config.useragent)]
socket.setdefaulttimeout(config.timeout)
def configureCookie(browser, cookieJar):
if cookieJar != None:
browser.set_cookiejar(cookieJar)
global defaultCookieJar
if defaultCookieJar == None:
defaultCookieJar = cookieJar
def addCookie(cookie):
global defaultCookieJar
if defaultCookieJar == None:
defaultCookieJar = cookielib.LWPCookieJar()
defaultCookieJar.set_cookie(cookie)