Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions twitter_openapi_python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "twitter_openapi_python"
version = "0.0.26"
version = "0.0.29"
description = "Twitter OpenAPI"
authors = ["fa0311 <yuki@yuki0311.com>"]
license = "proprietary" # or "AGPL-3.0-only"
Expand All @@ -14,7 +14,7 @@ include = ["twitter_openapi_python/py.typed"]
python = "^3.7"
pydantic = ">=2.6"

twitter-openapi-python-generated = "0.0.24"
twitter-openapi-python-generated = "0.0.27"

[tool.poetry.dev-dependencies]

Expand All @@ -27,4 +27,7 @@ extension-pkg-whitelist = "pydantic"

[tool.pyright]
reportPrivateImportUsage = false
typeCheckingMode = "standard"
typeCheckingMode = "standard"

[tool.ruff]
line-length = 120
Binary file modified twitter_openapi_python/requirements.txt
Binary file not shown.
8 changes: 5 additions & 3 deletions twitter_openapi_python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from setuptools import find_packages, setup

NAME = "twitter_openapi_python"
VERSION = "0.0.26"
VERSION = "0.0.29"
PYTHON_REQUIRES = ">=3.7"
REQUIRES = [
"twitter_openapi_python_generated == 0.0.24",
"twitter_openapi_python_generated == 0.0.27",
"pydantic >= 2.6",
]
GITHUB_RAW_URL = "https://raw.githubusercontent.com/fa0311/twitter_openapi_python/refs/heads/master/twitter_openapi_python/"
GITHUB_RAW_URL = (
"https://raw.githubusercontent.com/fa0311/twitter_openapi_python/refs/heads/master/twitter_openapi_python/"
)


def image_replace(text: str, url: str):
Expand Down
1 change: 1 addition & 0 deletions twitter_openapi_python/test/api/test_1_post_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_post_create_tweet(self):
def test_post_delete_tweet(self):
time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
result = self.client.post_create_tweet(tweet_text=f"Test{time}")
assert result.data.data.create_tweet
tweet_id = result.data.data.create_tweet.tweet_results.result.rest_id

result2 = self.client.post_delete_tweet(tweet_id=tweet_id)
Expand Down
4 changes: 1 addition & 3 deletions twitter_openapi_python/test/api/test_1_tweet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def test_get_home_latest_timeline(self):
print_tweet(tweet)

def test_get_list_latest_tweets_timeline(self):
result = self.client.get_list_latest_tweets_timeline(
list_id="1141162794290520064"
)
result = self.client.get_list_latest_tweets_timeline(list_id="1141162794290520064")
for tweet in list(filter(self.ad_fillter, result.data.data)):
print_tweet(tweet)

Expand Down
29 changes: 11 additions & 18 deletions twitter_openapi_python/twitter_openapi_python/api/default_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Optional, Type, TypeVar
from typing import Any, Callable, Optional, TypeVar

import twitter_openapi_python_generated as twitter
import twitter_openapi_python_generated.models as models
Expand All @@ -7,13 +7,13 @@
from twitter_openapi_python.utils import (
build_response,
build_tweet_api_utils,
check_error,
get_kwargs,
)
from twitter_openapi_python.utils.api import error_check

T1 = TypeVar("T1")
T2 = TypeVar("T2")
ApiFnType = Callable[..., twitter.ApiResponse]
ApiFnType = Callable[..., twitter.ApiResponse[T1]]
ParamType = dict[str, Any]


Expand All @@ -27,20 +27,15 @@ def __init__(self, api: twitter.DefaultApi, flag: ParamType):

def request(
self,
apiFn: ApiFnType,
apiFn: "ApiFnType[T1]",
convertFn: Callable[[T1], T2],
type1: Type[T1],
type2: Type[T2],
key: str,
param: ParamType,
) -> TwitterApiUtilsResponse[T2]:
):
args = get_kwargs(flag=self.flag[key], additional=param)
res = apiFn(**args)
checked = check_error(data=res, type=type1)

data = convertFn(checked)

