You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dybr2fedi/dybr2fedi.sh

89 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/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'
if [ ! -f "$basedir"/.auth ]; then
export basedir
"$basedir"/auth_helper.sh
if [ ! -f "$basedir"/.auth ]; then
echo 'Failed, access denied'
else
echo 'OK! Success'
fi
fi
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'
}
get_Lies_pages > "$basedir"/response.json
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
#