toot.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_with_image(){
  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. toot_post(){
  50. local status="$1"
  51. local data="status=$status"
  52. local error=$(curl --header "Authorization: Bearer $MASTODON_TOKEN" -sS -X POST https://$MASTODON_SERVER/api/v1/statuses -d "$data" | jq -r .error)
  53. if [ "$error" = "null" ]
  54. then
  55. echo "[$(date)][INFO] Tooted: $status"
  56. else
  57. echo $error
  58. exit 1
  59. fi
  60. }
  61. image_to_toot=""
  62. image_alt=""
  63. sensitive="false"
  64. if [ $# -eq 0 ]
  65. then
  66. toot_help
  67. exit 1
  68. fi
  69. for arg in "$@"
  70. do
  71. case $arg in
  72. -i=*|--image=*)
  73. image_to_toot="${arg#*=}"
  74. shift
  75. ;;
  76. -t=*|--token=*)
  77. MASTODON_TOKEN="${arg#*=}"
  78. shift
  79. ;;
  80. -s=*|--server=*)
  81. MASTODON_SERVER="${arg#*=}"
  82. shift
  83. ;;
  84. -a=*|--alt=*)
  85. image_alt="${arg#*=}"
  86. shift
  87. ;;
  88. -w|--warn)
  89. sensitive="true"
  90. shift
  91. ;;
  92. *)
  93. ;;
  94. esac
  95. done
  96. if [ $MASTODON_TOKEN = "" ]
  97. then
  98. echo "no token set"
  99. exit 1
  100. fi
  101. if [ "$image_to_toot" != "" ] && [ -f $image_to_toot ]
  102. then
  103. image_id=$(toot_upload_image "$image_to_toot" "$image_alt")
  104. toot_post_with_image "$*" "$image_id" "$sensitive"
  105. else
  106. toot_post "$*" "$image_id" "$sensitive"
  107. fi