return build_response(response=res, data=data)
data = convertFn(res.data)
return build_response(res, data)

def get_profile_spotlights_query(
self,
Expand All @@ -55,9 +50,7 @@ def get_profile_spotlights_query(

response = self.request(
apiFn=self.api.get_profile_spotlights_query_with_http_info,
convertFn=lambda x: x.data.user_result_by_screen_name,
type1=models.ProfileResponse,
type2=models.UserResultByScreenName,
convertFn=lambda x: error_check(x.data.user_result_by_screen_name, x.errors),
key="ProfileSpotlightsQuery",
param=param,
)
Expand All @@ -74,9 +67,9 @@ def get_tweet_result_by_rest_id(

response = self.request(
apiFn=self.api.get_tweet_result_by_rest_id_with_http_info,
convertFn=lambda x: build_tweet_api_utils(x.data.tweet_result),
type1=models.TweetResultByRestIdResponse,
type2=TweetApiUtilsData,
convertFn=lambda x: error_check(
build_tweet_api_utils(error_check(x.data.tweet_result, x.errors)), x.errors
),
key="TweetResultByRestId",
param=param,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def request(self, url: str) -> HTTPResponse:
def get_initial_state(self, url: str) -> InitialStateApiUtilsResponse:
response = self.request(url=url).data.decode("utf-8")
reg = '<script type="text/javascript" charset="utf-8" nonce="{nonce}">{any}</script>'
js = re.search(
reg.format(nonce=r"([a-zA-Z0-9]{48})", any=r"([\s\S]*?)"), response
)
js = re.search(reg.format(nonce=r"([a-zA-Z0-9]{48})", any=r"([\s\S]*?)"), response)

if js is None:
raise Exception("js is None")
Expand All @@ -56,14 +54,14 @@ def get_initial_state(self, url: str) -> InitialStateApiUtilsResponse:
def get_user(e: dict[str, Any]) -> Optional[models.UserLegacy]:
try:
entities = e["entities"]["users"]["entities"]
return models.UserLegacy.parse_obj(list(entities.values())[0])
return models.UserLegacy.model_validate(list(entities.values())[0])
except Exception:
# todo: fix
return None

def get_session(e: dict[str, Any]) -> Optional[models.Session]:
try:
return models.Session.parse_obj(e["session"])
return models.Session.model_validate(e["session"])
except Exception:
return None

Expand Down
66 changes: 18 additions & 48 deletions twitter_openapi_python/twitter_openapi_python/api/post_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import twitter_openapi_python_generated.models as models

from twitter_openapi_python.models import TwitterApiUtilsResponse
from twitter_openapi_python.utils import build_response, check_error, non_nullable
from twitter_openapi_python.utils import build_response, non_nullable

T = TypeVar("T")
ResponseType = TwitterApiUtilsResponse[T]
Expand All @@ -19,14 +19,6 @@ def __init__(self, api: twitter.PostApi, flag: ParamType):
self.api = api
self.flag = flag

def builder(
self,
res: twitter.ApiResponse,
type: type[T],
) -> ResponseType[T]:
checked = check_error(data=res, type=type)
return build_response(response=res, data=checked)

def post_create_tweet(
self,
tweet_text: str,
Expand All @@ -37,15 +29,9 @@ def post_create_tweet(
conversation_control: Optional[str] = None,
) -> ResponseType[models.CreateTweetResponse]:
variables = non_nullable(
twitter.PostCreateTweetRequestVariables.from_dict(
self.flag["CreateTweet"]["variables"]
)
)
features = non_nullable(
twitter.PostCreateTweetRequestFeatures.from_dict(
self.flag["CreateTweet"]["features"]
)
twitter.PostCreateTweetRequestVariables.from_dict(self.flag["CreateTweet"]["variables"])
)
features = non_nullable(twitter.PostCreateTweetRequestFeatures.from_dict(self.flag["CreateTweet"]["features"]))
variables.tweet_text = tweet_text
if media_ids:
tagged_or = tagged_users or []
Expand All @@ -58,10 +44,8 @@ def post_create_tweet(
]
variables.attachment_url = attachment_url
if conversation_control:
variables.conversation_control = (
twitter.PostCreateTweetRequestVariablesConversationControl(
mode=conversation_control
)
variables.conversation_control = twitter.PostCreateTweetRequestVariablesConversationControl(
mode=conversation_control
)

if in_reply_to_tweet_id:
Expand All @@ -78,17 +62,14 @@ def post_create_tweet(
features=features,
),
)

return self.builder(res=res, type=models.CreateTweetResponse)
return build_response(res, res.data)

def post_delete_tweet(
self,
tweet_id: str,
) -> ResponseType[models.DeleteTweetResponse]:
variables = non_nullable(
twitter.PostCreateRetweetRequestVariables.from_dict(
self.flag["DeleteTweet"]["variables"]
)
twitter.PostCreateRetweetRequestVariables.from_dict(self.flag["DeleteTweet"]["variables"])
)
variables.tweet_id = tweet_id

Expand All @@ -99,17 +80,14 @@ def post_delete_tweet(
variables=variables,
),
)

return self.builder(res=res, type=models.DeleteTweetResponse)
return build_response(res, res.data)

def post_create_retweet(
self,
tweet_id: str,
) -> ResponseType[models.CreateRetweetResponse]:
variables = non_nullable(
twitter.PostCreateRetweetRequestVariables.from_dict(
self.flag["CreateRetweet"]["variables"]
)
twitter.PostCreateRetweetRequestVariables.from_dict(self.flag["CreateRetweet"]["variables"])
)
variables.tweet_id = tweet_id

Expand All @@ -120,17 +98,14 @@ def post_create_retweet(
variables=variables,
),
)

return self.builder(res=res, type=models.CreateRetweetResponse)
return build_response(res, res.data)

def post_delete_retweet(
self,
source_tweet_id: str,
) -> ResponseType[models.DeleteRetweetResponse]:
variables = non_nullable(
twitter.PostDeleteRetweetRequestVariables.from_dict(
self.flag["DeleteRetweet"]["variables"]
)
twitter.PostDeleteRetweetRequestVariables.from_dict(self.flag["DeleteRetweet"]["variables"])
)
variables.source_tweet_id = source_tweet_id

Expand All @@ -141,18 +116,15 @@ def post_delete_retweet(
variables=variables,
),
)

return self.builder(res=res, type=models.DeleteRetweetResponse)
return build_response(res, res.data)

# postFavoriteTweet
def post_favorite_tweet(
self,
tweet_id: str,
) -> ResponseType[models.FavoriteTweetResponseData]:
) -> ResponseType[models.FavoriteTweetResponse]:
variables = non_nullable(
twitter.PostCreateBookmarkRequestVariables.from_dict(
self.flag["FavoriteTweet"]["variables"]
)
twitter.PostCreateBookmarkRequestVariables.from_dict(self.flag["FavoriteTweet"]["variables"])
)
variables.tweet_id = tweet_id

Expand All @@ -163,17 +135,15 @@ def post_favorite_tweet(
variables=variables,
),
)
return self.builder(res=res, type=models.FavoriteTweetResponseData)
return build_response(res, res.data)

# postUnfavoriteTweet
def post_unfavorite_tweet(
self,
tweet_id: str,
) -> ResponseType[models.UnfavoriteTweetResponseData]:
) -> ResponseType[models.UnfavoriteTweetResponse]:
variables = non_nullable(
twitter.PostCreateRetweetRequestVariables.from_dict(
self.flag["UnfavoriteTweet"]["variables"]
)
twitter.PostCreateRetweetRequestVariables.from_dict(self.flag["UnfavoriteTweet"]["variables"])
)
variables.tweet_id = tweet_id

Expand All @@ -184,4 +154,4 @@ def post_unfavorite_tweet(
variables=variables,
),
)
return self.builder(res=res, type=models.UnfavoriteTweetResponseData)
return build_response(res, res.data)
Loading