toot.sh 1.7 KB

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