Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Golang binary server & Python client. #19

Merged
merged 27 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1426dcc
feat: golang unix socket server
Neeraj319 Dec 23, 2024
b07e662
feat: add locatr request acceptance
Neeraj319 Dec 23, 2024
68b8f47
fix: selenium example
Neeraj319 Dec 24, 2024
0bda061
feat: working cdp support
Neeraj319 Dec 24, 2024
6a17b92
feat: add makefile
Neeraj319 Dec 24, 2024
3c0287e
feat: add logs
Neeraj319 Dec 24, 2024
a4f58b8
feat: update make file
Neeraj319 Dec 24, 2024
5821f1a
feat: update file names
Neeraj319 Dec 24, 2024
37cb952
feat: add exception for socket connect
Neeraj319 Dec 26, 2024
85565f3
feat: move process and socket initlization to get_locatrt
Neeraj319 Dec 26, 2024
f3f089d
feat: remove manifest.ini
Neeraj319 Dec 26, 2024
c2f1644
fix: bug with process intilization and url
Neeraj319 Dec 26, 2024
7af9494
fix: add locatr folder to python_client
Neeraj319 Dec 26, 2024
441e179
fix: bug in utils.py
Neeraj319 Dec 26, 2024
d4ba92f
fix: python example
Neeraj319 Dec 26, 2024
f64d981
fix: env var in python example
Neeraj319 Dec 26, 2024
d8d4816
feat: error to stderr
Neeraj319 Dec 26, 2024
b41890b
fix: reanme async method
Neeraj319 Dec 26, 2024
e498424
Merge pull request #20 from vertexcover-io/feat/python-client
Neeraj319 Dec 26, 2024
12f99d9
fix: improve errors
Neeraj319 Dec 26, 2024
ffb92bd
fix: add more errors
Neeraj319 Dec 26, 2024
79e2ceb
feat: improve errors
Neeraj319 Dec 26, 2024
4aba9b2
fix: typos
Neeraj319 Dec 26, 2024
ca84bee
fix: update readme
Neeraj319 Dec 26, 2024
ec7fb7d
fix: add version to locatr socket
Neeraj319 Dec 26, 2024
27571e6
feat: update makeifle
Neeraj319 Dec 26, 2024
1dc825a
feat: client and server versoning
Neeraj319 Dec 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# Go workspace file
go.work
coverage
venv/
*.bin

# Dependency directories (may be project-specific)
vendor/
Expand All @@ -40,3 +42,176 @@ node_modules/

# Environment files (optional, if used)
.env


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# PyPI configuration file
.pypirc
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ repos:
- id: go-fmt
- id: go-imports
- id: golangci-lint

- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args:
- --profile=black

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SOCKET_FILE=/tmp/locatr.sock
BINARY_PATH=server/locatr.bin
PYTHON_CLIENT_BIN=python_client/locatr/bin/

.PHONY: all build run clean

all: build

build:
cd server && go build -o locatr.bin . || { echo "Go build failed"; exit 1; }
mv $(BINARY_PATH) $(PYTHON_CLIENT_BIN) || { echo "Failed to move the binary"; exit 1; }
echo "Build and move successful."

run:
cd server && \
trap 'rm -rf $(SOCKET_FILE); exit' INT TERM EXIT && \
go run . -socketFilePath=$(SOCKET_FILE)


45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ starButtonLocator.click()

### Install Locatr with

#### Golang

```
go get github.com/vertexcover-io/locatr
```

#### Python
```
pip install locatr
```

## Table of Contents

