]> git.phdru.name Git - bookmarks_db.git/commitdiff
Refactor(Robots): Move `encode_url` to `base.py`
authorOleg Broytman <phd@phdru.name>
Mon, 9 Sep 2024 20:10:56 +0000 (23:10 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 9 Sep 2024 20:26:04 +0000 (23:26 +0300)
Robots/base.py
Robots/bkmk_rcurl.py

index 3aaac77acc9c39735b9d35f5b0c3eab2134141e8..fa7178a5aaf6b11c7aa70da72640c530a507624d 100644 (file)
@@ -12,7 +12,8 @@ __all__ = ['robot_base']
 
 
 from base64 import b64encode
-from urllib.parse import urlsplit, urljoin
+from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode, \
+    urljoin
 import asyncio
 import sys
 import socket
@@ -429,3 +430,44 @@ class robot_base(Robot):
         bookmark.last_tested = str(start)
         now = int(time.time())
         bookmark.test_time = str(now - start)
+
+
+def encode_url(url, encoding='latin1'):
+    split_results = urlsplit(url)
+    protocol, netloc, path, query, tag = split_results
+    user = split_results.username
+    password = split_results.password
+    host = split_results.hostname
+    port = split_results.port
+
+    if query:
+        qlist = []
+        for name, value in parse_qsl(query):
+            if not isinstance(name, bytes):
+                name = name.encode(encoding)
+                value = value.encode(encoding)
+            qlist.append((name, value))
+
+    url = protocol + "://"
+    if user:
+        url += quote(user.encode(encoding))
+        if password:
+            url += ':' + quote(password.encode(encoding))
+        url += '@'
+    if host:
+        if isinstance(host, bytes):
+            host = host.decode(encoding)
+        url += host.encode('idna').decode('ascii')
+        if port:
+            url += ':%d' % port
+    if path:
+        if protocol == "file":
+            url += quote(path)
+        else:
+            url += quote(path.encode(encoding))
+    if query:
+        url += '?' + urlencode(qlist)
+    if tag:
+        url += '#' + quote_plus(tag.encode(encoding))
+
+    return url
index 36c715b71add292b1255410c65281483174920a7..553dc7f0351d79529a3ef847040376d86d127bb1 100644 (file)
@@ -14,14 +14,13 @@ __all__ = ['robot_curl']
 
 from queue import Queue
 from time import sleep
-from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode
 import concurrent.futures
 import threading
 
 import certifi
 import pycurl
 
-from Robots.base import robot_base
+from Robots.base import robot_base, encode_url
 from Robots.concurrent_futures import concurrent_futures
 
 
@@ -172,44 +171,3 @@ class CurlWrapper:
 
     def body_callback(self, data):
         self.body += data
-
-
-def encode_url(url, encoding='latin1'):
-    split_results = urlsplit(url)
-    protocol, netloc, path, query, tag = split_results
-    user = split_results.username
-    password = split_results.password
-    host = split_results.hostname
-    port = split_results.port
-
-    if query:
-        qlist = []
-        for name, value in parse_qsl(query):
-            if not isinstance(name, bytes):
-                name = name.encode(encoding)
-                value = value.encode(encoding)
-            qlist.append((name, value))
-
-    url = protocol + "://"
-    if user:
-        url += quote(user.encode(encoding))
-        if password:
-            url += ':' + quote(password.encode(encoding))
-        url += '@'
-    if host:
-        if isinstance(host, bytes):
-            host = host.decode(encoding)
-        url += host.encode('idna').decode('ascii')
-        if port:
-            url += ':%d' % port
-    if path:
-        if protocol == "file":
-            url += quote(path)
-        else:
-            url += quote(path.encode(encoding))
-    if query:
-        url += '?' + urlencode(qlist)
-    if tag:
-        url += '#' + quote_plus(tag.encode(encoding))
-
-    return url