From e633cf083ce34588f36623279300a5a185539e16 Mon Sep 17 00:00:00 2001 From: Origami Official <64251776+origamiofficial@users.noreply.github.com> Date: Wed, 4 Sep 2024 19:59:08 +0930 Subject: [PATCH] Add RSS Feed generate function --- README.md | 2 +- main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e6e4180..017d289 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # AIUB Notice Checker -[![Facebook](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Facebook.svg)](https://facebook.com/aiubnotice) [![Telegram](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Telegram.svg)](https://t.me/aiubnotice) [![Twitter](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Twitter.svg)](https://twitter.com/aiubnotice) [![LinkedIn](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/LinkedIN.svg)](https://linkedin.com/in/aiubnotice) [![Discord](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Discord.svg)](https://discord.gg/M8XVrA2Fnb)
+[![Facebook](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Facebook.svg)](https://facebook.com/aiubnotice) [![Telegram](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Telegram.svg)](https://t.me/aiubnotice) [![Twitter](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Twitter.svg)](https://twitter.com/aiubnotice) [![LinkedIn](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/LinkedIN.svg)](https://linkedin.com/in/aiubnotice) [![Discord](https://raw.githubusercontent.com/gauravghongde/social-icons/master/SVG/Color/Discord.svg)](https://discord.gg/M8XVrA2Fnb) [![Valid RSS](https://validator.w3.org/feed/images/valid-rss-rogers.png)](http://validator.w3.org/feed/check.cgi?url=https%3A//raw.githubusercontent.com/origamiofficial/TestLab/main/rss.xml)
[![AIUB Notice Checker](https://github.com/origamiofficial/aiub-notice-checker/actions/workflows/aiub-notice-checker.yml/badge.svg)](https://github.com/origamiofficial/aiub-notice-checker/actions/workflows/aiub-notice-checker.yml) ![We Support](https://img.shields.io/badge/we%20stand%20with-%F0%9F%87%B5%F0%9F%87%B8%20palestine-white.svg) A Python script that checks for new or edited posts on the [AIUB Notice page](https://aiub.cf/category/notices/) and sends updates to a specified Telegram channel. diff --git a/main.py b/main.py index 9da8da6..b9f31c2 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ import sqlite3 import os import sys +import xml.etree.ElementTree as ET +import datetime # Environment variable information TELEGRAM_CHAT_ID = os.environ["TELEGRAM_CHAT_ID"] @@ -10,7 +12,7 @@ TELEGRAM_BOT_API_KEY = os.environ["TELEGRAM_BOT_API_KEY"] GITHUB_RUN_NUMBER = os.environ["GITHUB_RUN_NUMBER"] PROTOCOLS = ["https://", "http://"] -NOTICE_PAGE = "www.aiub.edu/category/notices/" +NOTICE_PAGE = "www.aiub.edu/category/notices" WEBSITE_URL = None # DO NOT CHANGE # XPath information for AIUB Notice page @@ -42,6 +44,10 @@ DB_NAME = "aiub_notices.db" DB_TABLE_NAME = "notices" +# RSS feed information +RSS_FEED_FILE = "rss.xml" +DEFAULT_TIME = "00:00:00" + # Script version SCRIPT_VERSION = "3.0" SCRIPT_URL = "https://raw.githubusercontent.com/origamiofficial/aiub-notice-checker/main/main.py" @@ -289,8 +295,47 @@ def send_telegram_message(message): ) send_telegram_message(message) +# Generate RSS feed +def generate_rss_feed(): + c.execute(f"SELECT title, description, link, day, month, year FROM {DB_TABLE_NAME} ORDER BY year DESC, month DESC, day DESC") + notices = c.fetchall() + # Root element + rss = ET.Element("rss", version="2.0") + channel = ET.SubElement(rss, "channel") + # Channel elements + ET.SubElement(channel, "title").text = "AIUB Notices" + ET.SubElement(channel, "link").text = f"https://{NOTICE_PAGE}" + ET.SubElement(channel, "description").text = "Latest notices from AIUB." + # Add notices to RSS feed + for notice in notices: + title, description, link, day, month_name, year = notice + # Extract month as a number (assuming month names are stored as strings) + month_number = datetime.datetime.strptime(month_name, "%B").month + # Generate RFC-822 date-time format with default time + pub_date = datetime.datetime(year=int(year), month=month_number, day=int(day), hour=int(DEFAULT_TIME.split(":")[0]), minute=int(DEFAULT_TIME.split(":")[1]), second=int(DEFAULT_TIME.split(":")[2])).strftime("%a, %d %b %Y %H:%M:%S GMT") + item = ET.SubElement(channel, "item") + ET.SubElement(item, "title").text = title + ET.SubElement(item, "description").text = description + ET.SubElement(item, "link").text = f"https://www.aiub.edu{link}" + ET.SubElement(item, "pubDate").text = pub_date + # Add guid element + guid = ET.SubElement(item, "guid") + guid.text = f"https://www.aiub.edu{link}" + # Add atom:link with rel="self" + self_link = ET.SubElement(channel, "{http://www.w3.org/2005/Atom}link") + self_link.set("rel", "self") + self_link.set("type", "application/rss+xml") + self_link.set("href", "https://raw.githubusercontent.com/origamiofficial/TestLab/main/rss.xml") + # Write to file + tree = ET.ElementTree(rss) + tree.write(RSS_FEED_FILE, encoding="UTF-8", xml_declaration=True, method="xml") + print(f"RSS feed generated at {RSS_FEED_FILE}") + +# Generate RSS feed after processing notices +generate_rss_feed() + # Close database connection conn.commit() conn.close() -print("Script Completed.") \ No newline at end of file +print("Script Completed.")