From: Oleg Broytman Date: Thu, 6 Mar 2025 20:08:47 +0000 (+0300) Subject: Feat(Robots): Robot based on `curl-cffi.request.AsyncSession()` X-Git-Tag: 6.4.0~1 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=47cd982da3fa3f67d1ef74e51752fd4ed05fd042;p=bookmarks_db.git Feat(Robots): Robot based on `curl-cffi.request.AsyncSession()` --- diff --git a/Robots/bkmk_rasynccurlcffi.py b/Robots/bkmk_rasynccurlcffi.py new file mode 100644 index 0000000..7a7287d --- /dev/null +++ b/Robots/bkmk_rasynccurlcffi.py @@ -0,0 +1,54 @@ +"""Robot based on curl-cffi.request.AsyncSession() + +This file is a part of Bookmarks database and Internet robot. + +""" + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2025 PhiloSoft Design" +__license__ = "GNU GPL" + +__all__ = ['robot_asynccurlcffi'] + + +from curl_cffi.requests import AsyncSession +import curl_cffi + +from Robots.aio_base import aio_base +from Robots.util import get_ftp + + +class robot_asynccurlcffi(aio_base): + def version_str(self): + return 'curl-cffi/%s' % curl_cffi.__version__ + + async def get(self, url, req_headers, use_proxy=False): + if url.startswith('ftp://'): + error, welcome, body = get_ftp(url, self.timeout) + if error is not None: + return error, None, None, None + self.welcome = welcome + return None, None, None, body + + if use_proxy: + proxies = {'http': self.proxy, 'https': self.proxy} + else: + proxies = None + + error = r = None + try: + async with AsyncSession() as s: + r = await s.get(url, headers=req_headers, + timeout=self.timeout, + allow_redirects=False, proxies=proxies, + verify=False, impersonate='firefox133') + except curl_cffi.CurlError as e: + error = str(e) + return error, None, None, None + + return None, r.status_code, r.headers, r.content + + def get_ftp_welcome(self): + welcome = self.welcome + self.welcome = '' + return welcome diff --git a/doc/TODO b/doc/TODO index bdc0675..385b0ba 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,5 +1,3 @@ -Async curl-cffi. - Twisted. Selenium or PlayWright.