- [ Quick Example ](#quick-example)
Expand All @@ -38,6 +45,44 @@ go get github.com/vertexcover-io/locatr

### Quick Example

#### With python


#### Python example

```
# example assumes that there is already a page opened in the selenium session.
import os

from locatr import (
LlmProvider,
LlmSettings,
Locatr,
LocatrCdpSettings,
LocatrSeleniumSettings,
PluginType,
)

llm_settings = LlmSettings(
llm_provider=LlmProvider.OPENAI,
llm_api_key=os.environ.get("LLM_API_KEY"),
model_name=os.environ.get("LLM_MODEL_NAME"),
reranker_api_key=os.environ.get("RERANKER_API_KEY"),
)

locatr_settings_selenium = LocatrSeleniumSettings(
plugin_type=PluginType.SELENIUM,
llm_settings=llm_settings,
selenium_url=os.environ.get("SELENIUM_URL"),
selenium_session_id="e4c543363b9000a66073db7a39152719",
)

l = Locatr(locatr_settings_selenium, debug=True)

print(lib.get_locatr("H1 element with text Example Domain"))

```

Here's a quick example on how to use the project:

```go
Expand Down
24 changes: 13 additions & 11 deletions cdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ type cdpLocatr struct {
}

type CdpConnectionOptions struct {
Host string
Port int `validate:"required"`
PageId string `validate:"required"`
HostName string
Port int `binding:"required"`
}

func CreateCdpConnection(options CdpConnectionOptions) (*rpcc.Conn, error) {
if len(options.Host) == 0 {
options.Host = "localhost"
ctx := context.Background()
if len(options.HostName) == 0 {
options.HostName = "localhost"
}
validator := validator.NewValidator()
if err := validator.Validate(options); err != nil {
optionValidator := validator.NewValidator()
if err := optionValidator.Validate(options); err != nil {
return nil, err
}
wsUrl, err := getWebsocketDebugUrl(fmt.Sprintf("http://%s:%d", options.HostName, options.Port), 0, ctx)
if err != nil {
return nil, err
}
ctx := context.Background()
wsUrl := fmt.Sprintf("ws://%s:%d/devtools/page/%s", options.Host, options.Port, (options.PageId))
conn, err := rpcc.DialContext(ctx, wsUrl, rpcc.WithWriteBufferSize(1048576))
if err != nil {
return nil, fmt.Errorf("Could not connect to cdp server: %s, err: %w", wsUrl, err)
Expand Down Expand Up @@ -67,7 +69,7 @@ func (cdpPlugin *cdpPlugin) evaluateJsFunction(function string) (string, error)
resultString := string(result.Result.Value)
str, err := strconv.Unquote(resultString)
if err != nil {
return resultString, err
return resultString, nil
}
return str, err

Expand All @@ -84,7 +86,7 @@ func (cdpPlugin *cdpPlugin) evaluateJsScript(scriptContent string) error {
return nil
}

func (cdpLocatr *cdpLocatr) GetLocatr(userReq string) (string, error) {
func (cdpLocatr *cdpLocatr) GetLocatrStr(userReq string) (string, error) {
locatrStr, err := cdpLocatr.locatr.getLocatorStr(userReq)
if err != nil {
return "", fmt.Errorf("error getting locator string: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions examples/cdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func main() {

// connect to the remote cdp server. CDP server can be started by passing `--remote-debugging-port` arg while starting the browser.
connectionOpts := locatr.CdpConnectionOptions{
Port: 9222,
PageId: "177AE4272FC8BBE48190C697A27942DA", // page id can be found by hitting route: http://localhost:9222/json.
Port: 9222,
}
connection, err := locatr.CreateCdpConnection(connectionOpts)
if err != nil {
Expand All @@ -35,7 +34,7 @@ func main() {
return
}

newsItem, err := cdpLocatr.GetLocatr("First news link")
newsItem, err := cdpLocatr.GetLocatrStr("First news link")
fmt.Println(newsItem)
if err != nil {
log.Fatalf("could not get locator: %v", err)
Expand Down
33 changes: 33 additions & 0 deletions examples/python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os

from locatr import (
LlmProvider,
LlmSettings,
Locatr,
LocatrCdpSettings,
LocatrSeleniumSettings,
PluginType,
)

llm_settings = LlmSettings(
llm_provider=LlmProvider.OPENAI,
llm_api_key=os.environ.get("LLM_API_KEY"),
model_name=os.environ.get("LLM_MODEL_NAME"),
reranker_api_key=os.environ.get("RERANKER_API_KEY"),
)
locatr_settings_selenium = LocatrSeleniumSettings(
plugin_type=PluginType.SELENIUM,
llm_settings=llm_settings,
selenium_url=os.environ.get("SELENIUM_URL"),
selenium_session_id="e4c543363b9000a66073db7a39152719",
)

locatr_settins_cdp = LocatrCdpSettings(
llm_settings=llm_settings,
cdp_url="http://localhost:9222",
)

lib = Locatr(locatr_settings_selenium, debug=True)

print("finished sleeping")
print(lib.get_locatr("H1 element with text Example Domain"))
Loading
Loading