forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserAgentSwitcher.py
More file actions
50 lines (40 loc) · 1.71 KB
/
UserAgentSwitcher.py
File metadata and controls
50 lines (40 loc) · 1.71 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
import pycurl
from pyload.core.network.browser import Browser
from pyload.core.network.http.http_request import HTTPRequest
from ..base.addon import BaseAddon
class UserAgentSwitcher(BaseAddon):
__name__ = "UserAgentSwitcher"
__type__ = "addon"
__version__ = "0.18"
__status__ = "testing"
__config__ = [
("enabled", "bool", "Activated", True),
("connecttimeout", "int", "Max timeout for link connection in seconds", 60),
("maxredirs", "int", "Maximum number of redirects to follow", 10),
(
"useragent",
"str",
"Custom user-agent string",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0",
),
]
__description__ = """Custom user-agent"""
__license__ = "GPLv3"
__authors__ = [("Walter Purcaro", "vuolter@gmail.com")]
def download_preparing(self, pyfile):
if not isinstance(pyfile.plugin.req, (HTTPRequest, Browser)):
return
connecttimeout = self.config.get("connecttimeout")
maxredirs = self.config.get("maxredirs")
useragent = self.config.get("useragent")
if connecttimeout:
self.log_debug(
"Setting connection timeout to {} seconds".format(connecttimeout)
)
pyfile.plugin.req.http.c.setopt(pycurl.CONNECTTIMEOUT, connecttimeout)
if maxredirs:
self.log_debug(f"Setting maximum redirections to {maxredirs}")
pyfile.plugin.req.http.c.setopt(pycurl.MAXREDIRS, maxredirs)
if useragent:
self.log_debug(f"Use custom user-agent string `{useragent}`")
pyfile.plugin.req.http.c.setopt(pycurl.USERAGENT, useragent.encode())