dybr2fedi/dybr2fedi.sh

89 lines
3.0 KiB
Bash
Raw Permalink Normal View History

2022-08-14 05:40:14 +00:00
#!/bin/sh
#Пробный образец "моста" Дыбра в Федивёрс
#На данный момент только для аккаунта Лиэс
#Односторонний, без возможности взаимодействия с Дыбром
#Пассивная переодическая проверка на новые статьи с записью id уже отправленных, нужен cron или обернуть в бесконечный цикл
blog_id=1045 # liesliasau Dybr blog id
basedir=$(dirname $0)
log_sended_articles="$basedir"/sended_dybr_ids
touch $log_sended_articles
#[Fedi section]
instance_point='https://expired.mentality.rip/api/v1'
2022-08-17 05:25:17 +00:00
if [ ! -f "$basedir"/.auth ]; then
export basedir
2022-08-14 12:52:01 +00:00
"$basedir"/auth_helper.sh
2022-08-17 05:25:17 +00:00
if [ ! -f "$basedir"/.auth ]; then
2022-08-14 12:52:01 +00:00
echo 'Failed, access denied'
else
echo 'OK! Success'
fi
fi
2022-08-14 05:40:14 +00:00
auth=$(cat "$basedir"/.auth | tr -d '\n')
post_request()
{
curl -s --compressed -X POST -H "Authorization: Bearer $auth" "$@"
}
write_api_status()
{
post_request --url "$instance_point"/statuses \
--data-urlencode "status@-" \
--data-urlencode "spoiler_text=$title" \
--data-raw "content_type=text/plain" \
--data-raw "visibility=unlisted"
}
#
#[Dybr section]
get_Lies_pages()
{
curl --compressed --get --url 'https://dybr.ru/v2/entries' \
-H 'accept: application/json' \
-H 'user-agent: Dybr2Fedi Bridge' \
--data-urlencode "filters[blog-id]=$blog_id" \
--data-urlencode 'filters[state]=published' \
--data-urlencode 'include=profile,community' \
--data-urlencode 'page[number]=1' \
--data-urlencode 'page[size]=10' \
--data-urlencode 'sort=-published-at'
}
2022-08-14 12:30:41 +00:00
get_Lies_pages > "$basedir"/response.json
2022-08-14 05:40:14 +00:00
for i in $(jj -l -i "$basedir"/response.json 'data.#.id' | tac); do
orig=$(echo $i | sed -e 's/"//g; s|^|https://dybr.ru/blog/liesliasau/|g')
title=$(jj -i "$basedir"/response.json "data.#[id=$i].attributes.title")
tags=$(jj -l -i "$basedir"/response.json "data.#[id=$i].attributes.tags" | tr ' -' '_' | tr -d '.!'| sed -e 's/"//g ; s/^/\#/g' | tr '\n' ' ')
content=$(jj -i "$basedir"/response.json "data.#[id=$i].attributes.content")
links_img=$(echo "$content" | pup 'img attr{src}' | sed 's|^|https://dybr.ru|g')
plain_content=$(echo "$content" | w3m -T text/html -cols 65535 -dump -o display_link_number=1 | sed -e 's|\[MORE=.*\]||g ; s|\[/MORE\]||g ; s|\[S:|~~|g ; s|:S\]|~~|g')
# Unpack inline images...
placeholders_img=$(echo "$plain_content" | grep -oP '\[\d{10}\]')
pseudo_index=$(echo "$links_img" | wc -l)
index=1
while [ $index -le $pseudo_index ]; do
placeholder=$(echo "$placeholders_img" | sed $index!d | tr -d '[]')
img=$(echo "$links_img" | sed $index!d)
plain_content=$(echo "$plain_content" | sed "s|\[$placeholder\]| $img |g")
index=$(expr $index + 1)
done
#
grep -F $i $log_sended_articles
sended=$?
if [ $sended -gt 0 ]; then
export title
echo "$plain_content\n\n$tags\norig: $orig" | write_api_status
echo $i >> $log_sended_articles
sleep 60
elif [ $sended -eq 0 ]; then
echo "Already sended"
fi
done
#