dybr2fedi/dybr2fedi.sh
2023-01-08 03:52:29 +03:00

59 lines
2.6 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
#Пробный образец "моста" Дыбра в gemini
#На данный момент только для аккаунта Лиэс
#Односторонний, без возможности взаимодействия с Дыбром
#Пассивная переодическая проверка на новые статьи с записью id уже отправленных, нужен cron или обернуть в бесконечный цикл
blog_id=1045 # liesliasau Dybr blog id
basedir=$(dirname $0)
baseblogdir=$basedir/blog
mkdir -p $baseblogdir
touch $baseblogdir/index.gmi
#[Dybr section]
get_Lies_pages()
{
curl --compressed --get --url 'https://dybr.ru/v2/entries' \
-H 'accept: application/json' \
-H 'user-agent: Dybr2Fedi,Dybr2Gemini,RSS 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'); do
raw_id=$(echo $i | tr -d '"')
if [ ! -f $baseblogdir/$raw_id.gmi ]; then
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")
published=$(jq -r ".[] | .[] | select(.id==$i) | .attributes[\"created-at\"]" "$basedir"/response.json)
tags=$(jj -l -i "$basedir"/response.json "data.#[id=$i].attributes.tags" | sed -e '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
echo "# $title\n$plain_content\n\ntags: $tags\n=> $orig orig" > $baseblogdir/$raw_id.gmi
touch -d "$published" $baseblogdir/$raw_id.gmi
echo "$raw_id.gmi $title ($published)" | cat - $baseblogdir/index.gmi > $basedir/tmp && mv tmp $baseblogdir/index.gmi
fi
done
#