Skip to content

Instantly share code, notes, and snippets.

@ahbanavi
Last active February 18, 2025 19:23
Show Gist options
  • Save ahbanavi/9a6c3582c70b636d66e32e668ea92e14 to your computer and use it in GitHub Desktop.
Save ahbanavi/9a6c3582c70b636d66e32e668ea92e14 to your computer and use it in GitHub Desktop.
Nyaa.si RSS Feed Monitor with Telegram Notifications
#!/bin/bash
# RSS Feed Monitor for Nyaa.si
# ==========================
# This script monitors nyaa.si RSS feed for new items matching a keyword and sends
# notifications via Telegram.
# By default, this script only checks for english-translated anime items.
#
# Requirements:
# - curl
# - xmlstarlet
# - Telegram bot token and chat ID
#
# Setup:
# 1. Install required packages:
# sudo apt-get install curl xmlstarlet
#
# 2. Set up Telegram bot:
# - Create a bot via @BotFather
# - Get the bot token
# - Start a chat with the bot
# - Get your chat ID using https://t.me/username_to_id_bot
#
# Usage:
# ./nyaa_rss_check.sh "<search keyword>"
#
# Examples:
# ./nyaa_rss_check.sh "One Piece"
# ./nyaa_rss_check.sh "[SubsPlease]"
#
# Notes:
# - Script runs for 10 hours by default
# - Checks RSS feed every minute
# - Stores processed items in processed_guids.txt
# - Only shows items published today and onwards
# Check if keyword is provided
if [ -z "$1" ]; then
echo "Usage: $0 <KEYWORD>"
exit 1
fi
KEYWORD="$1"
# Construct the RSS URL with the keyword
RSS_URL="https://nyaa.si/?page=rss&q=$(echo "$KEYWORD" | sed 's/ /+/g')&c=1_2&f=0"
# EDIT THESE VARIABLES
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID="CHAT_ID_TO_NOTIFIED"
PROCESSED_GUIDS_FILE="./processed_guids.txt"
RUN_DURATION=$((10 * 60 * 60)) # 10 hours in seconds
START_TIME=$(date +%s)
# END OF EDITABLE VARIABLES
# Function to send a Telegram message with HTML formatting
send_telegram_message() {
local message="$1"
curl -s -o /dev/null -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
-d "text=${message}" \
-d "parse_mode=HTML"
}
# Function to fetch and process the RSS feed
process_rss_feed() {
local rss_feed=$(curl -s "$RSS_URL")
# Get today's date in the same format as pubDate (e.g., "Sat, 15 Feb 2025")
local today=$(date -u +"%a, %d %b %Y")
echo "$rss_feed" | xmlstarlet sel -t -m "//item" -v "concat(pubDate, '|', title, '|', link, '|', nyaa:size, '|', guid)" -n | while IFS='|' read -r pub_date title link size guid; do
# Check if the item is from today
if [[ "$pub_date" == *"$today"* ]]; then
# Check if this item's GUID has already been processed
if ! grep -Fxq "$guid" "$PROCESSED_GUIDS_FILE"; then
# Send a Telegram message with HTML formatting
message="<b>New Item Found!</b>
<b>Title:</b> $title
<b>Size:</b> $size
<b>Link:</b> <a href=\"$link\">Torrent</a> | <a href=\"$guid\">View</a>"
send_telegram_message "$message"
# Print confirmation to the console
echo "New item found: $guid"
# Add the GUID to the processed GUIDs file
echo "$guid" >> "$PROCESSED_GUIDS_FILE"
fi
fi
done
}
# Initialize the processed GUIDs file if it doesn't exist
touch "$PROCESSED_GUIDS_FILE"
# Main loop to check the RSS feed every minute for 10 hours
while true; do
process_rss_feed
CURRENT_TIME=$(date +%s)
if (( CURRENT_TIME - START_TIME >= RUN_DURATION )); then
echo "Script has run for 10 hours. Exiting."
exit 0
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment