# py-bas-automation
[![Linux Python CI](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_linux.yml/badge.svg)](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_linux.yml)
[![Windows Python CI](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_windows.yml/badge.svg)](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_windows.yml)
[![codecov](https://codecov.io/gh/sergerdn/py-bas-automation/graph/badge.svg?token=YQYHYG9VVM)](https://codecov.io/gh/sergerdn/py-bas-automation)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
**Note: This library is currently in active development, and its API may undergo changes without notice at any time.**
**Note:** This project originally started as a `working proof of concept` and does not aim to offer extensive support or
documentation. It serves as a fundamental demonstration of the concept and should be considered a foundation for further
development or experimentation.
## Description
This library enables you to work with BAS (BrowserAutomationStudio) using headless Chromium browsers and a
customizable Windows GUI program, while controlling it with Python ❤️.
![bas_gui_window_3.png](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_3.png)
## Key Features
- **BrowserAutomationStudio Integration:** Run BAS seamlessly with headless browsers while enjoying the convenience of a
user-friendly and customizable Windows GUI program
through [BrowserAutomationStudio](https://bablosoft.com/shop/BrowserAutomationStudio).
- **Unique Fingerprint Feature:** The application includes a unique feature that assigns a `fingerprint` to each
browser instance using [FingerprintSwitcher](https://fingerprints.bablosoft.com/). Please be aware that this is
**paid** service.
- **Proxy Support:** The application supports proxy providers such as [Brightdata](https://brightdata.com/). Please
note that this is a **paid** service.
- **Executing Browser Automation Studio (BAS) Actions from Python**: Implement BAS actions from Python using the
un-documented API. This includes actions such as retrieving page source, `emulating mouse movements`, etc. (Note: Not
all functions are currently supported).
- **Playwright Control:** The application leverages [Playwright](https://playwright.dev/python/) to efficiently manage
and control BAS.
## Understanding the Workflow
The functioning of BAS (Browser Automation Studio) involves the following steps:
1. **Initial Execution:** Upon initiation, BAS runs [cmd_initial.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_initial.py). This script is responsible for
creating tasks and storing them on the disk for later use.
2. **Data Acquisition and Browser Configuration:** BAS then retrieves the necessary data, configures, and launches
browser instances based on the tasks provided earlier.
3. **Task Execution:** Following the browser setup, BAS executes [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) using the `task_id`
and `remote-debugging-port` number as command-line parameters.
4. **Task Handling:** [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) obtains both the `ws_endpoint` and `remote-debugging-port`
from the command line. It then manages complex tasks using [Playwright](https://playwright.dev/python/). These tasks
can range from opening a webpage to filling out forms or even taking screenshots.
5. **Task Completion:** Once the tasks have been completed, BAS terminates the browser instances and exits.
The result of running [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) is as follows:
```json
{
"tasks_file": "C:\\Users\\Administrator\\AppData\\Local\\PyBASProfilesTasks\\tasks.json"
}
```
This is an example of the created `tasks_file`:
```json
[
{
"task_id": "9683607e-2458-4adb-9b14-7e99123bf34d",
"browser_settings": {
"components": {
"widevine": "enable",
"safe_browsing": "enable",
"components": "enable"
},
"network": {
"enable_qiuc_protocol": true
},
"rendering": {
"maximum_fps": 30
},
"browser_version": "default",
"command_line": [
"--disk-cache-size=104857600",
"--disable-gpu-program-cache",
"--disable-gpu-shader-disk-cache",
"--disable-features=GpuProcessHighPriorityWin,GpuUseDisplayThreadPriority",
"--lang=en"
],
"profile": {
"profile_folder_path": "C:\\Users\\Administrator\\AppData\\Local\\PyBASProfiles\\tmp3az8nj96",
"always_load_fingerprint_from_profile_folder": false,
"always_load_proxy_from_profile_folder": false
},
"proxy": {
"server": "brd.superproxy.io",
"port": "22225",
"type": "http",
"login": "brd-customer-hl___redacted__",
"password": "__redacted__"
},
"fingerprint": {
"safe_canvas": true,
"use_perfect_canvas": true,
"safe_webgl": true,
"safe_audio": true,
"safe_battery": true,
"use_font_pack": true,
"safe_element_size": false,
"emulate_sensor_api": true,
"emulate_device_scale_factor": true
}
}
}
]
```
This file contains task details such as browser settings, network configurations, rendering options, and fingerprint
settings, among other things.
## System Requirements
For the optimal running of this application, the following system requirements are necessary:
- **Operating System:** The application is compatible with Windows 10/11 and Windows Server 2022 (tested on 21H2).
- **Python:** Ensure you have Python 3.11 or higher installed. If not, you can download it from the official
Python [website](https://www.python.org/downloads/).
- **Poetry:** This is a necessary tool for managing Python dependencies. You can find the installation guide on the
official Poetry [documentation](https://python-poetry.org/docs/#installation).
- **Git:** The application requires Git for version control. If it's not already installed on your system, you can
download it from the official Git [website](https://git-scm.com/downloads).
- **Make:** This is an optional tool, it can be downloaded from the
Chocolatey [website](https://community.chocolatey.org/packages/make).
- **FingerprintSwitcher License:** Please note that this is a **paid** feature. You will need a valid license
for [FingerprintSwitcher](https://fingerprints.bablosoft.com/) to access its functionalities.
## Installation Guide
### Installing the Current Development Version
To work with the most recent development version of `pybas-automation`, follow the steps outlined below:
1. **Clone the Repository:** Clone the `py-bas-automation` repository from GitHub.
2. **Navigate to the Directory:** Once cloned, navigate to the `py-bas-automation` directory on your local system.
3. **Install Dependencies:** With Poetry, install all the necessary dependencies.
Here are the corresponding commands for each step:
```bash
git clone git@github.com:sergerdn/py-bas-automation.git
cd py-bas-automation
git lfs pull
poetry install
```
### Installing the Latest Release from pypi.org (Currently not recommended)
If you wish to incorporate `pybas-automation` into your project, execute the following command:
```bash
poetry add pybas-automation
```
Please note that this is not currently recommended as the latest release may have unresolved issues.
## How to Run the Application
- **Download the BAS Program:** Begin by downloading the latest version of the compiled BAS program,
called `PyBasFree.zip` . You can find this file in the project directory
under [PyBasFree.zip](https://github.com/sergerdn/py-bas-automation/blob/develop/bas_release/PyBasFree.zip). After downloading, extract the contents and
execute `PyBasFree.exe`.
- **Set Variables in the BAS GUI:** After running the BAS program, proceed to set the necessary variables within the
BAS graphical user interface (GUI).
![BAS GUI](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_1.png)
- **Set Up Proxy Provider:** If you are using a proxy provider, you will need to configure it within the BAS GUI. This
can be accomplished by navigating to the `Proxy Settings` option in the vertical menu and selecting the appropriate
provider.
![Set up proxy provider](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_1_proxy.png)
- **Start the Program:** Once all variables have been set, click the "OK" button to initiate the program.
![Start Program](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_2.png)
## Advanced Usage
This guide introduces a Python script that integrates the `Browser Automation Studio` (BAS) with `py-bas-automation`.
The
purpose is to handle the creation of browser profiles through FingerprintSwitcher and manage tasks related to these
profiles.
### [Initial script: cmd_initial.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_initial.py)
### Description:
This script facilitates the integration between `BAS (Browser Automation Studio)` and `py-bas-automation`. It manages
the creation of browser profiles using `FingerprintSwitcher` and generates tasks associated with these profiles.
### Overview:
- **Initialization**: Import essential modules and configure logging.
- **Browser Profiles**: Utilize `FingerprintSwitcher`'s fingerprint key to generate or manage browser profiles.
- **Proxy Support**: Configure proxy settings for each browser profile in full-automatic mode by handling proxy
providers. Note: at the moment only [`Brightdata`](https://brightdata.com/) is supported.
- **Tasks Generation**: Generate an associated task for each browser profile and store it.
```python
"""
This script facilitates the integration between BAS (Browser Automation Studio) and `py-bas-automation`.
It handles the creation of browser profiles using FingerprintSwitcher and manages tasks associated with these profiles.
"""
import json
import logging
import os
import click
from pydantic import FilePath
from pybas_automation.browser_profile import BrowserProfileStorage
from pybas_automation.task import BasTask, TaskStorage, TaskStorageModeEnum
logger = logging.getLogger("[cmd_worker]")
def run(fingerprint_key: str, count_profiles: int) -> FilePath:
"""
Initialize and run the script.
:param fingerprint_key: Personal fingerprint key from FingerprintSwitcher.
:param count_profiles: Number of profiles to be created.
:return: Path to the generated tasks file.
"""
# Initialize task storage with read-write access and clear it.
# The default storage location is C:\Users\{username}\AppData\Local\PyBASTasks
task_storage = TaskStorage(mode=TaskStorageModeEnum.READ_WRITE)
task_storage.clear()
# Initialize browser profiles using the given fingerprint key.
# The default profile storage location is C:\Users\{username}\AppData\Local\PyBASProfiles
browser_profile_storage = BrowserProfileStorage(fingerprint_key=fingerprint_key)
needs = count_profiles - browser_profile_storage.count()
# Create any additional profiles if necessary
if needs > 0:
for _ in range(needs):
browser_profile = browser_profile_storage.new()
logger.debug("Created new profile: %s", browser_profile.profile_dir)
# Generate tasks corresponding to each profile
for browser_profile in browser_profile_storage.load_all()[:count_profiles]:
task = BasTask()
task.browser_settings.profile.profile_folder_path = browser_profile.profile_dir
task_storage.save(task=task)
logger.info("Total tasks generated: %d", task_storage.count())
task_storage.save_all()
return task_storage.task_file_path
@click.command()
@click.option(
"--bas_fingerprint_key",
help="Your personal fingerprint key of FingerprintSwitcher.",
required=True,
)
@click.option(
"--count_profiles",
help="Number of profiles.",
default=10,
)
def main(bas_fingerprint_key: str, count_profiles: int) -> None:
"""
Entry point of the script. Sets up logging, validates the fingerprint key,
triggers the primary function, and prints the path to the tasks file.
:param bas_fingerprint_key: Personal fingerprint key from FingerprintSwitcher.
:param count_profiles: Number of profiles to be created.
:return: None.
"""
import multiprocessing
process = multiprocessing.current_process()
# Configure logging settings
logging.basicConfig(
level=logging.DEBUG,
format=f"%(asctime)s {process.pid} %(levelname)s %(name)s %(message)s",
filename=os.path.join(os.path.dirname(__file__), "logs", "cmd_initial.log"),
)
logger.info("Script cmd_initial has started.")
# Ensure the fingerprint key is present
bas_fingerprint_key = bas_fingerprint_key.strip()
if not bas_fingerprint_key:
raise ValueError("bas_fingerprint_key is not provided")
# Invoke the main function to get the path to the tasks file
task_file_path = run(fingerprint_key=bas_fingerprint_key, count_profiles=count_profiles)
# Print the path for potential use in BAS
print(json.dumps({"tasks_file": str(task_file_path)}, indent=4))
logger.info("cmd_initial script execution completed.")
if __name__ == "__main__":
main()
```
### [Worker script: cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py)
### Description:
This script demonstrates how to execute tasks using the `Playwright` Python library in conjunction with the
`pybas_automation` package. The primary goal is to fetch task data, connect to an existing browser instance using
`Playwright`, and perform actions on a webpage.
### Overview:
- **Initialization**: Import necessary libraries and set up our task id and debugging port.
- **Task Storage**: Fetch a specific task from our task storage.
- **Remote Browser Connection**: Use the remote debugging port to get a WebSocket endpoint, which allows us to connect
to an existing browser instance.
- **Executing Browser Automation Studio (BAS) Actions from Python**: Implement BAS actions from Python using the
un-documented API. This includes actions such as retrieving page source, emulating mouse movements, etc. (Note: Not
all functions are currently supported).
```python
import asyncio
from uuid import UUID
from playwright.async_api import async_playwright
from pybas_automation.task import TaskStorage, TaskStorageModeEnum
from pybas_automation.browser_automator import BrowserAutomator
async def main():
# 1. Initialization
# For demonstration purposes, we're using hardcoded values. In a real scenario, these will be fetched dynamically.
task_id = UUID("some_task_id_that_we_getting_from_cmd_line_from_BAS")
remote_debugging_port = 9222
# A unique identifier for the `Worker.exe` process. Retrieved from the command line argument `--unique-process-id`.
unique_process_id = "some_unique_process_id"
# 2. Task Storage
# Create a new task storage instance in READ mode to fetch tasks.
task_storage = TaskStorage(mode=TaskStorageModeEnum.READ)
found_task = task_storage.get(task_id=task_id)
# Note: You can manipulate or inspect the `found_task` as needed.
# 3. Remote Browser Connection
async with BrowserAutomator(
remote_debugging_port=remote_debugging_port, unique_process_id=unique_process_id
) as automator:
# Variant 1: Work with the BrowserAutomator API
await automator.page.goto("https://playwright.dev/python/")
if unique_process_id:
# With Automator, you can call function from the BrowserAutomationStudio API.
print("Unique process ID: %s", unique_process_id)
page_content = await automator.bas_get_page_content()
elem = automator.page.locator("xpath=//a[@class='getStarted_Sjon']")
await automator.bas_move_mouse_to_elem(elem=elem)
await elem.click()
print("Page content from BAS_SAFE api: %s ...", page_content[:100])
# Variant 1: Work with the Playwright API directly.
ws_endpoint = automator.get_ws_endpoint()
async with async_playwright() as pw:
# Connect to an existing browser instance using the fetched WebSocket endpoint.
browser = await pw.chromium.connect_over_cdp(ws_endpoint)
# Access the main page of the connected browser instance.
page = browser.contexts[0].pages[0]
# Perform actions using Playwright, like navigating to a webpage.
await page.goto("https://playwright.dev/python/")
if __name__ == "__main__":
asyncio.run(main())
```
## Planned Improvements:
- [x] Add Proxy support.
- [x] Develop end-to-end tests to thoroughly assess the entire workflow.
- [ ] Include build scripts for converting Python files to executable format.
- [ ] Expand the repository with more illustrative examples.
## Contributing
Your ideas and contributions are highly valued. Please do not hesitate to open
an [issue](https://github.com/sergerdn/py-bas-automation/issues/new) if you have suggestions, questions, or if you would
like to contribute to its enhancement.
## License
[LICENSE](https://github.com/sergerdn/py-bas-automation/blob/develop/LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/sergerdn/py-bas-automation",
"name": "pybas-automation",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "headless Chromium,Python browser automation,browser fingerprint protection,prevent browser fingerprinting",
"author": "sergerdn",
"author_email": "64213648+sergerdn@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/b8/a8/523208afd84e02d4d5f06ae8a0302dc77f80c6b321dba8d15cc5e350e86d/pybas_automation-0.1.16.tar.gz",
"platform": null,
"description": "# py-bas-automation\n\n[![Linux Python CI](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_linux.yml/badge.svg)](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_linux.yml)\n[![Windows Python CI](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_windows.yml/badge.svg)](https://github.com/sergerdn/py-bas-automation/actions/workflows/ci_windows.yml)\n[![codecov](https://codecov.io/gh/sergerdn/py-bas-automation/graph/badge.svg?token=YQYHYG9VVM)](https://codecov.io/gh/sergerdn/py-bas-automation)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**Note: This library is currently in active development, and its API may undergo changes without notice at any time.**\n\n**Note:** This project originally started as a `working proof of concept` and does not aim to offer extensive support or\ndocumentation. It serves as a fundamental demonstration of the concept and should be considered a foundation for further\ndevelopment or experimentation.\n\n## Description\n\nThis library enables you to work with BAS (BrowserAutomationStudio) using headless Chromium browsers and a\ncustomizable Windows GUI program, while controlling it with Python \u2764\ufe0f.\n![bas_gui_window_3.png](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_3.png)\n\n## Key Features\n\n- **BrowserAutomationStudio Integration:** Run BAS seamlessly with headless browsers while enjoying the convenience of a\n user-friendly and customizable Windows GUI program\n through [BrowserAutomationStudio](https://bablosoft.com/shop/BrowserAutomationStudio).\n- **Unique Fingerprint Feature:** The application includes a unique feature that assigns a `fingerprint` to each\n browser instance using [FingerprintSwitcher](https://fingerprints.bablosoft.com/). Please be aware that this is\n **paid** service.\n- **Proxy Support:** The application supports proxy providers such as [Brightdata](https://brightdata.com/). Please\n note that this is a **paid** service.\n- **Executing Browser Automation Studio (BAS) Actions from Python**: Implement BAS actions from Python using the\n un-documented API. This includes actions such as retrieving page source, `emulating mouse movements`, etc. (Note: Not\n all functions are currently supported).\n- **Playwright Control:** The application leverages [Playwright](https://playwright.dev/python/) to efficiently manage\n and control BAS.\n\n## Understanding the Workflow\n\nThe functioning of BAS (Browser Automation Studio) involves the following steps:\n\n1. **Initial Execution:** Upon initiation, BAS runs [cmd_initial.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_initial.py). This script is responsible for\n creating tasks and storing them on the disk for later use.\n2. **Data Acquisition and Browser Configuration:** BAS then retrieves the necessary data, configures, and launches\n browser instances based on the tasks provided earlier.\n3. **Task Execution:** Following the browser setup, BAS executes [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) using the `task_id`\n and `remote-debugging-port` number as command-line parameters.\n4. **Task Handling:** [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) obtains both the `ws_endpoint` and `remote-debugging-port`\n from the command line. It then manages complex tasks using [Playwright](https://playwright.dev/python/). These tasks\n can range from opening a webpage to filling out forms or even taking screenshots.\n5. **Task Completion:** Once the tasks have been completed, BAS terminates the browser instances and exits.\n\nThe result of running [cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py) is as follows:\n\n```json\n{\n \"tasks_file\": \"C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\PyBASProfilesTasks\\\\tasks.json\"\n}\n```\n\nThis is an example of the created `tasks_file`:\n\n```json\n[\n {\n \"task_id\": \"9683607e-2458-4adb-9b14-7e99123bf34d\",\n \"browser_settings\": {\n \"components\": {\n \"widevine\": \"enable\",\n \"safe_browsing\": \"enable\",\n \"components\": \"enable\"\n },\n \"network\": {\n \"enable_qiuc_protocol\": true\n },\n \"rendering\": {\n \"maximum_fps\": 30\n },\n \"browser_version\": \"default\",\n \"command_line\": [\n \"--disk-cache-size=104857600\",\n \"--disable-gpu-program-cache\",\n \"--disable-gpu-shader-disk-cache\",\n \"--disable-features=GpuProcessHighPriorityWin,GpuUseDisplayThreadPriority\",\n \"--lang=en\"\n ],\n \"profile\": {\n \"profile_folder_path\": \"C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\PyBASProfiles\\\\tmp3az8nj96\",\n \"always_load_fingerprint_from_profile_folder\": false,\n \"always_load_proxy_from_profile_folder\": false\n },\n \"proxy\": {\n \"server\": \"brd.superproxy.io\",\n \"port\": \"22225\",\n \"type\": \"http\",\n \"login\": \"brd-customer-hl___redacted__\",\n \"password\": \"__redacted__\"\n },\n \"fingerprint\": {\n \"safe_canvas\": true,\n \"use_perfect_canvas\": true,\n \"safe_webgl\": true,\n \"safe_audio\": true,\n \"safe_battery\": true,\n \"use_font_pack\": true,\n \"safe_element_size\": false,\n \"emulate_sensor_api\": true,\n \"emulate_device_scale_factor\": true\n }\n }\n }\n]\n```\n\nThis file contains task details such as browser settings, network configurations, rendering options, and fingerprint\nsettings, among other things.\n\n## System Requirements\n\nFor the optimal running of this application, the following system requirements are necessary:\n\n- **Operating System:** The application is compatible with Windows 10/11 and Windows Server 2022 (tested on 21H2).\n- **Python:** Ensure you have Python 3.11 or higher installed. If not, you can download it from the official\n Python [website](https://www.python.org/downloads/).\n- **Poetry:** This is a necessary tool for managing Python dependencies. You can find the installation guide on the\n official Poetry [documentation](https://python-poetry.org/docs/#installation).\n- **Git:** The application requires Git for version control. If it's not already installed on your system, you can\n download it from the official Git [website](https://git-scm.com/downloads).\n- **Make:** This is an optional tool, it can be downloaded from the\n Chocolatey [website](https://community.chocolatey.org/packages/make).\n- **FingerprintSwitcher License:** Please note that this is a **paid** feature. You will need a valid license\n for [FingerprintSwitcher](https://fingerprints.bablosoft.com/) to access its functionalities.\n\n## Installation Guide\n\n### Installing the Current Development Version\n\nTo work with the most recent development version of `pybas-automation`, follow the steps outlined below:\n\n1. **Clone the Repository:** Clone the `py-bas-automation` repository from GitHub.\n2. **Navigate to the Directory:** Once cloned, navigate to the `py-bas-automation` directory on your local system.\n3. **Install Dependencies:** With Poetry, install all the necessary dependencies.\n\nHere are the corresponding commands for each step:\n\n```bash\ngit clone git@github.com:sergerdn/py-bas-automation.git\ncd py-bas-automation\ngit lfs pull\npoetry install\n```\n\n### Installing the Latest Release from pypi.org (Currently not recommended)\n\nIf you wish to incorporate `pybas-automation` into your project, execute the following command:\n\n```bash\npoetry add pybas-automation\n```\n\nPlease note that this is not currently recommended as the latest release may have unresolved issues.\n\n## How to Run the Application\n\n- **Download the BAS Program:** Begin by downloading the latest version of the compiled BAS program,\n called `PyBasFree.zip` . You can find this file in the project directory\n under [PyBasFree.zip](https://github.com/sergerdn/py-bas-automation/blob/develop/bas_release/PyBasFree.zip). After downloading, extract the contents and\n execute `PyBasFree.exe`.\n- **Set Variables in the BAS GUI:** After running the BAS program, proceed to set the necessary variables within the\n BAS graphical user interface (GUI).\n ![BAS GUI](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_1.png)\n- **Set Up Proxy Provider:** If you are using a proxy provider, you will need to configure it within the BAS GUI. This\n can be accomplished by navigating to the `Proxy Settings` option in the vertical menu and selecting the appropriate\n provider.\n ![Set up proxy provider](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_1_proxy.png)\n- **Start the Program:** Once all variables have been set, click the \"OK\" button to initiate the program.\n ![Start Program](https://sergerdn.github.io/py-bas-automation/images/bas_gui_window_2.png)\n\n## Advanced Usage\n\nThis guide introduces a Python script that integrates the `Browser Automation Studio` (BAS) with `py-bas-automation`.\nThe\npurpose is to handle the creation of browser profiles through FingerprintSwitcher and manage tasks related to these\nprofiles.\n\n### [Initial script: cmd_initial.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_initial.py)\n\n### Description:\n\nThis script facilitates the integration between `BAS (Browser Automation Studio)` and `py-bas-automation`. It manages\nthe creation of browser profiles using `FingerprintSwitcher` and generates tasks associated with these profiles.\n\n### Overview:\n\n- **Initialization**: Import essential modules and configure logging.\n- **Browser Profiles**: Utilize `FingerprintSwitcher`'s fingerprint key to generate or manage browser profiles.\n- **Proxy Support**: Configure proxy settings for each browser profile in full-automatic mode by handling proxy\n providers. Note: at the moment only [`Brightdata`](https://brightdata.com/) is supported.\n- **Tasks Generation**: Generate an associated task for each browser profile and store it.\n\n```python\n\"\"\"\nThis script facilitates the integration between BAS (Browser Automation Studio) and `py-bas-automation`.\nIt handles the creation of browser profiles using FingerprintSwitcher and manages tasks associated with these profiles.\n\"\"\"\n\nimport json\nimport logging\nimport os\n\nimport click\nfrom pydantic import FilePath\n\nfrom pybas_automation.browser_profile import BrowserProfileStorage\nfrom pybas_automation.task import BasTask, TaskStorage, TaskStorageModeEnum\n\nlogger = logging.getLogger(\"[cmd_worker]\")\n\n\ndef run(fingerprint_key: str, count_profiles: int) -> FilePath:\n \"\"\"\n Initialize and run the script.\n\n :param fingerprint_key: Personal fingerprint key from FingerprintSwitcher.\n :param count_profiles: Number of profiles to be created.\n\n :return: Path to the generated tasks file.\n \"\"\"\n\n # Initialize task storage with read-write access and clear it.\n # The default storage location is C:\\Users\\{username}\\AppData\\Local\\PyBASTasks\n task_storage = TaskStorage(mode=TaskStorageModeEnum.READ_WRITE)\n task_storage.clear()\n\n # Initialize browser profiles using the given fingerprint key.\n # The default profile storage location is C:\\Users\\{username}\\AppData\\Local\\PyBASProfiles\n browser_profile_storage = BrowserProfileStorage(fingerprint_key=fingerprint_key)\n\n needs = count_profiles - browser_profile_storage.count()\n\n # Create any additional profiles if necessary\n if needs > 0:\n for _ in range(needs):\n browser_profile = browser_profile_storage.new()\n logger.debug(\"Created new profile: %s\", browser_profile.profile_dir)\n\n # Generate tasks corresponding to each profile\n for browser_profile in browser_profile_storage.load_all()[:count_profiles]:\n task = BasTask()\n task.browser_settings.profile.profile_folder_path = browser_profile.profile_dir\n task_storage.save(task=task)\n\n logger.info(\"Total tasks generated: %d\", task_storage.count())\n task_storage.save_all()\n\n return task_storage.task_file_path\n\n\n@click.command()\n@click.option(\n \"--bas_fingerprint_key\",\n help=\"Your personal fingerprint key of FingerprintSwitcher.\",\n required=True,\n)\n@click.option(\n \"--count_profiles\",\n help=\"Number of profiles.\",\n default=10,\n)\ndef main(bas_fingerprint_key: str, count_profiles: int) -> None:\n \"\"\"\n Entry point of the script. Sets up logging, validates the fingerprint key,\n triggers the primary function, and prints the path to the tasks file.\n\n :param bas_fingerprint_key: Personal fingerprint key from FingerprintSwitcher.\n :param count_profiles: Number of profiles to be created.\n\n :return: None.\n \"\"\"\n\n import multiprocessing\n\n process = multiprocessing.current_process()\n\n # Configure logging settings\n logging.basicConfig(\n level=logging.DEBUG,\n format=f\"%(asctime)s {process.pid} %(levelname)s %(name)s %(message)s\",\n filename=os.path.join(os.path.dirname(__file__), \"logs\", \"cmd_initial.log\"),\n )\n logger.info(\"Script cmd_initial has started.\")\n\n # Ensure the fingerprint key is present\n bas_fingerprint_key = bas_fingerprint_key.strip()\n if not bas_fingerprint_key:\n raise ValueError(\"bas_fingerprint_key is not provided\")\n\n # Invoke the main function to get the path to the tasks file\n task_file_path = run(fingerprint_key=bas_fingerprint_key, count_profiles=count_profiles)\n\n # Print the path for potential use in BAS\n print(json.dumps({\"tasks_file\": str(task_file_path)}, indent=4))\n\n logger.info(\"cmd_initial script execution completed.\")\n\n\nif __name__ == \"__main__\":\n main()\n```\n\n### [Worker script: cmd_worker.py](https://github.com/sergerdn/py-bas-automation/blob/develop/cmd_worker.py)\n\n### Description:\n\nThis script demonstrates how to execute tasks using the `Playwright` Python library in conjunction with the\n`pybas_automation` package. The primary goal is to fetch task data, connect to an existing browser instance using\n`Playwright`, and perform actions on a webpage.\n\n### Overview:\n\n- **Initialization**: Import necessary libraries and set up our task id and debugging port.\n- **Task Storage**: Fetch a specific task from our task storage.\n- **Remote Browser Connection**: Use the remote debugging port to get a WebSocket endpoint, which allows us to connect\n to an existing browser instance.\n- **Executing Browser Automation Studio (BAS) Actions from Python**: Implement BAS actions from Python using the\n un-documented API. This includes actions such as retrieving page source, emulating mouse movements, etc. (Note: Not\n all functions are currently supported).\n\n```python\nimport asyncio\nfrom uuid import UUID\nfrom playwright.async_api import async_playwright\nfrom pybas_automation.task import TaskStorage, TaskStorageModeEnum\nfrom pybas_automation.browser_automator import BrowserAutomator\n\n\nasync def main():\n # 1. Initialization\n # For demonstration purposes, we're using hardcoded values. In a real scenario, these will be fetched dynamically.\n task_id = UUID(\"some_task_id_that_we_getting_from_cmd_line_from_BAS\")\n remote_debugging_port = 9222\n # A unique identifier for the `Worker.exe` process. Retrieved from the command line argument `--unique-process-id`.\n unique_process_id = \"some_unique_process_id\"\n\n # 2. Task Storage\n # Create a new task storage instance in READ mode to fetch tasks.\n task_storage = TaskStorage(mode=TaskStorageModeEnum.READ)\n found_task = task_storage.get(task_id=task_id)\n # Note: You can manipulate or inspect the `found_task` as needed.\n\n # 3. Remote Browser Connection\n async with BrowserAutomator(\n remote_debugging_port=remote_debugging_port, unique_process_id=unique_process_id\n ) as automator:\n # Variant 1: Work with the BrowserAutomator API\n await automator.page.goto(\"https://playwright.dev/python/\")\n if unique_process_id:\n # With Automator, you can call function from the BrowserAutomationStudio API.\n print(\"Unique process ID: %s\", unique_process_id)\n page_content = await automator.bas_get_page_content()\n\n elem = automator.page.locator(\"xpath=//a[@class='getStarted_Sjon']\")\n await automator.bas_move_mouse_to_elem(elem=elem)\n await elem.click()\n\n print(\"Page content from BAS_SAFE api: %s ...\", page_content[:100])\n\n # Variant 1: Work with the Playwright API directly.\n ws_endpoint = automator.get_ws_endpoint()\n async with async_playwright() as pw:\n # Connect to an existing browser instance using the fetched WebSocket endpoint.\n browser = await pw.chromium.connect_over_cdp(ws_endpoint)\n # Access the main page of the connected browser instance.\n page = browser.contexts[0].pages[0]\n # Perform actions using Playwright, like navigating to a webpage.\n await page.goto(\"https://playwright.dev/python/\")\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n## Planned Improvements:\n\n- [x] Add Proxy support.\n- [x] Develop end-to-end tests to thoroughly assess the entire workflow.\n- [ ] Include build scripts for converting Python files to executable format.\n- [ ] Expand the repository with more illustrative examples.\n\n## Contributing\n\nYour ideas and contributions are highly valued. Please do not hesitate to open\nan [issue](https://github.com/sergerdn/py-bas-automation/issues/new) if you have suggestions, questions, or if you would\nlike to contribute to its enhancement.\n\n## License\n\n[LICENSE](https://github.com/sergerdn/py-bas-automation/blob/develop/LICENSE)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python library for automating BrowserAutomationStudio (BAS) using headless Chromium browsers and Windows GUI program.",
"version": "0.1.16",
"project_urls": {
"Homepage": "https://github.com/sergerdn/py-bas-automation",
"Repository": "https://github.com/sergerdn/py-bas-automation"
},
"split_keywords": [
"headless chromium",
"python browser automation",
"browser fingerprint protection",
"prevent browser fingerprinting"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "39f41ba838091ddddd18bd5d0254d3928be93b00721a56b9f0921da893c95dea",
"md5": "82d3251799f637ba0143db4ecd409ae7",
"sha256": "c5bf17b2ee378982a90d5f53fa6b5f2f6c92bcbc4df7078ab7bedfc4ca52c51c"
},
"downloads": -1,
"filename": "pybas_automation-0.1.16-py3-none-any.whl",
"has_sig": false,
"md5_digest": "82d3251799f637ba0143db4ecd409ae7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 33322,
"upload_time": "2023-10-21T14:50:20",
"upload_time_iso_8601": "2023-10-21T14:50:20.340210Z",
"url": "https://files.pythonhosted.org/packages/39/f4/1ba838091ddddd18bd5d0254d3928be93b00721a56b9f0921da893c95dea/pybas_automation-0.1.16-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b8a8523208afd84e02d4d5f06ae8a0302dc77f80c6b321dba8d15cc5e350e86d",
"md5": "69969714c08b2bfaee0f3cf1f5853d9b",
"sha256": "21954782a0191d90018a781704b2103c5857b26f46ad70d4f9aca7ad44ad5643"
},
"downloads": -1,
"filename": "pybas_automation-0.1.16.tar.gz",
"has_sig": false,
"md5_digest": "69969714c08b2bfaee0f3cf1f5853d9b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 28824,
"upload_time": "2023-10-21T14:50:22",
"upload_time_iso_8601": "2023-10-21T14:50:22.576823Z",
"url": "https://files.pythonhosted.org/packages/b8/a8/523208afd84e02d4d5f06ae8a0302dc77f80c6b321dba8d15cc5e350e86d/pybas_automation-0.1.16.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-21 14:50:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sergerdn",
"github_project": "py-bas-automation",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pybas-automation"
}