feat: add twitter article markdown command (#16)

This commit is contained in:
jakevin
2026-03-12 14:47:49 +08:00
committed by GitHub
parent 1c0e4b0c39
commit 79eadd2579
9 changed files with 291 additions and 4 deletions

View File

@@ -43,7 +43,7 @@ from .graphql import (
_resolve_query_id,
_update_features_from_html,
)
from .models import UserProfile
from .models import Tweet, UserProfile
from .parser import (
_deep_get,
_parse_int,
@@ -321,6 +321,45 @@ class TwitterClient:
},
)
def fetch_article(self, tweet_id):
# type: (str) -> Tweet
"""Fetch a Twitter Article by tweet ID."""
logger.debug("fetch_article: tweet_id=%s", tweet_id)
data = self._graphql_get(
"TweetResultByRestId",
variables={
"tweetId": tweet_id,
"withCommunity": False,
"includePromotedContent": False,
"withVoice": False,
},
features={
"longform_notetweets_consumption_enabled": True,
"responsive_web_twitter_article_tweet_consumption_enabled": True,
"longform_notetweets_rich_text_read_enabled": True,
"longform_notetweets_inline_media_enabled": True,
"articles_preview_enabled": True,
"responsive_web_graphql_exclude_directive_enabled": True,
"verified_phone_label_enabled": False,
},
field_toggles={
"withArticleRichContentState": True,
"withArticlePlainText": True,
},
)
result = _deep_get(data, "data", "tweetResult", "result")
if not result:
raise NotFoundError("Article not found: tweet_id=%s" % tweet_id)
tweet = parse_tweet_result(result)
if tweet is None or (tweet.article_title is None and tweet.article_text is None):
raise NotFoundError("Tweet %s has no article content" % tweet_id)
logger.info("fetch_article: tweet_id=%s", tweet_id)
return tweet
def fetch_list_timeline(self, list_id, count=20):
# type: (str, int) -> List[Tweet]
"""Fetch tweets from a Twitter List."""