toot.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/sh
  2. # WTFPL!
  3. MASTODON_TOKEN=ENV["MASTODON_TOKEN"]
  4. MASTODON_SERVER="mastodon.bida.im"
  5. toot_help(){
  6. echo "toot [args..] your status update"
  7. echo "Argouments: "
  8. echo " -i=, --image= select file image to post"
  9. echo " -t=, --token= your API access token"
  10. echo " -s=, --server= mastodon server"
  11. echo ""
  12. echo "Mastodon server can be set in ~/.tootrc otherwise it will be defaulted to $MASTODON_SERVER"
  13. echo "The token can be passed as, cli argument, var in ~/.tootrc, env var (in this priority order)"
  14. }
  15. if [ -f ~/.tootrc ]
  16. then
  17. . ~/.tootrc
  18. fi
  19. toot_upload_image(){
  20. image="$1"
  21. id=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/media -F "file=@$image" | jq -r .id)
  22. if [ $? -eq 0 ] && [ -n "$id" ]
  23. then
  24. echo $id
  25. return 0
  26. fi
  27. echo "Image upload: Something went wrong"
  28. exit 1
  29. }
  30. toot_post(){
  31. status="$1"
  32. image="$2"
  33. data="status=$status&media_ids[]=$image"
  34. error=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/statuses -d "$data" | jq -r .error)
  35. if [ "$error" = "null" ]
  36. then
  37. echo "Yay!"
  38. else
  39. echo $error
  40. exit 1
  41. fi
  42. }
  43. image_to_toot=""
  44. if [ $# -eq 0 ]
  45. then
  46. toot_help
  47. exit 1
  48. fi
  49. for arg in "$@"
  50. do
  51. case $arg in
  52. -i=*|--image=*)
  53. image_to_toot="${arg#*=}"
  54. shift
  55. ;;
  56. -t=*|--token=*)
  57. MASTODON_TOKEN="${arg#*=}"
  58. shift
  59. ;;
  60. -s=*|--server=*)
  61. MASTODON_SERVER="${arg#*=}"
  62. shift
  63. ;;
  64. *)
  65. ;;
  66. esac
  67. done
  68. if [ $MASTODON_TOKEN = "" ]
  69. then
  70. echo "no token set"
  71. exit 1
  72. fi
  73. if [ -n $image_to_toot ] && [ -f $image_to_toot ]
  74. then
  75. image_id=$(toot_upload_image $image_to_toot)
  76. fi
  77. toot_post "$*" "$image_id"