Skip to content

Commit

Permalink
Working ThreadsafeBrowser.request prototype
Browse files Browse the repository at this point in the history
Related to #957
  • Loading branch information
Yomguithereal committed Apr 15, 2024
1 parent 244511c commit 1431152
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 3 additions & 1 deletion ftest/playwright_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from minet.browser import ThreadsafeBrowser

with ThreadsafeBrowser(headless=False) as browser:
with ThreadsafeBrowser(headless=False, adblock=True) as browser:
response = browser.request("http://lemonde.fr")

print(response)
print(response.stack)
print(response.headers)
32 changes: 30 additions & 2 deletions minet/browser/threadsafe_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,45 @@ async def __request(self, context: BrowserContext, url: str) -> Response:

assert emulated_response is not None

# Collection redirections
responses = []

current = emulated_response

while True:
responses.append(current)

if not current.request.redirected_from:
break

current = await current.request.redirected_from.response()

assert current is not None

redirection_stack = []

for r in reversed(responses):
redirection_stack.append(
Redirection(
r.url,
"hit" if r.status == 200 else "location-header",
status=r.status,
)
)

# Building headers
headers = HTTPHeaderDict()

for header in await emulated_response.headers_array():
headers[header["name"]] = header["value"]

# Building response
response = Response(
url,
[Redirection(url, status=emulated_response.status)],
redirection_stack,
headers,
emulated_response.status,
(await page.content()).encode(),
(await page.content()).encode(), # NOTE: we can do better
known_encoding="utf-8",
)

Expand Down

0 comments on commit 1431152

Please sign in to comment.