Skip to content

Commit

Permalink
added workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
clientinfo committed Jul 13, 2024
1 parent 131ba28 commit 3fb1475
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 16 deletions.
33 changes: 18 additions & 15 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ name: Pylint
on: [push]

jobs:
build:
runs-on: ubuntu-latest
build-windows:
runs-on: windows-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.9"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint beautifulsoup4 requests
- name: Analysing the code with pylint
run: |
Get-ChildItem -Path . -Filter *.py -Recurse | ForEach-Object { pylint $_.FullName --max-line-length=140 --disable=R0903 }
2 changes: 1 addition & 1 deletion .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions last_sent_changelog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "2.0.2.7",
"date": "2024-07-03",
"changelog": [
"Behoben: Das Problem der Verz\u00f6gerung beim Videowiedergabeergebnis der Video-Passthrough-Funktion des Konvertermoduls wurde behoben.",
"Behoben: Das Problem, dass die KI-Modulkonvertierung beim Herunterladen und Ausf\u00fchren der Umgebung h\u00e4ngenbleibt.",
"Behoben: Einige Probleme mit der Anzeige der Benutzeroberfl\u00e4che."
]
}
103 changes: 103 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""
Module: changelog_watcher
This module implements the ChangelogWatcher class, which monitors a specified URL for updates,
parses the changelog, and sends notifications to a Discord webhook.
"""

import time
from modules.console_color import Color # Import the Color class for color formatting
from modules.changelog_parser import ChangelogParser
from modules.discord_notifier import DiscordNotifier


class ChangelogWatcher:
"""
ChangelogWatcher class monitors a specified URL for updates,
parses the changelog, and sends notifications to a Discord webhook.
"""

def __init__(self, arg_url, arg_webhook_url):
"""
Initializes the ChangelogWatcher instance with the provided URL and Discord webhook URL.
Args:
arg_url (str): The URL to fetch the changelog from.
arg_webhook_url (str): The URL of the Discord webhook to send notifications to.
"""
self.url = arg_url
self.webhook_url = arg_webhook_url
self.discord_notifier = DiscordNotifier(arg_webhook_url)
self.changelog_parser = ChangelogParser()
self.last_sent_changelog = self.changelog_parser.load_last_sent_changelog()

def fetch_changelog(self):
"""
Fetches the changelog HTML content from the specified URL.
Returns:
str or None: The fetched HTML content if successful, None if an error occurs.
"""
return self.changelog_parser.fetch_changelog(self.url)

def parse_changelog(self, arg_html_content):
"""
Parses the HTML content of the changelog to extract version, date, and changelog items.
Args:
arg_html_content (str): The HTML content of the changelog to parse.
Returns:
dict or None: A dictionary containing version, date, and changelog items
if parsing is successful, None if an error occurs.
"""
return self.changelog_parser.parse_changelog(arg_html_content)

def send_to_discord_webhook(self, arg_software_name, arg_changelog_data):
"""
Sends the parsed changelog data to the Discord webhook.
Args:
arg_software_name (str): The name of the software for the update.
arg_changelog_data (dict): The parsed changelog data to send.
"""
self.discord_notifier.send_to_discord_webhook(arg_software_name, arg_changelog_data)

def run(self):
"""
Runs the ChangelogWatcher to continuously monitor the changelog, parse updates,
and send notifications to Discord.
"""
while True:
html_content = self.fetch_changelog()
if html_content:
changelog_data = self.parse_changelog(html_content)

if changelog_data:
if changelog_data != self.last_sent_changelog:
self.send_to_discord_webhook("UniFab", changelog_data)
self.last_sent_changelog = changelog_data
self.changelog_parser.save_last_sent_changelog(changelog_data) # Save immediately
else:
self.last_sent_changelog = changelog_data
self.changelog_parser.save_last_sent_changelog(changelog_data) # Save immediately
else:
print(Color.red("No valid Update data found."))
else:
print(Color.red("Failed to fetch Update data."))

time.sleep(20) # 20 seconds


if __name__ == "__main__":
URL = 'https://de.unifab.ai/unifab-new.htm' # Replace with the actual URL
WEBHOOK_URL = ('WEBHOOK URL HERE')
SOFTWARE_NAME = 'UniFab Update Notificator'
AUTHOR_NAME = 'clientinfo'

# Print software name in green and author name in blue
print(f"{Color.green('Software:')} {Color.blue(SOFTWARE_NAME)}")
print(f"{Color.green('Creator:')} {Color.blue(AUTHOR_NAME)}")

watcher = ChangelogWatcher(URL, WEBHOOK_URL)
watcher.run()
170 changes: 170 additions & 0 deletions modules/changelog_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"""
Module: changelog_parser
This module provides functionality to fetch, parse, save, and load changelog data
from HTML and JSON files.
"""

import json
import os
from datetime import datetime
import requests
from bs4 import BeautifulSoup
from modules.console_color import Color # Import the Color class from console_color.py


class ChangelogParser:
"""
A class to fetch, parse, save, and load changelog data from HTML and JSON files.
Methods:
fetch_changelog(arg_url):
Fetches HTML content from a given URL.
parse_changelog(arg_html_content):
Parses HTML content to extract changelog information.
save_last_sent_changelog(arg_changelog_data):
Saves parsed changelog data to a JSON file.
load_last_sent_changelog():
Loads previously saved changelog data from a JSON file.
"""

@staticmethod
def fetch_changelog(arg_url):
"""
Fetches HTML content from a given URL using requests.
Args:
arg_url (str): The URL to fetch HTML content from.
Returns:
str: The HTML content fetched from the URL, or None if there was an error.
"""
try:
response = requests.get(arg_url, timeout=10) # Added timeout argument
response.raise_for_status() # Raise an error for bad status codes
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Fetching URL:')} {arg_url}")
return response.text
except requests.exceptions.RequestException as e:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Error fetching the URL:')} {e}")
return None

@staticmethod
def parse_changelog(arg_html_content):
"""
Parses HTML content to extract changelog information using BeautifulSoup.
Args:
arg_html_content (str): The HTML content to parse.
Returns:
dict or None: A dictionary containing parsed changelog data (version, date, changelog),
or None if parsing fails or no valid changelog data found.
"""
if not arg_html_content:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Empty HTML content received.')}")
return None

try:
soup = BeautifulSoup(arg_html_content, 'html.parser')
container = soup.find('div', class_='bg-white b-rd-8 pl40 pr40 pb32 changelog-content')
if not container:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Container div not found.')}")
return None

section = container.find('p', class_='whatsnew')
if not section:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('No changelog section found within the container.')}")
return None

version = section.find('strong').text.strip() if section.find('strong') else 'N/A'
date = section.find('span').text.strip() if section.find('span') else 'N/A'
changelog_items = section.find_all('li')
changelog = [item.text.strip() for item in changelog_items]
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.green('Changelog data parsed successfully.')}")
return {
'version': version,
'date': date,
'changelog': changelog
}
except ValueError as ve:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('ValueError parsing HTML content:')} {ve}")
return None
except AttributeError as ae:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('AttributeError parsing HTML content:')} {ae}")
return None
except Exception as e:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Error parsing HTML content:')} {e}")
return None

@staticmethod
def save_last_sent_changelog(arg_changelog_data):
"""
Saves parsed changelog data to a JSON file named 'last_sent_changelog.json'.
Args:
arg_changelog_data (dict): The changelog data to save.
"""
try:
with open('last_sent_changelog.json', 'w', encoding='utf-8') as file: # Specified encoding
json.dump(arg_changelog_data, file, indent=4) # Update the file with indent for readability
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.green('Changelog data saved to file.')}")
except FileNotFoundError as fnfe:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('FileNotFoundError saving Changelog data:')} {fnfe}")
except IOError as ioe:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('IOError saving Changelog data:')} {ioe}")
except Exception as e:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Error saving Changelog data:')} {e}")

@staticmethod
def load_last_sent_changelog():
"""
Loads previously saved changelog data from 'last_sent_changelog.json'.
Returns:
dict or None: A dictionary containing the last sent changelog data,
or None if the file doesn't exist or cannot be loaded.
"""
try:
if os.path.exists('last_sent_changelog.json'):
with open('last_sent_changelog.json', 'r', encoding='utf-8') as file: # Specified encoding
last_sent_changelog = json.load(file)
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.green('Last sent changelog data loaded from file.')}")
return last_sent_changelog
else:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('No saved changelog data found. Creating a new file.')}")
with open('last_sent_changelog.json', 'w', encoding='utf-8') as file: # Specified encoding
json.dump(None, file) # Create an empty JSON object
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.green('Empty file created for the last sent changelog data.')}")
return None
except FileNotFoundError as fnfe:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('FileNotFoundError loading last sent changelog data:')} {fnfe}")
except IOError as ioe:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('IOError loading last sent changelog data:')} {ioe}")
except json.JSONDecodeError as je:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('JSONDecodeError loading last sent changelog data:')} {je}")
except Exception as e:
print(f"{Color.blue('[' + datetime.now().strftime('%H:%M:%S') + ']')}: "
f"{Color.red('Error loading last sent changelog data:')} {e}")
return None
Loading

0 comments on commit 3fb1475

Please sign in to comment.