TwiiterのAPIを使用して動画を投稿する際にアクション誘導リンク(call_to_action_url)を付けて投稿したいのですがどのようなコードを追加をすれば投稿できるのでしょうか
動画投稿のpythonコードは
以下です
import settings from requests_oauthlib import OAuth1Session import json import os import time twitter = OAuth1Session(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, resource_owner_key = settings.ACCESS_TOKEN_KEY, resource_owner_secret= settings.ACCESS_TOKEN_SECRET ) tweetUrl = 'https://api.twitter.com/1.1/statuses/update.json' uploadUrl = 'https://upload.twitter.com/1.1/media/upload.json' def tweetWithVideo(): total_bytes = os.path.getsize("動画ファイル") initParams = { "command" : "INIT", "media_type" : "video/mp4", "total_bytes" : total_bytes, "media_category" : "tweet_video", } init_response = twitter.post(url=uploadUrl ,data=initParams) media_id = init_response.json()["media_id"] segment_id = 0 bytes_sent = 0 with open("動画ファイル", "rb") as f: while bytes_sent < total_bytes: chunk = f.read(4*1024*1024) appendParams = { "command" : "APPEND", "media_id" : media_id, "segment_index" : segment_id } files = { "media" : chunk } append_response = twitter.post(url=uploadUrl, data = appendParams,files=files ) if append_response.status_code < 200 or append_response.status_code > 299: print(append_response.status_code) print(append_response.text) segment_id = segment_id + 1 bytes_sent = f.tell() print("%s of %s bytes uploaded" % (str(bytes_sent), str(total_bytes) )) print("アップロード完了") finalizeParams = { "command" : "FINALIZE", "media_id" : media_id } finalize_response = twitter.post(url=uploadUrl,data=finalizeParams) statusParams = { "command" : "STATUS", "media_id" : media_id } status_response = twitter.get(url=uploadUrl,params=statusParams) processing_info = status_response.json().get("processing_info", None) # print(processing_info) while processing_info["state"] == "in_progress": time.sleep(1) status_response = twitter.get(url=uploadUrl,params=statusParams) processing_info = status_response.json().get("processing_info", None) print(processing_info["progress_percent"]) print("ファイナライズ完了") params = { "media_ids" : media_id , } response = twitter.post(url=tweetUrl, data=params) print(response.status_code) if append_response.status_code < 200 or response.status_code > 299: print("ツイート失敗: %s", response.text) if __name__ == "__main__": tweetWithVideo()
あなたの回答
tips
プレビュー