toot.sh 2.3 KB

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