Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use media_id to upload media #43

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion examples/chunked_media_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

X::MediaUploader.await_processing(client:, media:)

tweet_body = {text: "Posting media from @gem!", media: {media_ids: [media["media_id_string"]]}}
tweet_body = {text: "Posting media from @gem!", media: {media_ids: [media["id"]]}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy/pasted the example and had :

> media_category = "tweet_video" # other options include: tweet_image, tweet_gif, dm_image, dm_video, dm_gif, subtitles
=> "tweet_video"

> media = X::MediaUploader.chunked_upload(client:, file_path:, media_category:)
=> 
{"id"=>"1904180809726124032",
...

> media
=> 
{"id"=>"1904180809726124032",
 "media_key"=>"7_1904180809726124032",
 "size"=>2107842,
 "expires_after_secs"=>86400,
 "processing_info"=>{"state"=>"pending", "check_after_secs"=>1}}

> X::MediaUploader.await_processing(client:, media:)
=> 
{"expires_after_secs"=>86392,
 "id"=>"1904180809726124032",
 "media_key"=>"7_1904180809726124032",
 "processing_info"=>{"progress_percent"=>100, "state"=>"succeeded"},
 "size"=>2107842,
 "video"=>{"video_type"=>"video/mp4"}}

> tweet_body = {text: "Posting media from @gem!", media: {media_ids: [media["id"]]}}
=> {:text=>"Posting media from @gem!", :media=>{:media_ids=>["1904180809726124032"]}}

> tweet = client.post("tweets", tweet_body.to_json)
=> 
{"data"=>
...

> tweet
=> 
{"data"=>
  {"edit_history_tweet_ids"=>["1904180958837801012"],
   "id"=>"1904180958837801012",
   "text"=>"Posting media from @gem! https://t.co/Sgui6sMm6U"}}


tweet = client.post("tweets", tweet_body.to_json)

Expand Down
2 changes: 1 addition & 1 deletion examples/post_media_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

media = X::MediaUploader.upload(client:, file_path:, media_category:)

tweet_body = {text: "Posting media from @gem!", media: {media_ids: [media["media_id_string"]]}}
tweet_body = {text: "Posting media from @gem!", media: {media_ids: [media["id"]]}}

tweet = client.post("tweets", tweet_body.to_json)

Expand Down
6 changes: 3 additions & 3 deletions lib/x/media_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def chunked_upload(client:, file_path:, media_category:, media_type: infer_media
media = init(client:, file_path:, media_type:, media_category:)
chunk_size = chunk_size_mb * BYTES_PER_MB
append(client:, file_paths: split(file_path, chunk_size), media:, media_type:, boundary:)
client.post("media/upload?command=FINALIZE&media_key=#{media["media_key"]}")&.fetch("data")
client.post("media/upload?command=FINALIZE&media_id=#{media["id"]}")&.fetch("data")
end

def await_processing(client:, media:)
loop do
status = client.get("media/upload?command=STATUS&media_key=#{media["media_key"]}")&.fetch("data")
status = client.get("media/upload?command=STATUS&media_id=#{media["id"]}")&.fetch("data")
return status if !status["processing_info"] || PROCESSING_INFO_STATES.include?(status["processing_info"]["state"])

sleep status["processing_info"]["check_after_secs"].to_i
Expand Down Expand Up @@ -85,7 +85,7 @@ def append(client:, file_paths:, media:, media_type:, boundary: SecureRandom.hex
threads = file_paths.map.with_index do |file_path, index|
Thread.new do
upload_body = construct_upload_body(file_path:, media_type:, boundary:)
query = "command=APPEND&media_key=#{media["media_key"]}&segment_index=#{index}"
query = "command=APPEND&media_id=#{media["id"]}&segment_index=#{index}"
headers = {"Content-Type" => "multipart/form-data, boundary=#{boundary}"}
upload_chunk(client:, query:, upload_body:, file_path:, headers:)
end
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
TEST_OAUTH_NONCE = "TEST_OAUTH_NONCE".freeze
TEST_OAUTH_TIMESTAMP = Time.utc(1983, 11, 24).to_i.to_s
TEST_MEDIA_ID = "TEST_MEDIA_ID".freeze
TEST_MEDIA_KEY = "TEST_MEDIA_KEY".freeze

def test_oauth_credentials
{
Expand Down
18 changes: 9 additions & 9 deletions test/x/media_uploader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MediaUploaderTest < Minitest::Test

def setup
@client = Client.new
@media = {"id" => TEST_MEDIA_ID, "media_key" => TEST_MEDIA_KEY}
@media = {"id" => TEST_MEDIA_ID}
@data = {"data" => @media}
end

Expand All @@ -27,8 +27,8 @@ def test_chunked_upload
chunk_size_mb = (total_bytes - 1) / MediaUploader::BYTES_PER_MB.to_f
stub_request(:post, "https://api.twitter.com/2/media/upload?command=INIT&media_category=tweet_video&media_type=video/mp4&total_bytes=#{total_bytes}")
.to_return(status: 202, headers: {"content-type" => "application/json"}, body: @data.to_json)
2.times { |segment_index| stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_key=#{TEST_MEDIA_KEY}&segment_index=#{segment_index}").to_return(status: 204) }
stub_request(:post, "https://api.twitter.com/2/media/upload?command=FINALIZE&media_key=#{TEST_MEDIA_KEY}")
2.times { |segment_index| stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_id=#{TEST_MEDIA_ID}&segment_index=#{segment_index}").to_return(status: 204) }
stub_request(:post, "https://api.twitter.com/2/media/upload?command=FINALIZE&media_id=#{TEST_MEDIA_ID}")
.to_return(status: 201, headers: {"content-type" => "application/json"}, body: @data.to_json)

response = MediaUploader.chunked_upload(client: @client, file_path:, media_category: MediaUploader::TWEET_VIDEO, chunk_size_mb:)
Expand All @@ -41,16 +41,16 @@ def test_append_method
file_paths = MediaUploader.send(:split, file_path, File.size(file_path) - 1)

file_paths.each_with_index do |_chunk_path, segment_index|
stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_key=#{TEST_MEDIA_KEY}&segment_index=#{segment_index}")
stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_id=#{TEST_MEDIA_ID}&segment_index=#{segment_index}")
.with(headers: {"Content-Type" => "multipart/form-data, boundary=AaB03x"}).to_return(status: 204)
end
MediaUploader.send(:append, client: @client, file_paths:, media: @media, media_type: "video/mp4", boundary: "AaB03x")

file_paths.each_with_index { |_, segment_index| assert_requested(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_key=#{TEST_MEDIA_KEY}&segment_index=#{segment_index}") }
file_paths.each_with_index { |_, segment_index| assert_requested(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_id=#{TEST_MEDIA_ID}&segment_index=#{segment_index}") }
end

def test_await_processing
stub_request(:get, "https://api.twitter.com/2/media/upload?command=STATUS&media_key=#{TEST_MEDIA_KEY}")
stub_request(:get, "https://api.twitter.com/2/media/upload?command=STATUS&media_id=#{TEST_MEDIA_ID}")
.to_return(headers: {"content-type" => "application/json"}, body: '{"data":{"processing_info": {"state": "pending"}}}')
.to_return(headers: {"content-type" => "application/json"}, body: '{"data":{"processing_info": {"state": "succeeded"}}}')
result = MediaUploader.await_processing(client: @client, media: @media)
Expand All @@ -59,7 +59,7 @@ def test_await_processing
end

def test_await_processing_and_failed
stub_request(:get, "https://api.twitter.com/2/media/upload?command=STATUS&media_key=#{TEST_MEDIA_KEY}")
stub_request(:get, "https://api.twitter.com/2/media/upload?command=STATUS&media_id=#{TEST_MEDIA_ID}")
.to_return(headers: {"content-type" => "application/json"}, body: '{"data":{"processing_info": {"state": "pending"}}}')
.to_return(headers: {"content-type" => "application/json"}, body: '{"data":{"processing_info": {"state": "failed"}}}')
result = MediaUploader.await_processing(client: @client, media: @media)
Expand All @@ -71,9 +71,9 @@ def test_retry
file_path = "test/sample_files/sample.mp4"
stub_request(:post, "https://api.twitter.com/2/media/upload?command=INIT&media_category=tweet_video&media_type=video/mp4&total_bytes=#{File.size(file_path)}")
.to_return(status: 202, headers: {"content-type" => "application/json"}, body: @data.to_json)
stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_key=#{TEST_MEDIA_KEY}&segment_index=0")
stub_request(:post, "https://api.twitter.com/2/media/upload?command=APPEND&media_id=#{TEST_MEDIA_ID}&segment_index=0")
.to_return(status: 500).to_return(status: 204)
stub_request(:post, "https://api.twitter.com/2/media/upload?command=FINALIZE&media_key=#{TEST_MEDIA_KEY}")
stub_request(:post, "https://api.twitter.com/2/media/upload?command=FINALIZE&media_id=#{TEST_MEDIA_ID}")
.to_return(status: 201, headers: {"content-type" => "application/json"}, body: @data.to_json)

assert MediaUploader.chunked_upload(client: @client, file_path:, media_category: MediaUploader::TWEET_VIDEO)
Expand Down
Loading