anti-cf 1.0.2__tar.gz → 1.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {anti_cf-1.0.2 → anti_cf-1.1.1}/PKG-INFO +1 -1
- {anti_cf-1.0.2 → anti_cf-1.1.1}/pyproject.toml +1 -1
- {anti_cf-1.0.2 → anti_cf-1.1.1}/src/anti_cf/_persistent_session.py +16 -8
- {anti_cf-1.0.2 → anti_cf-1.1.1}/README.md +0 -0
- {anti_cf-1.0.2 → anti_cf-1.1.1}/src/anti_cf/__init__.py +0 -0
- {anti_cf-1.0.2 → anti_cf-1.1.1}/src/anti_cf/_constants.py +0 -0
- {anti_cf-1.0.2 → anti_cf-1.1.1}/src/anti_cf/_flaresolverr.py +0 -0
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|
2
2
|
|
3
3
|
import pickle
|
4
4
|
import tempfile
|
5
|
+
from pathlib import Path
|
5
6
|
from typing import TYPE_CHECKING, ClassVar
|
6
7
|
|
7
8
|
import fake_useragent
|
@@ -22,8 +23,6 @@ except ImportError:
|
|
22
23
|
_HAS_CACHE = False
|
23
24
|
|
24
25
|
if TYPE_CHECKING:
|
25
|
-
from pathlib import Path
|
26
|
-
|
27
26
|
from requests import Response
|
28
27
|
|
29
28
|
|
@@ -47,10 +46,11 @@ class PersistentSession(Session):
|
|
47
46
|
super().__init__()
|
48
47
|
|
49
48
|
self._load_cookies()
|
50
|
-
ensure_flaresolverr_running()
|
51
49
|
self.set_user_agent()
|
50
|
+
self._flaresolverr_initialized = False
|
52
51
|
|
53
52
|
def _get_user_agent(self) -> str:
|
53
|
+
# Try FlareSolverr first, but don't start it if not running
|
54
54
|
flaresolverr_settings = get_flaresolverr_settings()
|
55
55
|
if flaresolverr_settings is not None:
|
56
56
|
return flaresolverr_settings["userAgent"]
|
@@ -79,7 +79,9 @@ class PersistentSession(Session):
|
|
79
79
|
|
80
80
|
def save_cookies(self) -> None:
|
81
81
|
"""Save current cookies to file."""
|
82
|
-
|
82
|
+
temp_file = Path(tempfile.mktemp(dir=self._COOKIES_FILE.parent))
|
83
|
+
temp_file.write_bytes(pickle.dumps(self.cookies, protocol=4))
|
84
|
+
temp_file.replace(self._COOKIES_FILE)
|
83
85
|
|
84
86
|
def request(self, *args: object, **kwargs: object) -> Response:
|
85
87
|
"""Override request method to save cookies after each request."""
|
@@ -87,6 +89,12 @@ class PersistentSession(Session):
|
|
87
89
|
self.save_cookies()
|
88
90
|
return response
|
89
91
|
|
92
|
+
def _ensure_flaresolverr_initialized(self) -> None:
|
93
|
+
"""Ensure FlareSolverr is ready when needed."""
|
94
|
+
if not self._flaresolverr_initialized:
|
95
|
+
ensure_flaresolverr_running()
|
96
|
+
self._flaresolverr_initialized = True
|
97
|
+
|
90
98
|
def get(self, url: str | bytes, *, try_with_cloudflare: bool = False, _cloudflare_counter: int = 0, **kwargs: object) -> Response | None:
|
91
99
|
if not try_with_cloudflare or "cf_clearance" in self.cookies:
|
92
100
|
try:
|
@@ -107,10 +115,10 @@ class PersistentSession(Session):
|
|
107
115
|
else:
|
108
116
|
logger.warning("Cloudflare detected, but `try_with_cloudflare` wasn't set to True!")
|
109
117
|
|
118
|
+
self._ensure_flaresolverr_initialized()
|
119
|
+
|
110
120
|
try:
|
111
121
|
self._get_url_via_flaresolverr(url)
|
112
|
-
# After the url is retrieved from the flaresolverr proxy, it's not necessarily the one we want
|
113
|
-
# --> So we'll re-request it here:
|
114
122
|
return super().get(url, **kwargs)
|
115
123
|
except Exception:
|
116
124
|
logger.error("FlareSolverr didn't solve it :(")
|
@@ -129,8 +137,8 @@ class PersistentSession(Session):
|
|
129
137
|
dta = response.json()
|
130
138
|
for cookie in dta["solution"]["cookies"]:
|
131
139
|
self.cookies.set(
|
132
|
-
name=cookie["name"],
|
133
|
-
value=cookie["value"],
|
140
|
+
name=cookie["name"],
|
141
|
+
value=cookie["value"],
|
134
142
|
version=cookie.get("version", 0),
|
135
143
|
port=cookie.get("port", None),
|
136
144
|
domain=cookie.get("domain", ""),
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|