Skip to content

Commit aebb246

Browse files
committed
patched
1 parent 320c6a1 commit aebb246

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# rebrowser-playwright
2+
> ⚠️ This is the original [`playwright-python`](https://github.com/microsoft/playwright-python) patched with [`rebrowser-patches`](https://github.com/rebrowser/rebrowser-patches).
3+
>
4+
> 🕵️ The ultimate goal is to pass all automation detection tests presented in [`rebrowser-bot-detector`](https://github.com/rebrowser/rebrowser-bot-detector).
5+
>
6+
> 🪄 It's designed to be a drop-in replacement for the original `playwright` without changing your codebase. Each major and minor version of this repo matches the original repo, patch version could differ due to changes related to the patch itself.
7+
>
8+
> ☝️ Make sure to read: [How to Access Main Context Objects from Isolated Context](https://rebrowser.net/blog/how-to-access-main-context-objects-from-isolated-context-in-puppeteer-and-playwright-23741)
9+
>
10+
> 🐛 Please report any issues in the [`rebrowser-patches`](https://github.com/rebrowser/rebrowser-patches/issues) repo.
11+
112
# 🎭 [Playwright](https://playwright.dev) for Python [![PyPI version](https://badge.fury.io/py/playwright.svg)](https://pypi.python.org/pypi/playwright/) [![Anaconda version](https://img.shields.io/conda/v/microsoft/playwright)](https://anaconda.org/Microsoft/playwright) [![Join Discord](https://img.shields.io/badge/join-discord-infomational)](https://aka.ms/playwright/discord)
213

314
Playwright is a Python library to automate [Chromium](https://www.chromium.org/Home), [Firefox](https://www.mozilla.org/en-US/firefox/new/) and [WebKit](https://webkit.org/) browsers with a single API. Playwright delivers automation that is **ever-green**, **capable**, **reliable** and **fast**. [See how Playwright is better](https://playwright.dev/python).

Diff for: setup.py

+73-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import os
1717
import platform
1818
import shutil
19+
import tempfile
20+
import urllib.request
21+
import tarfile
22+
import re
1923
import subprocess
2024
import sys
2125
import zipfile
@@ -30,8 +34,59 @@
3034
InWheel = None
3135
from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand
3236

33-
driver_version = "1.47.0-beta-1726138322000"
37+
driver_version = "1.47.2"
3438

39+
def prepare_rebrowser_sources():
40+
old_path = "playwright"
41+
new_path = "rebrowser_playwright"
42+
shutil.rmtree(new_path, ignore_errors=True)
43+
shutil.copytree(old_path, new_path)
44+
45+
print(f"[rebrowser-patches] preparing new sources in {new_path}")
46+
for root, dirs, files in os.walk(new_path):
47+
for file in files:
48+
if file.endswith('.py'):
49+
file_path = os.path.join(root, file)
50+
with open(file_path, 'r') as f:
51+
content = f.read()
52+
new_content = content
53+
54+
new_content = re.sub(r'= playwright\._impl', '= rebrowser_playwright._impl', new_content)
55+
new_content = re.sub(r'from playwright.', 'from rebrowser_playwright.', new_content)
56+
new_content = re.sub(r'import playwright.', 'import rebrowser_playwright.', new_content)
57+
new_content = re.sub(r'import playwright', 'import rebrowser_playwright as playwright', new_content)
58+
59+
if new_content != content:
60+
with open(file_path, 'w') as f:
61+
f.write(new_content)
62+
print(f"[rebrowser-patches] updated imports in {file_path}")
63+
64+
def replace_driver_with_rebrowser(path: str):
65+
package_folder = path + "/package"
66+
package_name = "rebrowser-playwright-core"
67+
package_version = "1.47.100"
68+
69+
print(f"[rebrowser-patches] package_version = {package_version}, path = {path}")
70+
71+
# clean old package folder
72+
if os.path.exists(package_folder):
73+
shutil.rmtree(package_folder)
74+
os.makedirs(package_folder)
75+
76+
# Create a temporary directory for downloading
77+
with tempfile.TemporaryDirectory() as temp_dir:
78+
# Construct the NPM package URL
79+
npm_url = f"https://registry.npmjs.org/{package_name}/-/{package_name}-{package_version}.tgz"
80+
81+
# Download the package
82+
tgz_file = os.path.join(temp_dir, f"{package_name}.tgz")
83+
urllib.request.urlretrieve(npm_url, tgz_file)
84+
85+
# Extract the package
86+
with tarfile.open(tgz_file, "r:gz") as tar:
87+
tar.extractall(path=path)
88+
89+
print(f"[rebrowser-patches] {package_name} has been downloaded and extracted to {path}")
3590

3691
def extractall(zip: zipfile.ZipFile, path: str) -> None:
3792
for name in zip.namelist():
@@ -70,12 +125,13 @@ def initialize_options(self) -> None:
70125
self.all = False
71126

72127
def run(self) -> None:
128+
prepare_rebrowser_sources()
73129
shutil.rmtree("build", ignore_errors=True)
74130
shutil.rmtree("dist", ignore_errors=True)
75131
shutil.rmtree("playwright.egg-info", ignore_errors=True)
76132
super().run()
77133
os.makedirs("driver", exist_ok=True)
78-
os.makedirs("playwright/driver", exist_ok=True)
134+
os.makedirs("rebrowser_playwright/driver", exist_ok=True)
79135
base_wheel_bundles: List[Dict[str, str]] = [
80136
{
81137
"wheel": "macosx_10_13_x86_64.whl",
@@ -147,6 +203,7 @@ def _build_wheels(
147203
)
148204
with zipfile.ZipFile(zip_file, "r") as zip:
149205
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
206+
replace_driver_with_rebrowser(f"driver/{wheel_bundle['zip_name']}")
150207
wheel_location = without_platform + wheel_bundle["wheel"]
151208
shutil.copy(base_wheel_location, wheel_location)
152209
with zipfile.ZipFile(wheel_location, "a") as zip:
@@ -155,9 +212,9 @@ def _build_wheels(
155212
for file in files:
156213
from_path = os.path.join(dir_path, file)
157214
to_path = os.path.relpath(from_path, driver_root)
158-
zip.write(from_path, f"playwright/driver/{to_path}")
215+
zip.write(from_path, f"rebrowser_playwright/driver/{to_path}")
159216
zip.writestr(
160-
"playwright/driver/README.md",
217+
"rebrowser_playwright/driver/README.md",
161218
f"{wheel_bundle['wheel']} driver package",
162219
)
163220
os.remove(base_wheel_location)
@@ -194,27 +251,28 @@ def _download_and_extract_local_driver(
194251
download_driver(zip_name)
195252
zip_file = f"driver/playwright-{driver_version}-{zip_name}.zip"
196253
with zipfile.ZipFile(zip_file, "r") as zip:
197-
extractall(zip, "playwright/driver")
254+
extractall(zip, "rebrowser_playwright/driver")
198255

199256

200257
setup(
201-
name="playwright",
258+
name="rebrowser_playwright",
259+
version="1.47.0",
202260
author="Microsoft Corporation",
203261
author_email="",
204262
description="A high-level API to automate web browsers",
205263
long_description=Path("README.md").read_text(encoding="utf-8"),
206264
long_description_content_type="text/markdown",
207265
license="Apache-2.0",
208-
url="https://github.com/Microsoft/playwright-python",
266+
url="https://github.com/rebrowser/rebrowser-playwright-python",
209267
project_urls={
210-
"Release notes": "https://github.com/microsoft/playwright-python/releases",
268+
"Release notes": "https://github.com/rebrowser/rebrowser-playwright-python/releases",
211269
},
212270
packages=[
213-
"playwright",
214-
"playwright.async_api",
215-
"playwright.sync_api",
216-
"playwright._impl",
217-
"playwright._impl.__pyinstaller",
271+
"rebrowser_playwright",
272+
"rebrowser_playwright.async_api",
273+
"rebrowser_playwright.sync_api",
274+
"rebrowser_playwright._impl",
275+
"rebrowser_playwright._impl.__pyinstaller",
218276
],
219277
include_package_data=True,
220278
install_requires=[
@@ -240,8 +298,8 @@ def _download_and_extract_local_driver(
240298
cmdclass={"bdist_wheel": PlaywrightBDistWheelCommand},
241299
entry_points={
242300
"console_scripts": [
243-
"playwright=playwright.__main__:main",
301+
"rebrowser_playwright=rebrowser_playwright.__main__:main",
244302
],
245-
"pyinstaller40": ["hook-dirs=playwright._impl.__pyinstaller:get_hook_dirs"],
303+
"pyinstaller40": ["hook-dirs=rebrowser_playwright._impl.__pyinstaller:get_hook_dirs"],
246304
},
247305
)

0 commit comments

Comments
 (0)