]> git.phdru.name Git - bookmarks_db.git/commitdiff
Refactor(Rebobt/requests)
authorOleg Broytman <phd@phdru.name>
Fri, 1 Mar 2024 21:02:57 +0000 (00:02 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 1 Mar 2024 21:19:55 +0000 (00:19 +0300)
Robots/bkmk_rrequests.py

index 24b09d6803ef763b24406267599f61bb2df73911..e1f00198dcda56a7f4d54835ce322d29ba51bc69 100644 (file)
@@ -25,27 +25,34 @@ class robot_requests(robot_base):
     proxy = None
 
     def get(self, bookmark, url, accept_charset=False):
-        try:
-            r = requests.Session().get(
-                url, timeout=self.timeout, allow_redirects=False)
-        except requests.RequestException as e:
-            error = str(e)
+        error, r = request_get(url, self.timeout, None)
+        if error is not None:
             self.log('   Error: %s' % error)
             if self.proxy:
-                error = None
                 self.log('   Retrying with the proxy...')
-                try:
-                    r = requests.get(
-                        url, timeout=self.timeout, allow_redirects=False,
-                        proxies={'http': self.proxy, 'https': self.proxy})
-                except requests.RequestException as e:
-                    error = str(e)
-                    self.log('   Proxy error: %s' % error)
-            if error is not None:
-                return error, None, None, None, None
+                error, r = request_get(url, self.timeout, self.proxy)
+        if error is not None:
+            if self.proxy:
+                self.log('   Proxy error: %s' % error)
+            return error, None, None, None, None
         if r.is_redirect:
             return None, r.status_code, r.next.url, None, None
         return None, None, None, r.headers, r.content
 
     def get_ftp_welcome(self):
         return ''  # Alas, requests_ftp doesn't store welcome message
+
+
+def request_get(url, timeout, proxy):
+    if proxy:
+        proxies = {'http': proxy, 'https': proxy}
+    else:
+        proxies = None
+
+    try:
+        r = requests.Session().get(
+            url, timeout=timeout, allow_redirects=False, proxies=proxies)
+    except requests.RequestException as e:
+        return str(e), None
+    else:
+        return None, r