import time import feedparser import requests import sqlite3 import logging import os # Koji RSS feed URL KOJI_RSS_URL = "http://koji.koozali.org/koji/recentbuilds?feed=rss" # Updated Rocket.Chat webhook URL ROCKET_CHAT_URL = "https://chat.koozali.org/hooks/66d24441effca216c2ca309f/KJLaNwc5vyHwqz5MhRDpBkKWnQuAfsCX3xZMHxPhpuqmFgBn" # Set up logging log_file = "/var/log/Koji2Rocket.log" logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # Ensure the log directory exists os.makedirs(os.path.dirname(log_file), exist_ok=True) # Database setup def setup_database(): conn = sqlite3.connect('sent_builds.db') cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS sent_builds ( id TEXT PRIMARY KEY ) ''') conn.commit() conn.close() # Function to check if a build ID has been sent def has_build_been_sent(build_id): conn = sqlite3.connect('sent_builds.db') cursor = conn.cursor() cursor.execute('SELECT * FROM sent_builds WHERE id = ?', (build_id,)) exists = cursor.fetchone() is not None conn.close() return exists # Function to mark a build ID as sent def mark_build_as_sent(build_id): conn = sqlite3.connect('sent_builds.db') cursor = conn.cursor() cursor.execute('INSERT OR IGNORE INTO sent_builds (id) VALUES (?)', (build_id,)) conn.commit() conn.close() # Function to send message to Rocket.Chat def send_to_rocket_chat(build_title, build_link, build_id): payload = { "alias": "Koji", "text": f"Build Notification - ID: {build_id}", "attachments": [ { "title": build_title, "title_link": build_link, "color": "#764FA5" } ] } response = requests.post(ROCKET_CHAT_URL, json=payload) if response.status_code == 200: logging.info(f"Build notification sent successfully: {build_title} (ID: {build_id})") else: logging.error(f"Failed to send build notification: {response.status_code} - {response.text}") # Function to send startup message to Rocket.Chat def send_startup_message(): payload = { "alias": "Koji", "text": "Koji to Rocket.Chat integration started successfully.", } response = requests.post(ROCKET_CHAT_URL, json=payload) if response.status_code == 200: logging.info("Startup message sent successfully to Rocket.Chat.") else: logging.error(f"Failed to send startup message: {response.status_code} - {response.text}") # Function to fetch and parse the Koji RSS feed def fetch_koji_feed(): feed = feedparser.parse(KOJI_RSS_URL) return feed.entries # Main polling loop def main(): setup_database() # Initialize the database send_startup_message() # Send startup message while True: entries = fetch_koji_feed() for entry in entries: build_title = entry.title.strip() build_link = entry.link # Extracting the build ID from the title build_id = build_title.split(':')[1].split(',')[0].strip() # Extract the relevant part from the title if not has_build_been_sent(build_id): # Check if the build has been sent send_to_rocket_chat(build_title, build_link, build_id) mark_build_as_sent(build_id) # Mark the build ID as sent # Wait for 1 minute before polling again time.sleep(60) if __name__ == "__main__": main()