Get Ip for koji.koozali.org initially and use it from then
This commit is contained in:
		| @@ -6,12 +6,45 @@ import logging | ||||
| import os | ||||
| import requests | ||||
| from bs4 import BeautifulSoup | ||||
| import socket | ||||
| import sys | ||||
|  | ||||
| def join_lines_with_newline(lines): | ||||
|     return "\n".join(lines) | ||||
|  | ||||
| def extract_changelog_top(koji_link): | ||||
|     response = requests.get(koji_link) | ||||
| def get_ip_address(domain, retries=10, delay=1): | ||||
|     """ | ||||
|     Resolves the IP address of a domain, retrying up to `retries` times if it fails. | ||||
|      | ||||
|     Args: | ||||
|         domain (str): The domain name to resolve. | ||||
|         retries (int): Number of retry attempts (default is 10). | ||||
|         delay (int): Delay between retries in seconds (default is 1 second). | ||||
|      | ||||
|     Returns: | ||||
|         str: The IP address if resolved successfully. | ||||
|      | ||||
|     Raises: | ||||
|         RuntimeError: If unable to resolve the domain after all attempts. | ||||
|     """ | ||||
|     for attempt in range(1, retries + 1): | ||||
|         try: | ||||
|             ip_address = socket.gethostbyname(domain) | ||||
|             logging.info(f"Successfully resolved {domain} to {ip_address}") | ||||
|             return ip_address | ||||
|         except socket.gaierror: | ||||
|             logging.warning(f"Attempt {attempt} failed. Retrying...") | ||||
|             time.sleep(delay) | ||||
|      | ||||
|     raise RuntimeError(f"Unable to resolve domain '{domain}' after {retries} attempts.") | ||||
|  | ||||
| def extract_changelog_top(koji_link, host_header): | ||||
|     try: | ||||
|         response = requests.get(koji_link, headers={'Host': host_header}) | ||||
|     except Exception as e: | ||||
|         logging.error(f"Koji link failed: {str(e)}") | ||||
|         return [] | ||||
|          | ||||
|     soup = BeautifulSoup(response.text, 'html.parser') | ||||
|     changelog_td = soup.find('td', class_='changelog') | ||||
|  | ||||
| @@ -24,7 +57,7 @@ def extract_changelog_top(koji_link): | ||||
|         for line in lines: | ||||
|             stripped_line = line.strip() | ||||
|             if not stripped_line: | ||||
|                 if result:  # Stop at the first blank line after content | ||||
|                 if result: | ||||
|                     break | ||||
|                 continue | ||||
|             if stripped_line not in seen: | ||||
| @@ -36,16 +69,12 @@ def extract_changelog_top(koji_link): | ||||
|     else: | ||||
|         return [] | ||||
|  | ||||
| # 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" | ||||
| log_file = "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 | ||||
| @@ -60,7 +89,6 @@ def setup_database(): | ||||
|     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() | ||||
| @@ -69,7 +97,6 @@ def has_build_been_sent(build_id): | ||||
|     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() | ||||
| @@ -77,8 +104,7 @@ def mark_build_as_sent(build_id): | ||||
|     conn.commit() | ||||
|     conn.close() | ||||
|  | ||||
| # Function to send message to Rocket.Chat | ||||
| def send_to_rocket_chat(build_title, build_link, build_id,changelog): | ||||
| def send_to_rocket_chat(build_title, build_link, build_id, changelog): | ||||
|     payload = { | ||||
|         "alias": "Koji", | ||||
|         "text": f"Build Notification - ID: {build_id}\n{changelog}", | ||||
| @@ -96,7 +122,6 @@ def send_to_rocket_chat(build_title, build_link, build_id,changelog): | ||||
|     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", | ||||
| @@ -108,40 +133,45 @@ def send_startup_message(): | ||||
|     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(): | ||||
| def fetch_koji_feed(koji_rss_url, host_header): | ||||
|     try: | ||||
|         feed = feedparser.parse(KOJI_RSS_URL) | ||||
|         response = requests.get(koji_rss_url, headers={'Host': host_header}) | ||||
|         feed = feedparser.parse(response.content) | ||||
|         return feed.entries | ||||
|     except Exception as e: | ||||
|         exc_type, exc_value, exc_traceback = sys.exc_info() | ||||
|         logging.error(f"RSS request to koji failed: {exc_type.__name__}: {str(e)}") | ||||
|         logging.debug(f"Exception traceback: {''.join(traceback.format_tb(exc_traceback))}") | ||||
|         logging.error(f"RSS request to koji failed: {str(e)}") | ||||
|         return [] | ||||
|  | ||||
| # Main polling loop | ||||
| def main(): | ||||
|     setup_database()  # Initialize the database | ||||
|     send_startup_message()  # Send startup message | ||||
|     setup_database() | ||||
|     send_startup_message() | ||||
|  | ||||
|     original_domain = "koji.koozali.org" | ||||
|     try: | ||||
|         koji_ip = get_ip_address(original_domain) | ||||
|     except RuntimeError as e: | ||||
|         logging.error(str(e)) | ||||
|         sys.exit(1) | ||||
|  | ||||
|     # Create IP-based URL and preserve original domain for headers | ||||
|     koji_rss_url = f"http://{koji_ip}/koji/recentbuilds?feed=rss" | ||||
|     logging.info(f"Using Koji RSS URL: {koji_rss_url}") | ||||
|  | ||||
|     while True: | ||||
|         entries = fetch_koji_feed() | ||||
|         entries = fetch_koji_feed(koji_rss_url, original_domain) | ||||
|          | ||||
|         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 | ||||
|             changelog = extract_changelog_top(entry.link) | ||||
| #            print(f"{entry.link}\n{changelog}") | ||||
| #            quit(1) | ||||
|             original_link = entry.link | ||||
|             build_link = original_link.replace(original_domain, koji_ip) | ||||
|             build_id = build_title.split(':')[1].split(',')[0].strip() | ||||
|             changelog = extract_changelog_top(build_link, original_domain) | ||||
|  | ||||
|             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,changelog) | ||||
|                 mark_build_as_sent(build_id)  # Mark the build ID as sent | ||||
|             if not has_build_been_sent(build_id): | ||||
|                 send_to_rocket_chat(build_title, build_link, build_id, changelog) | ||||
|                 mark_build_as_sent(build_id) | ||||
|          | ||||
|         # Wait for 1 minute before polling again | ||||
|         time.sleep(60) | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
|     main() | ||||
		Reference in New Issue
	
	Block a user