Last active
February 2, 2023 17:05
-
-
Save EmberHeartshine/a981647422333c705c4a1932e7d36b4f to your computer and use it in GitHub Desktop.
Simple Bash script to mirror any Twitter account to a Mastodon account you control.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# birdsite_mirror.sh | |
# Copyright (c) 2023 Ember Heartshine | |
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# SYSTEM REQUIREMENTS: | |
# - python | |
# - jq | |
# - yq (pip install yq) | |
# Some notes: | |
# - Because this script uses Nitter as a scraper, it cannot handle video posts. | |
# - This script is tested working with crontab, assuming a standard python/pip install. | |
# - Example crontab assuming the script is located in ~: ~/birdsite_mirror.sh >> ~/birdlog.log 2>&1 | |
# User-friendly name (can be anything, but should not share with others of the same script) | |
NAME= | |
# Your Mastodon instance | |
INSTANCE= | |
# Mastodon access token; get it from Preferences > Development > $YOUR_APPLICATION | |
# If you haven't made an application, do so now and get the access token. | |
# The application MUST have the following permissions: write:media write:statuses | |
AUTH= | |
# The Twitter account to monitor (CASE SENSITIVE; I recommend copying straight from the URL) | |
BIRDACCT= | |
# The Nitter instance to use (default should be fine unless you have any reason to use another instance) | |
NITTER=nitter.fly.dev | |
cd "$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)" | |
PATH=$PATH":/home/$(whoami)/.local/bin" | |
if test -f $NAME'_pubdate.txt'; then | |
RSS=$(curl -s -L http://$NITTER/$BIRDACCT/rss) | |
POSTDATE=$(echo "$RSS" |xq -r '.rss.channel.item[0].pubDate') | |
if [[ "$POSTDATE" == 'null' ]]; then | |
echo "Error connecting to $NITTER for $NAME. Nothing to do." | |
elif [[ "$(cat $NAME'_pubdate.txt')" == "$POSTDATE" ]]; then | |
echo "No new $NAME post to fetch." | |
else | |
DESCRIPTION=$(echo "$RSS" |xq -r '.rss.channel.item[0].description') | |
TITLE=$(echo "$RSS" |xq -r '.rss.channel.item[0].title') | |
CREATOR=$(echo "$RSS" |xq -r '.rss.channel.item[0]."dc:creator"') | |
if [[ $(echo "$DESCRIPTION" |grep '<img' |grep -c -v 'card_img') -gt 0 ]]; then | |
FIELDNUM=${NITTER//[^\.]} | |
FIELDNUM=$(( 2 + ${#FIELDNUM} )) | |
if [[ $(echo "$DESCRIPTION" |sed 's/ /\n/g' |grep -c 'src=') -gt 1 ]]; then | |
echo "New album post found for $NAME. Fetching and uploading to Mastodon." | |
IMGARR=($(echo "$DESCRIPTION" |sed 's/ /\n/g' |grep 'src=' |cut -d '"' -f2 |tr '\n' ' ')) | |
for IMAGE in "${IMGARR[@]}" | |
do | |
EXT=$(echo "$IMAGE" |cut -d '.' -f $FIELDNUM) | |
curl -s -L "$IMAGE" -o $NAME.$EXT | |
IMGID+=($(curl -s -H "Authorization: Bearer "$AUTH -X POST -H 'Content-Type: multipart/form-data' https://$INSTANCE/api/v2/media --form file="@"$NAME.$EXT |jq -r '.id')) | |
rm $NAME.$EXT | |
done | |
else | |
echo "Found new image post for $NAME. Fetching and uploading to Mastodon." | |
IMAGE=$(echo "$DESCRIPTION" |sed 's/ /\n/g' |grep 'src=' |cut -d '"' -f2 |head -1) | |
EXT=$(echo "$IMAGE" |cut -d '.' -f $FIELDNUM) | |
curl -s -L "$IMAGE" -o $NAME.$EXT | |
IMGID=$(curl -s -H "Authorization: Bearer "$AUTH -X POST -H 'Content-Type: multipart/form-data' https://$INSTANCE/api/v2/media --form file="@"$NAME.$EXT |jq -r '.id') | |
rm $NAME.$EXT | |
fi | |
TEXT=$(echo "$TITLE" |sed 's/^@/.@/g') | |
if [[ $(echo "$CREATOR" |sed s/^@//g) != "$BIRDACCT" ]]; then | |
TEXT="From $CREATOR $TEXT" | |
fi | |
if [[ -n $(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/") ]]; then | |
TEXT="$TEXT"' QT @'$(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/" |cut -d '/' -f4)': '$(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/" |cut -d '"' -f2) | |
fi | |
for ID in "${IMGID[@]}" | |
do | |
IDFIELD+=( -F 'media_ids[]="'$ID'"' ) | |
done | |
curl -s https://$INSTANCE/api/v1/statuses -H "Authorization: Bearer $AUTH" -F "status=$TEXT" "${IDFIELD[@]}" |jq -r '.url' >> $NAME'_entries.log' | |
else | |
echo "Found new text post for $NAME. Mirroring to Mastodon." | |
TEXT=$(echo "$TITLE" |sed 's/^@/.@/g') | |
if [[ $(echo "$CREATOR" |sed s/^@//g) != "$BIRDACCT" ]]; then | |
TEXT="From $CREATOR $TEXT" | |
fi | |
if [[ -n $(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/") ]]; then | |
TEXT="$TEXT"' QT @'$(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/" |cut -d '/' -f4)': '$(echo "$DESCRIPTION" |grep -E "$NITTER/.*/status/" |cut -d '"' -f2) | |
fi | |
curl -s https://$INSTANCE/api/v1/statuses -H "Authorization: Bearer "$AUTH -F "status=$TEXT" |jq -r '.url' >> $NAME'_entries.log' | |
fi | |
echo "$POSTDATE" > $NAME'_pubdate.txt' | |
fi | |
else | |
echo 0 > $NAME'_pubdate.txt' | |
./${0##*/} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment