]> git.phdru.name Git - bookmarks_db.git/commitdiff
Feat(Robots/bkmk_rrequests): Add request headers
authorOleg Broytman <phd@phdru.name>
Sun, 3 Mar 2024 10:10:13 +0000 (13:10 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 3 Mar 2024 10:25:40 +0000 (13:25 +0300)
Robots/bkmk_rrequests.py
doc/ChangeLog

index 114d67094586ed97d928768803aed3704f075790..54b4c625d6bcf28cfb4497099cebba5fd2c39426 100644 (file)
@@ -16,7 +16,7 @@ from urllib.parse import urlsplit
 import requests
 import requests_ftp
 
-from Robots.bkmk_robot_base import robot_base
+from Robots.bkmk_robot_base import robot_base, request_headers
 
 requests_ftp.monkeypatch_session()
 
@@ -38,16 +38,23 @@ class robot_requests(robot_base):
         if url_host in self.proxy_error:
             return 'proxy error', None, None, None, None
 
+        if accept_charset and bookmark.charset:
+            headers = request_headers.copy()
+            headers['Accept-Charset'] = bookmark.charset
+        else:
+            headers = request_headers
+
         if url_host in self.proxy_ok:
             self.log('   Immediately trying with the proxy')
-            error, r = request_get(url, self.timeout, self.proxy)
+            error, r = request_get(url, headers, self.timeout, self.proxy)
         else:
-            error, r = request_get(url, self.timeout, None)
+            error, r = request_get(url, headers, self.timeout, None)
             if error is not None:
                 self.log('   Error: %s' % error)
                 if self.proxy:
                     self.log('   Retrying with the proxy...')
-                    error, r = request_get(url, self.timeout, self.proxy)
+                    error, r = request_get(url, headers,
+                                           self.timeout, self.proxy)
                     if error is None:
                         self.proxy_ok.add(url_host)
         if error is not None:
@@ -64,7 +71,7 @@ class robot_requests(robot_base):
         return ''  # Alas, requests_ftp doesn't store welcome message
 
 
-def request_get(url, timeout, proxy):
+def request_get(url, headers, timeout, proxy):
     if proxy:
         proxies = {'http': proxy, 'https': proxy}
     else:
@@ -72,7 +79,8 @@ def request_get(url, timeout, proxy):
 
     try:
         r = requests.Session().get(
-            url, timeout=timeout, allow_redirects=False, proxies=proxies)
+            url, headers=headers, timeout=timeout,
+            allow_redirects=False, proxies=proxies)
     except requests.RequestException as e:
         return str(e), None
     else:
index fa40adaf4381d66749b338cb92f61210f04c9633..4096a6f6ec9a67afccfe7ce2c3f6be77229d3f85 100644 (file)
@@ -1,3 +1,7 @@
+Version 5.2.2 (2024-03-03)
+
+   Robots/bkmk_rrequests: Add request headers.
+
 Version 5.2.1 (2024-03-02)
 
    Speedup second access through proxy.