Files
Linux-Tech-Tips/Scripts/arch_linux-news.sh

38 lines
986 B
Bash

#!/bin/bash
set -eou pipefail
# Variables
CHAT_ID=""
TOKEN=""
URL="https://archlinux.org"
RECENT_FILE="/tmp/recent_arch_news"
# Creates file if not existing
if [[ ! -f "${RECENT_FILE}" ]]; then
touch "${RECENT_FILE}"
fi
# Check if recent news has already been send
LATEST_NEWS=$(curl -s "$URL/news/" | xmllint --html --xpath 'string(//table[@id="article-list"]/tbody/tr[1]/td[2]/a/@href)' - 2>/dev/null)
RECENT_NEWS=$(cat "${RECENT_FILE}")
if [[ "${LATEST_NEWS}" == "${RECENT_NEWS}" ]]; then
exit 0
fi
# Takes URL and title to make message text
NEWS_URL="${URL}${LATEST_NEWS}"
TITEL=$(curl -s "${NEWS_URL}" | xmllint --html --xpath 'string(//div[@id="content"]//h2)' - 2>/dev/null)
TEXT="${TITEL}: ${NEWS_URL}"
# Send message
send_message(){
curl -s -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" -d text="${TEXT}"
}
# If message was send save news to file
if send_message; then
echo "${LATEST_NEWS}" > "${RECENT_FILE}"
fi