arcade-x 0.1.15__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.
- arcade_x-0.1.15/.gitignore +175 -0
- arcade_x-0.1.15/.pre-commit-config.yaml +18 -0
- arcade_x-0.1.15/.ruff.toml +46 -0
- arcade_x-0.1.15/LICENSE +21 -0
- arcade_x-0.1.15/Makefile +55 -0
- arcade_x-0.1.15/PKG-INFO +20 -0
- arcade_x-0.1.15/arcade_x/__init__.py +0 -0
- arcade_x-0.1.15/arcade_x/tools/__init__.py +0 -0
- arcade_x-0.1.15/arcade_x/tools/constants.py +1 -0
- arcade_x-0.1.15/arcade_x/tools/tweets.py +215 -0
- arcade_x-0.1.15/arcade_x/tools/users.py +64 -0
- arcade_x-0.1.15/arcade_x/tools/utils.py +162 -0
- arcade_x-0.1.15/conftest.py +17 -0
- arcade_x-0.1.15/evals/eval_x_tools.py +219 -0
- arcade_x-0.1.15/pyproject.toml +54 -0
- arcade_x-0.1.15/tests/__init__.py +0 -0
- arcade_x-0.1.15/tests/test_tweets.py +298 -0
- arcade_x-0.1.15/tests/test_users.py +78 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
.DS_Store
|
|
2
|
+
credentials.yaml
|
|
3
|
+
docker/credentials.yaml
|
|
4
|
+
|
|
5
|
+
*.lock
|
|
6
|
+
|
|
7
|
+
# example data
|
|
8
|
+
examples/data
|
|
9
|
+
scratch
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
docs/source
|
|
13
|
+
|
|
14
|
+
# From https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore
|
|
15
|
+
|
|
16
|
+
# Byte-compiled / optimized / DLL files
|
|
17
|
+
__pycache__/
|
|
18
|
+
*.py[cod]
|
|
19
|
+
*$py.class
|
|
20
|
+
|
|
21
|
+
# C extensions
|
|
22
|
+
*.so
|
|
23
|
+
|
|
24
|
+
# Distribution / packaging
|
|
25
|
+
.Python
|
|
26
|
+
build/
|
|
27
|
+
develop-eggs/
|
|
28
|
+
dist/
|
|
29
|
+
downloads/
|
|
30
|
+
eggs/
|
|
31
|
+
.eggs/
|
|
32
|
+
lib/
|
|
33
|
+
lib64/
|
|
34
|
+
parts/
|
|
35
|
+
sdist/
|
|
36
|
+
var/
|
|
37
|
+
wheels/
|
|
38
|
+
share/python-wheels/
|
|
39
|
+
*.egg-info/
|
|
40
|
+
.installed.cfg
|
|
41
|
+
*.egg
|
|
42
|
+
MANIFEST
|
|
43
|
+
|
|
44
|
+
# PyInstaller
|
|
45
|
+
# Usually these files are written by a python script from a template
|
|
46
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
47
|
+
*.manifest
|
|
48
|
+
*.spec
|
|
49
|
+
|
|
50
|
+
# Installer logs
|
|
51
|
+
pip-log.txt
|
|
52
|
+
pip-delete-this-directory.txt
|
|
53
|
+
|
|
54
|
+
# Unit test / coverage reports
|
|
55
|
+
htmlcov/
|
|
56
|
+
.tox/
|
|
57
|
+
.nox/
|
|
58
|
+
.coverage
|
|
59
|
+
.coverage.*
|
|
60
|
+
.cache
|
|
61
|
+
nosetests.xml
|
|
62
|
+
coverage.xml
|
|
63
|
+
*.cover
|
|
64
|
+
*.py,cover
|
|
65
|
+
.hypothesis/
|
|
66
|
+
.pytest_cache/
|
|
67
|
+
cover/
|
|
68
|
+
|
|
69
|
+
# Translations
|
|
70
|
+
*.mo
|
|
71
|
+
*.pot
|
|
72
|
+
|
|
73
|
+
# Django stuff:
|
|
74
|
+
*.log
|
|
75
|
+
local_settings.py
|
|
76
|
+
db.sqlite3
|
|
77
|
+
db.sqlite3-journal
|
|
78
|
+
|
|
79
|
+
# Flask stuff:
|
|
80
|
+
instance/
|
|
81
|
+
.webassets-cache
|
|
82
|
+
|
|
83
|
+
# Scrapy stuff:
|
|
84
|
+
.scrapy
|
|
85
|
+
|
|
86
|
+
# Sphinx documentation
|
|
87
|
+
docs/_build/
|
|
88
|
+
|
|
89
|
+
# PyBuilder
|
|
90
|
+
.pybuilder/
|
|
91
|
+
target/
|
|
92
|
+
|
|
93
|
+
# Jupyter Notebook
|
|
94
|
+
.ipynb_checkpoints
|
|
95
|
+
|
|
96
|
+
# IPython
|
|
97
|
+
profile_default/
|
|
98
|
+
ipython_config.py
|
|
99
|
+
|
|
100
|
+
# pyenv
|
|
101
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
102
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
103
|
+
# .python-version
|
|
104
|
+
|
|
105
|
+
# pipenv
|
|
106
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
107
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
108
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
109
|
+
# install all needed dependencies.
|
|
110
|
+
#Pipfile.lock
|
|
111
|
+
|
|
112
|
+
# poetry
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
114
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
115
|
+
# commonly ignored for libraries.
|
|
116
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
117
|
+
poetry.lock
|
|
118
|
+
|
|
119
|
+
# pdm
|
|
120
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
121
|
+
#pdm.lock
|
|
122
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
123
|
+
# in version control.
|
|
124
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
125
|
+
.pdm.toml
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.venv
|
|
140
|
+
env/
|
|
141
|
+
venv/
|
|
142
|
+
ENV/
|
|
143
|
+
env.bak/
|
|
144
|
+
venv.bak/
|
|
145
|
+
|
|
146
|
+
# Spyder project settings
|
|
147
|
+
.spyderproject
|
|
148
|
+
.spyproject
|
|
149
|
+
|
|
150
|
+
# Rope project settings
|
|
151
|
+
.ropeproject
|
|
152
|
+
|
|
153
|
+
# mkdocs documentation
|
|
154
|
+
/site
|
|
155
|
+
|
|
156
|
+
# mypy
|
|
157
|
+
.mypy_cache/
|
|
158
|
+
.dmypy.json
|
|
159
|
+
dmypy.json
|
|
160
|
+
|
|
161
|
+
# Pyre type checker
|
|
162
|
+
.pyre/
|
|
163
|
+
|
|
164
|
+
# pytype static type analyzer
|
|
165
|
+
.pytype/
|
|
166
|
+
|
|
167
|
+
# Cython debug symbols
|
|
168
|
+
cython_debug/
|
|
169
|
+
|
|
170
|
+
# PyCharm
|
|
171
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
172
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
173
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
174
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
175
|
+
#.idea/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
files: ^.*/x/.*
|
|
2
|
+
repos:
|
|
3
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
4
|
+
rev: "v4.4.0"
|
|
5
|
+
hooks:
|
|
6
|
+
- id: check-case-conflict
|
|
7
|
+
- id: check-merge-conflict
|
|
8
|
+
- id: check-toml
|
|
9
|
+
- id: check-yaml
|
|
10
|
+
- id: end-of-file-fixer
|
|
11
|
+
- id: trailing-whitespace
|
|
12
|
+
|
|
13
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
14
|
+
rev: v0.6.7
|
|
15
|
+
hooks:
|
|
16
|
+
- id: ruff
|
|
17
|
+
args: [--fix]
|
|
18
|
+
- id: ruff-format
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
target-version = "py310"
|
|
2
|
+
line-length = 100
|
|
3
|
+
fix = true
|
|
4
|
+
|
|
5
|
+
[lint]
|
|
6
|
+
select = [
|
|
7
|
+
# flake8-2020
|
|
8
|
+
"YTT",
|
|
9
|
+
# flake8-bandit
|
|
10
|
+
"S",
|
|
11
|
+
# flake8-bugbear
|
|
12
|
+
"B",
|
|
13
|
+
# flake8-builtins
|
|
14
|
+
"A",
|
|
15
|
+
# flake8-comprehensions
|
|
16
|
+
"C4",
|
|
17
|
+
# flake8-debugger
|
|
18
|
+
"T10",
|
|
19
|
+
# flake8-simplify
|
|
20
|
+
"SIM",
|
|
21
|
+
# isort
|
|
22
|
+
"I",
|
|
23
|
+
# mccabe
|
|
24
|
+
"C90",
|
|
25
|
+
# pycodestyle
|
|
26
|
+
"E", "W",
|
|
27
|
+
# pyflakes
|
|
28
|
+
"F",
|
|
29
|
+
# pygrep-hooks
|
|
30
|
+
"PGH",
|
|
31
|
+
# pyupgrade
|
|
32
|
+
"UP",
|
|
33
|
+
# ruff
|
|
34
|
+
"RUF",
|
|
35
|
+
# tryceratops
|
|
36
|
+
"TRY",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[lint.per-file-ignores]
|
|
40
|
+
"*" = ["TRY003", "B904"]
|
|
41
|
+
"**/tests/*" = ["S101", "E501"]
|
|
42
|
+
"**/evals/*" = ["S101", "E501"]
|
|
43
|
+
|
|
44
|
+
[format]
|
|
45
|
+
preview = true
|
|
46
|
+
skip-magic-trailing-comma = false
|
arcade_x-0.1.15/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Arcade AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
arcade_x-0.1.15/Makefile
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
.PHONY: help
|
|
2
|
+
|
|
3
|
+
help:
|
|
4
|
+
@echo "🛠️ github Commands:\n"
|
|
5
|
+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
|
6
|
+
|
|
7
|
+
.PHONY: install
|
|
8
|
+
install: ## Install the uv environment and install all packages with dependencies
|
|
9
|
+
@echo "🚀 Creating virtual environment and installing all packages using uv"
|
|
10
|
+
@uv sync --active --all-extras --no-sources
|
|
11
|
+
@if [ -f .pre-commit-config.yaml ]; then uv run --no-sources pre-commit install; fi
|
|
12
|
+
@echo "✅ All packages and dependencies installed via uv"
|
|
13
|
+
|
|
14
|
+
.PHONY: install-local
|
|
15
|
+
install-local: ## Install the uv environment and install all packages with dependencies with local Arcade sources
|
|
16
|
+
@echo "🚀 Creating virtual environment and installing all packages using uv"
|
|
17
|
+
@uv sync --active --all-extras
|
|
18
|
+
@if [ -f .pre-commit-config.yaml ]; then uv run pre-commit install; fi
|
|
19
|
+
@echo "✅ All packages and dependencies installed via uv"
|
|
20
|
+
|
|
21
|
+
.PHONY: build
|
|
22
|
+
build: clean-build ## Build wheel file using poetry
|
|
23
|
+
@echo "🚀 Creating wheel file"
|
|
24
|
+
uv build
|
|
25
|
+
|
|
26
|
+
.PHONY: clean-build
|
|
27
|
+
clean-build: ## clean build artifacts
|
|
28
|
+
@echo "🗑️ Cleaning dist directory"
|
|
29
|
+
rm -rf dist
|
|
30
|
+
|
|
31
|
+
.PHONY: test
|
|
32
|
+
test: ## Test the code with pytest
|
|
33
|
+
@echo "🚀 Testing code: Running pytest"
|
|
34
|
+
@uv run --no-sources pytest -W ignore -v --cov --cov-config=pyproject.toml --cov-report=xml
|
|
35
|
+
|
|
36
|
+
.PHONY: coverage
|
|
37
|
+
coverage: ## Generate coverage report
|
|
38
|
+
@echo "coverage report"
|
|
39
|
+
@uv run --no-sources coverage report
|
|
40
|
+
@echo "Generating coverage report"
|
|
41
|
+
@uv run --no-sources coverage html
|
|
42
|
+
|
|
43
|
+
.PHONY: bump-version
|
|
44
|
+
bump-version: ## Bump the version in the pyproject.toml file by a patch version
|
|
45
|
+
@echo "🚀 Bumping version in pyproject.toml"
|
|
46
|
+
uv version --no-sources --bump patch
|
|
47
|
+
|
|
48
|
+
.PHONY: check
|
|
49
|
+
check: ## Run code quality tools.
|
|
50
|
+
@if [ -f .pre-commit-config.yaml ]; then\
|
|
51
|
+
echo "🚀 Linting code: Running pre-commit";\
|
|
52
|
+
uv run --no-sources pre-commit run -a;\
|
|
53
|
+
fi
|
|
54
|
+
@echo "🚀 Static type checking: Running mypy"
|
|
55
|
+
@uv run --no-sources mypy --config-file=pyproject.toml
|
arcade_x-0.1.15/PKG-INFO
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: arcade_x
|
|
3
|
+
Version: 0.1.15
|
|
4
|
+
Summary: Arcade.dev LLM tools for X (Twitter)
|
|
5
|
+
Author-email: Arcade <dev@arcade.dev>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Requires-Dist: arcade-tdk<3.0.0,>=2.0.0
|
|
9
|
+
Requires-Dist: httpx<1.0.0,>=0.27.2
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: arcade-ai[evals]<3.0.0,>=2.0.0; extra == 'dev'
|
|
12
|
+
Requires-Dist: arcade-serve<3.0.0,>=2.0.0; extra == 'dev'
|
|
13
|
+
Requires-Dist: mypy<1.6.0,>=1.5.1; extra == 'dev'
|
|
14
|
+
Requires-Dist: pre-commit<3.5.0,>=3.4.0; extra == 'dev'
|
|
15
|
+
Requires-Dist: pytest-asyncio<0.25.0,>=0.24.0; extra == 'dev'
|
|
16
|
+
Requires-Dist: pytest-cov<4.1.0,>=4.0.0; extra == 'dev'
|
|
17
|
+
Requires-Dist: pytest-mock<3.12.0,>=3.11.1; extra == 'dev'
|
|
18
|
+
Requires-Dist: pytest<8.4.0,>=8.3.0; extra == 'dev'
|
|
19
|
+
Requires-Dist: ruff<0.8.0,>=0.7.4; extra == 'dev'
|
|
20
|
+
Requires-Dist: tox<4.12.0,>=4.11.1; extra == 'dev'
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TWEETS_URL = "https://api.x.com/2/tweets"
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
from typing import Annotated, Any
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from arcade_tdk import ToolContext, tool
|
|
5
|
+
from arcade_tdk.auth import X
|
|
6
|
+
from arcade_tdk.errors import RetryableToolError
|
|
7
|
+
|
|
8
|
+
from arcade_x.tools.constants import TWEETS_URL
|
|
9
|
+
from arcade_x.tools.utils import (
|
|
10
|
+
expand_attached_media,
|
|
11
|
+
expand_long_tweet,
|
|
12
|
+
expand_urls_in_tweets,
|
|
13
|
+
get_headers_with_token,
|
|
14
|
+
get_tweet_url,
|
|
15
|
+
parse_search_recent_tweets_response,
|
|
16
|
+
remove_none_values,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# Manage Tweets Tools. See developer docs for additional available parameters:
|
|
20
|
+
# https://developer.x.com/en/docs/x-api/tweets/manage-tweets/api-reference
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@tool(
|
|
24
|
+
requires_auth=X(
|
|
25
|
+
scopes=["tweet.read", "tweet.write", "users.read"],
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
async def post_tweet(
|
|
29
|
+
context: ToolContext,
|
|
30
|
+
tweet_text: Annotated[str, "The text content of the tweet you want to post"],
|
|
31
|
+
) -> Annotated[str, "Success string and the URL of the tweet"]:
|
|
32
|
+
"""Post a tweet to X (Twitter)."""
|
|
33
|
+
|
|
34
|
+
headers = get_headers_with_token(context)
|
|
35
|
+
payload = {"text": tweet_text}
|
|
36
|
+
|
|
37
|
+
async with httpx.AsyncClient() as client:
|
|
38
|
+
response = await client.post(TWEETS_URL, headers=headers, json=payload, timeout=10)
|
|
39
|
+
response.raise_for_status()
|
|
40
|
+
|
|
41
|
+
tweet_id = response.json()["data"]["id"]
|
|
42
|
+
return f"Tweet with id {tweet_id} posted successfully. URL: {get_tweet_url(tweet_id)}"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@tool(requires_auth=X(scopes=["tweet.read", "tweet.write", "users.read"]))
|
|
46
|
+
async def delete_tweet_by_id(
|
|
47
|
+
context: ToolContext,
|
|
48
|
+
tweet_id: Annotated[str, "The ID of the tweet you want to delete"],
|
|
49
|
+
) -> Annotated[str, "Success string confirming the tweet deletion"]:
|
|
50
|
+
"""Delete a tweet on X (Twitter)."""
|
|
51
|
+
|
|
52
|
+
headers = get_headers_with_token(context)
|
|
53
|
+
url = f"{TWEETS_URL}/{tweet_id}"
|
|
54
|
+
|
|
55
|
+
async with httpx.AsyncClient() as client:
|
|
56
|
+
response = await client.delete(url, headers=headers, timeout=10)
|
|
57
|
+
response.raise_for_status()
|
|
58
|
+
|
|
59
|
+
return f"Tweet with id {tweet_id} deleted successfully."
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@tool(requires_auth=X(scopes=["tweet.read", "users.read"]))
|
|
63
|
+
async def search_recent_tweets_by_username(
|
|
64
|
+
context: ToolContext,
|
|
65
|
+
username: Annotated[str, "The username of the X (Twitter) user to look up"],
|
|
66
|
+
max_results: Annotated[
|
|
67
|
+
int, "The maximum number of results to return. Must be in range [1, 100] inclusive"
|
|
68
|
+
] = 10,
|
|
69
|
+
next_token: Annotated[
|
|
70
|
+
str | None, "The pagination token starting from which to return results"
|
|
71
|
+
] = None,
|
|
72
|
+
) -> Annotated[dict[str, Any], "Dictionary containing the search results"]:
|
|
73
|
+
"""Search for recent tweets (last 7 days) on X (Twitter) by username.
|
|
74
|
+
Includes replies and reposts."""
|
|
75
|
+
|
|
76
|
+
headers = get_headers_with_token(context)
|
|
77
|
+
params: dict[str, Any] = {
|
|
78
|
+
"query": f"from:{username}",
|
|
79
|
+
"max_results": min(
|
|
80
|
+
max(max_results, 10), 100
|
|
81
|
+
), # X API does not allow 'max_results' less than 10 or greater than 100
|
|
82
|
+
"next_token": next_token,
|
|
83
|
+
"expansions": "author_id",
|
|
84
|
+
"user.fields": "id,name,username,entities",
|
|
85
|
+
"tweet.fields": "entities,note_tweet",
|
|
86
|
+
}
|
|
87
|
+
params = expand_attached_media(remove_none_values(params))
|
|
88
|
+
|
|
89
|
+
url = f"{TWEETS_URL}/search/recent"
|
|
90
|
+
|
|
91
|
+
async with httpx.AsyncClient() as client:
|
|
92
|
+
response = await client.get(url, headers=headers, params=params, timeout=10)
|
|
93
|
+
response.raise_for_status()
|
|
94
|
+
|
|
95
|
+
response_data: dict[str, Any] = response.json()
|
|
96
|
+
|
|
97
|
+
for tweet in response_data.get("data", []):
|
|
98
|
+
expand_long_tweet(tweet)
|
|
99
|
+
|
|
100
|
+
# Expand the URLs that are in the tweets
|
|
101
|
+
response_data["data"] = expand_urls_in_tweets(
|
|
102
|
+
response_data.get("data", []), delete_entities=True
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Parse the response data
|
|
106
|
+
response_data = parse_search_recent_tweets_response(response_data)
|
|
107
|
+
|
|
108
|
+
return response_data
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@tool(requires_auth=X(scopes=["tweet.read", "users.read"]))
|
|
112
|
+
async def search_recent_tweets_by_keywords(
|
|
113
|
+
context: ToolContext,
|
|
114
|
+
keywords: Annotated[
|
|
115
|
+
list[str] | None, "List of keywords that must be present in the tweet"
|
|
116
|
+
] = None,
|
|
117
|
+
phrases: Annotated[
|
|
118
|
+
list[str] | None, "List of phrases that must be present in the tweet"
|
|
119
|
+
] = None,
|
|
120
|
+
max_results: Annotated[
|
|
121
|
+
int, "The maximum number of results to return. Must be in range [1, 100] inclusive"
|
|
122
|
+
] = 10,
|
|
123
|
+
next_token: Annotated[
|
|
124
|
+
str | None, "The pagination token starting from which to return results"
|
|
125
|
+
] = None,
|
|
126
|
+
) -> Annotated[dict[str, Any], "Dictionary containing the search results"]:
|
|
127
|
+
"""
|
|
128
|
+
Search for recent tweets (last 7 days) on X (Twitter) by required keywords and phrases.
|
|
129
|
+
Includes replies and reposts.
|
|
130
|
+
One of the following input parameters MUST be provided: keywords, phrases
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
if not any([keywords, phrases]):
|
|
134
|
+
raise RetryableToolError(
|
|
135
|
+
"No keywords or phrases provided",
|
|
136
|
+
developer_message="Predicted inputs didn't contain any keywords or phrases",
|
|
137
|
+
additional_prompt_content="Please provide at least one keyword or phrase for search",
|
|
138
|
+
retry_after_ms=500, # Play nice with X API rate limits
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
headers = get_headers_with_token(context)
|
|
142
|
+
|
|
143
|
+
query = "".join([f'"{phrase}" ' for phrase in (phrases or [])])
|
|
144
|
+
if keywords:
|
|
145
|
+
query += " ".join(keywords or [])
|
|
146
|
+
|
|
147
|
+
params: dict[str, Any] = {
|
|
148
|
+
"query": query.strip(),
|
|
149
|
+
"max_results": min(
|
|
150
|
+
max(max_results, 10), 100
|
|
151
|
+
), # X API does not allow 'max_results' less than 10 or greater than 100
|
|
152
|
+
"next_token": next_token,
|
|
153
|
+
"expansions": "author_id",
|
|
154
|
+
"user.fields": "id,name,username,entities",
|
|
155
|
+
"tweet.fields": "entities,note_tweet",
|
|
156
|
+
}
|
|
157
|
+
params = expand_attached_media(remove_none_values(params))
|
|
158
|
+
|
|
159
|
+
url = f"{TWEETS_URL}/search/recent"
|
|
160
|
+
|
|
161
|
+
async with httpx.AsyncClient() as client:
|
|
162
|
+
response = await client.get(url, headers=headers, params=params, timeout=10)
|
|
163
|
+
response.raise_for_status()
|
|
164
|
+
|
|
165
|
+
response_data: dict[str, Any] = response.json()
|
|
166
|
+
|
|
167
|
+
for tweet in response_data.get("data", []):
|
|
168
|
+
expand_long_tweet(tweet)
|
|
169
|
+
|
|
170
|
+
# Expand the URLs that are in the tweets
|
|
171
|
+
response_data["data"] = expand_urls_in_tweets(
|
|
172
|
+
response_data.get("data", []), delete_entities=True
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
# Parse the response data
|
|
176
|
+
response_data = parse_search_recent_tweets_response(response_data)
|
|
177
|
+
|
|
178
|
+
return response_data
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@tool(requires_auth=X(scopes=["tweet.read", "users.read"]))
|
|
182
|
+
async def lookup_tweet_by_id(
|
|
183
|
+
context: ToolContext,
|
|
184
|
+
tweet_id: Annotated[str, "The ID of the tweet you want to look up"],
|
|
185
|
+
) -> Annotated[dict[str, Any], "Dictionary containing the tweet data"]:
|
|
186
|
+
"""Look up a tweet on X (Twitter) by tweet ID."""
|
|
187
|
+
|
|
188
|
+
headers = get_headers_with_token(context)
|
|
189
|
+
params = {
|
|
190
|
+
"expansions": "author_id",
|
|
191
|
+
"user.fields": "id,name,username,entities",
|
|
192
|
+
"tweet.fields": "entities,note_tweet",
|
|
193
|
+
}
|
|
194
|
+
params = expand_attached_media(params)
|
|
195
|
+
|
|
196
|
+
url = f"{TWEETS_URL}/{tweet_id}"
|
|
197
|
+
|
|
198
|
+
async with httpx.AsyncClient() as client:
|
|
199
|
+
response = await client.get(url, headers=headers, params=params, timeout=10)
|
|
200
|
+
response.raise_for_status()
|
|
201
|
+
|
|
202
|
+
response_data: dict[str, Any] = response.json()
|
|
203
|
+
|
|
204
|
+
# Get the tweet data
|
|
205
|
+
tweet_data = response_data.get("data")
|
|
206
|
+
if tweet_data:
|
|
207
|
+
expand_long_tweet(tweet_data)
|
|
208
|
+
|
|
209
|
+
# Expand the URLs that are in the tweet
|
|
210
|
+
expanded_tweet_list = expand_urls_in_tweets([tweet_data], delete_entities=True)
|
|
211
|
+
response_data["data"] = expanded_tweet_list[0]
|
|
212
|
+
else:
|
|
213
|
+
response_data["data"] = {}
|
|
214
|
+
|
|
215
|
+
return response_data
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
from typing import Annotated
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from arcade_tdk import ToolContext, tool
|
|
5
|
+
from arcade_tdk.auth import X
|
|
6
|
+
from arcade_tdk.errors import RetryableToolError
|
|
7
|
+
|
|
8
|
+
from arcade_x.tools.utils import (
|
|
9
|
+
expand_urls_in_user_description,
|
|
10
|
+
expand_urls_in_user_url,
|
|
11
|
+
get_headers_with_token,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
# Users Lookup Tools. See developer docs for additional available query parameters:
|
|
15
|
+
# https://developer.x.com/en/docs/x-api/users/lookup/api-reference
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@tool(requires_auth=X(scopes=["users.read", "tweet.read"]))
|
|
19
|
+
async def lookup_single_user_by_username(
|
|
20
|
+
context: ToolContext,
|
|
21
|
+
username: Annotated[str, "The username of the X (Twitter) user to look up"],
|
|
22
|
+
) -> Annotated[dict, "User information including id, name, username, and description"]:
|
|
23
|
+
"""Look up a user on X (Twitter) by their username."""
|
|
24
|
+
|
|
25
|
+
headers = get_headers_with_token(context)
|
|
26
|
+
|
|
27
|
+
user_fields = ",".join([
|
|
28
|
+
"created_at",
|
|
29
|
+
"description",
|
|
30
|
+
"id",
|
|
31
|
+
"location",
|
|
32
|
+
"most_recent_tweet_id",
|
|
33
|
+
"name",
|
|
34
|
+
"pinned_tweet_id",
|
|
35
|
+
"profile_image_url",
|
|
36
|
+
"protected",
|
|
37
|
+
"public_metrics",
|
|
38
|
+
"url",
|
|
39
|
+
"username",
|
|
40
|
+
"verified",
|
|
41
|
+
"verified_type",
|
|
42
|
+
"withheld",
|
|
43
|
+
"entities",
|
|
44
|
+
])
|
|
45
|
+
url = f"https://api.x.com/2/users/by/username/{username}?user.fields={user_fields}"
|
|
46
|
+
|
|
47
|
+
async with httpx.AsyncClient() as client:
|
|
48
|
+
response = await client.get(url, headers=headers, timeout=10)
|
|
49
|
+
if response.status_code == 404:
|
|
50
|
+
# User not found
|
|
51
|
+
raise RetryableToolError(
|
|
52
|
+
"User not found",
|
|
53
|
+
developer_message=f"User with username '{username}' not found.",
|
|
54
|
+
additional_prompt_content="Please check the username and try again.",
|
|
55
|
+
retry_after_ms=500, # Play nice with X API rate limits
|
|
56
|
+
)
|
|
57
|
+
response.raise_for_status()
|
|
58
|
+
# Parse the response JSON
|
|
59
|
+
user_data = response.json()["data"]
|
|
60
|
+
|
|
61
|
+
user_data = expand_urls_in_user_description(user_data, delete_entities=False)
|
|
62
|
+
user_data = expand_urls_in_user_url(user_data, delete_entities=True)
|
|
63
|
+
|
|
64
|
+
return {"data": user_data}
|