今回の記事では、前回の記事の内容も踏まえてTwitterApiを使って複数の画像をツイートする方法を紹介します。
前回の記事
下準備
Twitter Apiを「Elevated」にアップグレード
Tweepyで画像を投稿するにはアカウントが「Elevated」になってないとダメです。
そのため利用申請をしていきましょう。
以下にログインしてください。
https://developer.twitter.com/en/portal/products/elevated
「Apply for Elevated」をクリックしてアップグレードしていきます。
次に自分のスキルレベルを聞かれるので答えましょう。
自分は一応未経験にしました。
次に進むといろいろ英語で聞かれるので答えていきましょう。
自分は色々参考にしながらGoogle翻訳にぶち込んで書きました。
質問1
In English, please describe how you plan to use Twitter data and/or APIs. The more detailed the response, the easier it is to review and approve.
TwitterのデータやAPIの使用方法を英語で説明してください。 回答が詳細であるほど、レビューと承認が容易になります。
回答1
Regarding the Twitter API, we mainly use the tweet function to post product introductions for affiliates on a regular basis.
We will use the Twitter data to analyze popular tweets in our account, but we do not intend to display it externally.
Twitter APIについては、主にツイート機能を使用しアフィリエイトのための商品紹介を定期的に投稿します。
Twitterのデータについては、自分のアカウントで人気のあるツイートの分析に使用しますが、外部に表示するつもりはありません。
質問2
Please describe how you will analyze Twitter data including any analysis of Tweets or Twitter users.
ツイートやTwitterユーザーの分析を含め、Twitterデータを分析する方法を説明してください。
回答2
I would like to regularly analyze what kind of tweets are popular and decide what kind of tweets should be made.
どのようなツイートが人気があるのかを定期的に分析し、どのようなツイートをしていくべきかの判断を行いたい。
質問3
Will your App use Tweet, Retweet, Like, Follow, or Direct Message functionality?
Please describe your planned use of these features.
アプリはツイート、リツイート、いいね、フォロー、ダイレクトメッセージ機能を使用しますか?
これらの機能の使用予定について説明してください。
回答3
I would like to regularly analyze what kind of tweets are popular and decide what kind of tweets should be made.
We don't currently expect to use the retweet, like, follow, or direct message features. I plan to use only the tweet function.
今のところリツイート、いいね、フォロー、またはダイレクトメッセージ機能を利用する想定はありません。ツイート機能のみを利用する予定です。
質問4,5
4.Do you plan to display Tweets or aggregate data about Twitter content outside Twitter?
Twitter以外のTwitterコンテンツに関するツイートや集計データを表示する予定はありますか?
5.Will your product, service, or analysis make Twitter content or derived information available to a government entity?
あなたの製品、サービス、または分析により、Twitterのコンテンツまたは派生情報を政府機関が利用できるようになりますか?
回答4,5
自分は使う予定はなかったのでどちらもNoにしました。
回答が終わったので「Next」で情報を確認してください。
確認出来たらまた「Next」で進みます。
規約に同意して「Submit」で申請が終わりです。
以下のようになっていると思います。
自分は上記の通りに回答してすぐに認証されました。
次にツイートする権限を変更しましょう。
ツイートできるように権限を変更
appのprojectにいくと「User authentication settings」と下のほうにあるので「Set up」をクリックします。
OAuth 1.0の方にチェックを入れると「App permissions」が出てくるので「Read and write」を選択してください。
そしたら「Callback URL」と「Website URL」を求められます。
自分はどちらにも自分のTwitterのプロフィールのURLを入力しました。
そして、「Save」で完了です。
では画像を投稿していきましょう。
画像投稿
全体のコード
import tweepy
# Consumer Keys
ck = "Consumer Key"
cs = "Consumer Key Secret"
# Authentication Tokens
at = "Authentication Token"
ats = "Authentication Token Secret"
auth = tweepy.OAuthHandler(ck, cs)
auth.set_access_token(at, ats)
api = tweepy.API(auth)
media_ids= []
images=["fail_path","fail_path","fail_path","fail_path"]
for image in images:
img = api.media_upload(image)
media_ids.append(img.media_id)
text="test\n改行"
api.update_status(status=text, media_ids=media_ids)
Twitter Apiで画像を投稿するには一度メディアをアップロードしないといけません。
api.media_upload(image)
これですね。
今回は複数(4枚)投稿したいのでList型にしてFor文で回してます。
アップロードしたら「media_id」が渡されるのでそれを使ってツイートするって感じです。
下準備の解説のほうが長かったですけど、コード自体は上記のようにめちゃくちゃ簡単です。
実践してみてください。
また、複数アカウントでツイートしたい方は以下の記事を見てみてください。