C# 版は Python 版(twikit 2.4.0)の構造をそのまま移植しています。Python の関数名を C# の慣習
(PascalCase、非同期メソッドは ...Async)に置き換えただけなので、Python 版のコードは機械的に読み替えられます。
| Python | C# |
|---|---|
from twikit import Client |
using Twikit; / new Client(...) |
client.search_tweet(q, 'Latest') |
client.SearchTweetAsync(q, "Latest") |
client.get_user_by_screen_name(name) |
client.GetUserByScreenNameAsync(name) |
client.get_user_tweets(id, 'Tweets') |
client.GetUserTweetsAsync(id, "Tweets") |
client.create_tweet(text, media_ids=...) |
client.CreateTweetAsync(text, mediaIds) |
client.upload_media(path) |
client.UploadMediaAsync(path)(byte[] を受けるオーバーロードもあります) |
client.get_dm_history(user_id) |
client.GetDmHistoryAsync(userId) |
client.get_streaming_session(topics) |
client.GetStreamingSessionAsync(topics) |
client.spaces.get_space(id) |
client.Spaces.GetSpaceAsync(id) |
client.set_cookies({...}) |
client.SetCookies(new Dictionary<string, string> {...}) |
client.load_cookies(path) / save_cookies |
client.LoadCookies(path) / client.SaveCookies(path) |
client.user_id() / client.user() |
client.UserIdAsync() / client.UserAsync() |
client.rate_limit_remaining |
client.RateLimitRemaining |
client.refresh_transaction() |
client.RefreshTransaction() |
result.next() / result.previous() |
result.NextAsync() / result.PreviousAsync() |
tweet.text / tweet.full_text / tweet.user |
tweet.Text / tweet.FullText / tweet.User |
tweet.created_at_datetime |
tweet.CreatedAtDatetime(DateTimeOffset) |
client.highlight_tweet(id) / unhighlight_tweet(id) |
client.HighlightTweetAsync(id) / client.UnhighlightTweetAsync(id) |
client.is_tweet_highlighted(id) |
client.IsTweetHighlightedAsync(id) |
tweet.highlighted / await tweet.is_highlighted() |
tweet.Highlighted(bool?)/ await tweet.IsHighlightedAsync() |
client.pin_tweet(id) / unpin_tweet(id) |
client.PinTweetAsync(id) / client.UnpinTweetAsync(id) |
await tweet.pin() / await tweet.unpin() |
await tweet.PinAsync() / await tweet.UnpinAsync() |
client.translate_tweet(id, dst_lang) / await tweet.translate(dst_lang) |
client.TranslateTweetAsync(id, dstLang) / await tweet.TranslateAsync(dstLang)(TweetTranslation、.Text が翻訳文) |
client.get_draft_tweets() / create_draft_tweet(text) / delete_draft_tweets(ids) |
client.GetDraftTweetsAsync() / CreateDraftTweetAsync(text) / DeleteDraftTweetsAsync(ids) |
client.post_draft_tweet(draft) / await draft.post() |
client.PostDraftTweetAsync(draft) / await draft.PostAsync() |
client.get_community_notes(tweet_id) / await tweet.get_community_notes() |
client.GetCommunityNotesAsync(tweetId) / await tweet.GetCommunityNotesAsync() |
client.get_community_notes_profile(alias) |
client.GetCommunityNotesProfileAsync(alias)(CommunityNotesProfile) |
client.get_contributor_community_notes(alias, count, include_drafts) |
client.GetContributorCommunityNotesAsync(alias, count, includeDrafts) |
client.get_community_notes_timeline(tab) |
client.GetCommunityNotesTimelineAsync(CommunityNotesTimelineTab.NeedsYourHelp) |
client.rate_community_note(note_id, tweet_id, helpfulness, helpful_tags=...) / await note.rate(...) |
client.RateCommunityNoteAsync(noteId, tweetId, CommunityNoteHelpfulness.Helpful, helpfulTags: ...) / await note.RateAsync(...) |
twikit.constants.COMMUNITY_NOTE_HELPFUL_TAGS などのタプル |
CommunityNoteHelpfulTags / CommunityNoteNotHelpfulTags / CommunityNoteRatingSource の定数クラス |
twikit.List |
TwitterList(System.Collections.Generic.List<T> との混同を避けるため) |
twikit.build_query(text, options) |
SearchQuery.Build(text, new SearchOptions { ... }) |
twikit.streaming.Topic.dm_update(id) |
Twikit.Streaming.Topic.DmUpdate(id) |
twikit.guest.GuestClient |
Twikit.Guest.GuestClient |
twikit.Capsolver |
Twikit.Captcha.Capsolver |
例外は ...Exception を付けた名前になります: BadRequestException、UnauthorizedException、ForbiddenException、
NotFoundException、RequestTimeoutException、TooManyRequestsException、ServerErrorException、CouldNotTweetException、
DuplicateTweetException、TweetNotAvailableException、InvalidMediaException、UserNotFoundException、
UserUnavailableException、AccountSuspendedException、AccountLockedException、ClientTransactionException、
LoginRetiredException、InvalidSessionException。基底クラスは Python 版と同じ TwitterException です。
ValueError / TypeError に相当する引数エラーは ArgumentException になります。
- 型付き — Python 版で
dictを返していたメソッドの多くは、C# ではモデルクラスかJsonObject(System.Text.Json.Nodes)を返します。get_place_trendsはPlaceTrendsクラス、voteはPoll、get_about_accountはJsonObjectです。 - ミューテーションの戻り値 —
favorite_tweetなどが返していたhttpx.ResponseはHttpResponseMessageになります。 Tweet.replies— Python 版ではResultとリストが混在していましたが、C# 版では常にResult<Tweet>?です。Tweet.community_note— 辞書ではなくBirdwatchNote(Id、Text)を返します。Tweet.urls/User.urlsはUrlEntityのリストです(生の JSON はRawから読めます)。Community.creator— 完全なユーザー情報があるときはCreator(User)、ID とスクリーンネームだけのときはCreatorInfo(CommunityCreator)に入ります。- Cookie —
CookieContainerを使い、.x.comと.twitter.comに固定して保存します(Python 版と同じ挙動)。 X が返すホスト単位のct0の重複は、Python 版と同じく応答のたびに取り除かれます。 impersonate=は無い — curl_cffi 相当の TLS 偽装は .NET 標準に無いため、代わりにhandler引数で独自のHttpMessageHandlerを差し込めます。- ストリーミング —
StreamingSessionはIAsyncEnumerable<(string? Topic, Payload Payload)>なのでawait foreachで読みます。 - X スペースの音声(WebRTC) — Python 版は aiortc を使いますが、.NET 標準に WebRTC の実装は無いため、
IPeerConnection/IPeerConnectionFactoryインターフェイスを用意しています。SIPSorcery などで実装してSpeakAsync/HostAsync/ListenAsyncに渡してください。メタデータ・検索・作成・終了・モデレーション・ チャット(履歴 + WebSocket)は実装無しで動作します。 - 警告 — Python 版の
warnings.warnはTwikitWarnings.Handler(既定はTrace.TraceWarning)に流れます。 loginのcode_callback—Func<string, Task<string>>を渡します(既定は標準入力)。- ゲストクライアント —
GuestClientも移植していますが、Python 版と同じく X 側でゲストアクセスが閉じられているためActivateAsync()はInvalidSessionExceptionになります。
X-Client-Transaction-Id の計算(アニメーションキー、float_to_hex、3 次ベジェ、丸め)は、Python 版の実装で生成した
テストフィクスチャ(tests/Twikit.Tests/Fixtures)と bit 単位で一致することをテストで確認しています。
Python の round() は .NET の Math.Round と結果が異なる値があるため(例: 2.675)、C# 版では double の厳密な
二進値から丸める PyCompat.Round を使っています。