Skip to content

Instantly share code, notes, and snippets.

@CornuAmmonis
Last active June 17, 2018 17:08
Show Gist options
  • Select an option

  • Save CornuAmmonis/aa1155f5d3f22f6767ec to your computer and use it in GitHub Desktop.

Select an option

Save CornuAmmonis/aa1155f5d3f22f6767ec to your computer and use it in GitHub Desktop.
Shell script that uses twurl to upload a chunked native video to Twitter.
file=""
tweet=""
if [ -n "$1" ]
then
file="$1"
else
echo "Usage: splittwit /path/to/video.mp4 \"My tweet goes here!\""
exit
fi
if [ -n "$2" ]
then
tweet="$2"
else
echo "Usage: splittwit /path/to/video.mp4 \"My tweet goes here!\""
exit
fi
length=`ls -nl $file | awk '{print $5}'`
chunks=$(( 1 + $length / 5242880 )) # max chunk size is 5MB
echo "Splitting file of length $length bytes into $chunks chunks"
split -n${chunks} -d ${file} split_
echo "Getting media ID from Twitter"
media_id=`twurl -H upload.twitter.com "/1.1/media/upload.json" -d "command=INIT&media_type=video/mp4&total_bytes=${length}" | sed -r 's/\{"media_id":([0-9]+).*/\1/'`
echo "Received media ID $media_id"
for ((i=0; i<chunks; i++))
do
echo "Uploading chunk $i"
twurl -H upload.twitter.com "/1.1/media/upload.json" -d "command=APPEND&media_id=${media_id}&segment_index=${i}" --file ./split_0${i} --file-field "media"
done
echo "Finalizing upload"
twurl -H upload.twitter.com "/1.1/media/upload.json" -d "command=FINALIZE&media_id=${media_id}"
echo -e "\nTweeting!"
twurl "/1.1/statuses/update.json" -d "status=${tweet}&media_ids=${media_id}"
@dieRafa

dieRafa commented Apr 26, 2016

Copy link
Copy Markdown

He, I am trying to upload videos to twitter programmatically, so I came across your script. It produces the same error then the one I rote which is:
Splitting file of length 4980087 bytes into 1 chunks
Getting media ID from Twitter
Received media ID 724933006197137408
Uploading chunk 0
/usr/lib/ruby/1.9.1/net/protocol.rb:146:in rescue in rbuf_fill': Timeout::Error (Timeout::Error) from /usr/lib/ruby/1.9.1/net/protocol.rb:140:inrbuf_fill'
from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in readuntil' from /usr/lib/ruby/1.9.1/net/protocol.rb:132:inreadline'
from /usr/lib/ruby/1.9.1/net/http.rb:2562:in read_status_line' from /usr/lib/ruby/1.9.1/net/http.rb:2551:inread_new'
from /usr/lib/ruby/1.9.1/net/http.rb:1319:in block in transport_request' from /usr/lib/ruby/1.9.1/net/http.rb:1316:incatch'
from /usr/lib/ruby/1.9.1/net/http.rb:1316:in transport_request' from /usr/lib/ruby/1.9.1/net/http.rb:1293:inrequest'
from /usr/lib/ruby/1.9.1/net/http.rb:1286:in block in request' from /usr/lib/ruby/1.9.1/net/http.rb:745:instart'
from /usr/lib/ruby/1.9.1/net/http.rb:1284:in request' from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/oauth_client.rb:118:inperform_request_from_options'
from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/request_controller.rb:13:in perform_request' from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/request_controller.rb:9:indispatch'
from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/abstract_command_controller.rb:7:in dispatch' from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/cli.rb:38:indispatch'
from /var/lib/gems/1.9.1/gems/twurl-0.9.3/lib/twurl/cli.rb:21:in run' from /var/lib/gems/1.9.1/gems/twurl-0.9.3/bin/twurl:4:in<top (required)>'
from /usr/local/bin/twurl:19:in load' from /usr/local/bin/twurl:19:in

'
Finalizing upload
{"request":"/1.1/media/upload.json","error":"Segments do not add up to provided total file size."}
Tweeting!
{"request":"/1.1/statuses/update.json","error":"Read-only application cannot POST."}

Maybe you can help me! You find all the details in the output above!

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment