]> git.phdru.name Git - bookmarks_db.git/commitdiff
Refactor(Robots): Move `get_ftp()` into `util.py`
authorOleg Broytman <phd@phdru.name>
Fri, 21 Feb 2025 14:37:37 +0000 (17:37 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 21 Feb 2025 14:37:37 +0000 (17:37 +0300)
Robots/bkmk_rrequests.py
Robots/util.py

index e033d15dd21ad9ab3ce0859a894ae94c5c8bd7f8..e75410e838347fc4ac3182e95bb3aaf72acaa85f 100644 (file)
@@ -11,9 +11,6 @@ __license__ = "GNU GPL"
 __all__ = ['robot_requests']
 
 
-from urllib.parse import urlsplit
-import ftplib
-import socket
 import warnings
 
 from requests.adapters import HTTPAdapter
@@ -22,6 +19,7 @@ import requests
 import urllib3
 
 from Robots.base import robot_base
+from Robots.util import get_ftp
 
 
 class robot_requests(robot_base):
@@ -30,7 +28,7 @@ class robot_requests(robot_base):
 
     async def get(self, url, req_headers, use_proxy=False):
         if url.startswith('ftp://'):
-            error, welcome, body = _get_ftp(url, self.timeout)
+            error, welcome, body = get_ftp(url, self.timeout)
             if error is not None:
                 return error, None, None, None
             self.welcome = welcome
@@ -61,36 +59,6 @@ class robot_requests(robot_base):
         return welcome
 
 
-def _get_ftp(url, timeout):
-    split_results = urlsplit(url)
-    path = split_results.path or '/'
-    user = split_results.username or 'anonymous'
-    password = split_results.password or 'ftp'
-    host = split_results.hostname
-    port = split_results.port or 21
-
-    ftp = ftplib.FTP()
-    try:
-        ftp.connect(host, port, timeout)
-        ftp.login(user, password)
-        if path != '/':
-            ftp.cwd(path)
-    except (socket.error, ftplib.Error) as e:
-        return "Error: %s" % e, None, None
-
-    files = []
-    try:
-        files = ftp.nlst()
-    except ftplib.error_perm as e:
-        if str(e).startswith("550 No files found"):
-            return None, ftp.welcome, ''
-        return "Error: %s" % e, None, None
-    except ftplib.Error as e:
-        return "Error: %s" % e, None, None
-
-    return None, ftp.welcome, '\n'.join(files)
-
-
 # See https://lukasa.co.uk/2017/02/Configuring_TLS_With_Requests/
 
 class AllCiphersAdapter(HTTPAdapter):
index b978614768d67878621dc4aeefebfd794e5316cb..c7818ffa65977cecba8596321f1669d6dd1c7f04 100644 (file)
@@ -8,10 +8,12 @@ __author__ = "Oleg Broytman <phd@phdru.name>"
 __copyright__ = "Copyright (C) 2025 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-__all__ = ['encode_url']
+__all__ = ['encode_url', 'get_ftp']
 
 
 from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode
+import ftplib
+import socket
 
 
 def encode_url(url, encoding='latin1'):
@@ -53,3 +55,33 @@ def encode_url(url, encoding='latin1'):
         url += '#' + quote_plus(tag.encode(encoding))
 
     return url
+
+
+def get_ftp(url, timeout):
+    split_results = urlsplit(url)
+    path = split_results.path or '/'
+    user = split_results.username or 'anonymous'
+    password = split_results.password or 'ftp'
+    host = split_results.hostname
+    port = split_results.port or 21
+
+    ftp = ftplib.FTP()
+    try:
+        ftp.connect(host, port, timeout)
+        ftp.login(user, password)
+        if path != '/':
+            ftp.cwd(path)
+    except (socket.error, ftplib.Error) as e:
+        return "Error: %s" % e, None, None
+
+    files = []
+    try:
+        files = ftp.nlst()
+    except ftplib.error_perm as e:
+        if str(e).startswith("550 No files found"):
+            return None, ftp.welcome, ''
+        return "Error: %s" % e, None, None
+    except ftplib.Error as e:
+        return "Error: %s" % e, None, None
+
+    return None, ftp.welcome, '\n'.join(files)