Files
dotfiles/scripts/scripts/post-to-facebook
2023-10-06 22:46:53 -04:00

95 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Posts to facebook group and personal page.
#
# Typical usage would be to generate a link to the latest
# blog post and send to facebook:
#
# `$ mhlink --last | xargs -I {} post-to-facebook -m <optional-message> {}`
#
#
#################### Options ####################
declare -a message
zparseopts -D -F -K -- \
{a,-all}=locationOpt \
{m,-message}+:=message \
{o,-offline}=offline \
{p,-personal}=locationOpt \
{h,-help}=help
baseUrl="https://graph.facebook.com/v18.0"
link=$1
message="${message[-1]}"
token=$(cat < "$HOME/.config/facebook-bot/access-token.txt")
#################### Usage ####################
function usage() {
cat <<EOF
post-to-facebook: Create posts on facebook using. This accepts
a link as an argument and a message option. Either the message
or the link (or both) needs to be supplied.
Typical usage:
post-to-facebook --message "Some message" "https://mhouhs.com/<post>"
Usage: post-to-facebook [-a] [-m <message>] [-o] [-p] <link>
-a | --all: Send the post to the group and personal pages.
-m | --message: The optional message.
-o | --offline: Do not send the request(s), but print them.
-p | --personal: Send to personal page only.
-h | --help: Show the usage.
The -a or -p options are optional, if neither is supplied then it will
only be posted to the group.
EOF
}
#################### Helpers ####################
function post() {
local url=$1
if [ -n "$offline" ]; then
echo "Offline mode, request not being sent."
http --offline --ignore-stdin POST "$url" access_token=="$token" \
link="$link" \
message="$message"
else
http --ignore-stdin POST "$url" access_token=="$token" \
link="$link" \
message="$message"
fi
}
function post_to_group() {
group=$(cat < "$HOME/.config/facebook-bot/group.txt")
post "$baseUrl/$group/feed"
}
function post_to_personal() {
post "$baseUrl/me/feed"
}
#################### Main ####################
[ -n "$help" ] && usage && exit 0
[ -z "$link" ] && [ -z "$message" ] \
&& echo "Link or message is required." \
&& exit 1
[ "$locationOpt" = "-p" ] || [ "$locationOpt" = "--personal" ] \
&& post_to_personal \
&& exit 0
[ "$locationOpt" = "-a" ] || [ "$locationOpt" = "--all" ] \
&& post_to_personal
post_to_group