Repository navigation
Expand file tree
/
Copy pathRequestFactory.py
More file actions
135 lines (103 loc) · 4.24 KB
/
Copy pathRequestFactory.py
File metadata and controls
135 lines (103 loc) · 4.24 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay, RaNaN
"""
from threading import Lock
from Browser import Browser
from Bucket import Bucket
from HTTPRequest import HTTPRequest
from CookieJar import CookieJar
from XDCCRequest import XDCCRequest
class RequestFactory():
def __init__(self, core):
self.lock = Lock()
self.core = core
self.bucket = Bucket()
self.updateBucket()
self.cookiejars = {}
def iface(self):
return self.core.config["download"]["interface"]
def getRequest(self, pluginName, account=None, type="HTTP", **kwargs):
self.lock.acquire()
options = self.getOptions()
options.update(kwargs) # submit kwargs as additional options
if type == "XDCC":
req = XDCCRequest(self.bucket, options)
else:
req = Browser(self.bucket, options)
if account:
cj = self.getCookieJar(pluginName, account)
req.setCookieJar(cj)
else:
req.setCookieJar(CookieJar(pluginName))
self.lock.release()
return req
def getHTTPRequest(self, **kwargs):
""" returns a http request, dont forget to close it ! """
options = self.getOptions()
options.update(kwargs) # submit kwargs as additional options
return HTTPRequest(CookieJar(None), options)
def getURL(self, *args, **kwargs):
""" see HTTPRequest for argument list """
h = HTTPRequest(None, self.getOptions())
try:
rep = h.load(*args, **kwargs)
finally:
h.close()
return rep
def getCookieJar(self, pluginName, account=None):
if (pluginName, account) in self.cookiejars:
return self.cookiejars[(pluginName, account)]
cj = CookieJar(pluginName, account)
self.cookiejars[(pluginName, account)] = cj
return cj
def getProxies(self):
""" returns a proxy list for the request classes """
if not self.core.config["proxy"]["proxy"]:
return {}
else:
type = "http"
setting = self.core.config["proxy"]["type"].lower()
if setting == "socks4":
type = "socks4"
elif setting == "socks5":
type = "socks5"
username = None
if self.core.config["proxy"]["username"] and self.core.config["proxy"]["username"].lower() != "none":
username = self.core.config["proxy"]["username"]
pw = None
if self.core.config["proxy"]["password"] and self.core.config["proxy"]["password"].lower() != "none":
pw = self.core.config["proxy"]["password"]
return {
"type" : type,
"address" : self.core.config["proxy"]["address"],
"port" : self.core.config["proxy"]["port"],
"username": username,
"password": pw,
}
def getOptions(self):
"""returns options needed for pycurl"""
return {"interface": self.iface(),
"proxies" : self.getProxies(),
"ipv6" : self.core.config["download"]["ipv6"]}
def updateBucket(self):
""" set values in the bucket according to settings"""
if not self.core.config["download"]["limit_speed"]:
self.bucket.setRate(-1)
else:
self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024)
# needs pyreq in global namespace
def getURL(*args, **kwargs):
return pyreq.getURL(*args, **kwargs)
def getRequest(*args, **kwargs):
return pyreq.getHTTPRequest()