arcade-x 1.1.1__py3-none-any.whl → 1.2.0__py3-none-any.whl
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/tools/user_context.py +76 -0
- {arcade_x-1.1.1.dist-info → arcade_x-1.2.0.dist-info}/METADATA +1 -1
- {arcade_x-1.1.1.dist-info → arcade_x-1.2.0.dist-info}/RECORD +5 -4
- {arcade_x-1.1.1.dist-info → arcade_x-1.2.0.dist-info}/WHEEL +1 -1
- {arcade_x-1.1.1.dist-info → arcade_x-1.2.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""User context tools for X (Twitter) toolkit."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
from arcade_tdk import ToolContext, tool
|
|
7
|
+
from arcade_tdk.auth import X
|
|
8
|
+
from arcade_tdk.errors import ToolExecutionError
|
|
9
|
+
|
|
10
|
+
from arcade_x.tools.utils import (
|
|
11
|
+
expand_urls_in_user_description,
|
|
12
|
+
expand_urls_in_user_url,
|
|
13
|
+
get_headers_with_token,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@tool(requires_auth=X(scopes=["users.read", "tweet.read"]))
|
|
18
|
+
async def who_am_i(
|
|
19
|
+
context: ToolContext,
|
|
20
|
+
) -> Annotated[dict, "Authenticated user's profile information"]:
|
|
21
|
+
"""
|
|
22
|
+
Get information about the authenticated X (Twitter) user.
|
|
23
|
+
|
|
24
|
+
Returns the current user's profile including their username, name, description,
|
|
25
|
+
follower counts, and other account information.
|
|
26
|
+
"""
|
|
27
|
+
headers = get_headers_with_token(context)
|
|
28
|
+
|
|
29
|
+
# User fields to retrieve
|
|
30
|
+
user_fields = ",".join([
|
|
31
|
+
"created_at",
|
|
32
|
+
"description",
|
|
33
|
+
"id",
|
|
34
|
+
"location",
|
|
35
|
+
"most_recent_tweet_id",
|
|
36
|
+
"name",
|
|
37
|
+
"pinned_tweet_id",
|
|
38
|
+
"profile_image_url",
|
|
39
|
+
"protected",
|
|
40
|
+
"public_metrics",
|
|
41
|
+
"url",
|
|
42
|
+
"username",
|
|
43
|
+
"verified",
|
|
44
|
+
"verified_type",
|
|
45
|
+
"withheld",
|
|
46
|
+
"entities",
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
# Use the /2/users/me endpoint to get authenticated user's information
|
|
50
|
+
# API Reference: https://developer.x.com/en/docs/x-api/users/lookup/api-reference/get-users-me
|
|
51
|
+
# Returns user object with fields: id, name, username, and optional expansions
|
|
52
|
+
url = f"https://api.x.com/2/users/me?user.fields={user_fields}"
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
async with httpx.AsyncClient() as client:
|
|
56
|
+
response = await client.get(url, headers=headers, timeout=10)
|
|
57
|
+
response.raise_for_status()
|
|
58
|
+
except httpx.HTTPStatusError as e:
|
|
59
|
+
raise ToolExecutionError(
|
|
60
|
+
f"X API returned an error: {e.response.status_code} {e.response.reason_phrase}"
|
|
61
|
+
) from e
|
|
62
|
+
except httpx.RequestError as e:
|
|
63
|
+
raise ToolExecutionError(f"Failed to connect to X API: {e}") from e
|
|
64
|
+
|
|
65
|
+
# Parse the response JSON
|
|
66
|
+
response_data = response.json()
|
|
67
|
+
user_data = response_data.get("data")
|
|
68
|
+
|
|
69
|
+
if not user_data:
|
|
70
|
+
raise ToolExecutionError(f"X API response missing 'data' field. Response: {response_data}")
|
|
71
|
+
|
|
72
|
+
# Expand URLs in the user description and profile URL
|
|
73
|
+
user_data = expand_urls_in_user_description(user_data, delete_entities=False)
|
|
74
|
+
user_data = expand_urls_in_user_url(user_data, delete_entities=True)
|
|
75
|
+
|
|
76
|
+
return {"data": user_data}
|
|
@@ -2,9 +2,10 @@ arcade_x/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
2
2
|
arcade_x/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
arcade_x/tools/constants.py,sha256=d-OJK5Qx05JRUcpK5G1DcYB91wT37hFSzGmIYfwlEtA,42
|
|
4
4
|
arcade_x/tools/tweets.py,sha256=xfg31_Jh7sUuBfFjRIY_TwbUFCcYbm_xyUE_s35HB-8,9416
|
|
5
|
+
arcade_x/tools/user_context.py,sha256=nKlsM2LGdoIPl8FQ7FKqd6hkbPHVSJUiZdShZ7ZCsIk,2470
|
|
5
6
|
arcade_x/tools/users.py,sha256=gopYKTBOwbZAeVdL_T3vwkepbpv1bdPbqcU_2oPC3SY,2132
|
|
6
7
|
arcade_x/tools/utils.py,sha256=lR_shws08LWQD-4XU9r3xYmasZ2j2i-cUaCCDnuN8UE,5723
|
|
7
|
-
arcade_x-1.
|
|
8
|
-
arcade_x-1.
|
|
9
|
-
arcade_x-1.
|
|
10
|
-
arcade_x-1.
|
|
8
|
+
arcade_x-1.2.0.dist-info/METADATA,sha256=JJjv675epNksYQsVM50LV1jT6RuO3PwdafIcs2hpuOM,897
|
|
9
|
+
arcade_x-1.2.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
10
|
+
arcade_x-1.2.0.dist-info/licenses/LICENSE,sha256=ixeE7aL9b2B-_ZYHTY1vQcJB4NufKeo-LWwKNObGDN0,1960
|
|
11
|
+
arcade_x-1.2.0.dist-info/RECORD,,
|
|
File without changes
|