Skip to content

Commit

Permalink
Add RSS Feed generate function
Browse files Browse the repository at this point in the history
  • Loading branch information
origamiofficial committed Sep 4, 2024
1 parent 218eda1 commit e633cf0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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) <br />
[![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) <br />
[![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.
Expand Down
49 changes: 47 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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"]
TELEGRAM_ADMIN_CHAT_ID = os.environ.get("TELEGRAM_ADMIN_CHAT_ID")
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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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.")
print("Script Completed.")

0 comments on commit e633cf0

Please sign in to comment.