From 4105e9bac1edc4a71e4099fbba890ad0af475ec2 Mon Sep 17 00:00:00 2001 From: DarkFeather Date: Tue, 20 Aug 2024 00:34:25 -0500 Subject: [PATCH] First RSS hook with Python --- Hooks/scripts.d/generate-rss-snippet | 12 +++++++++++ Python/first_rss_entry.py | 21 +++++++++++++++++++ Python/first_rss_entry_url.py | 31 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100755 Hooks/scripts.d/generate-rss-snippet create mode 100644 Python/first_rss_entry.py create mode 100644 Python/first_rss_entry_url.py diff --git a/Hooks/scripts.d/generate-rss-snippet b/Hooks/scripts.d/generate-rss-snippet new file mode 100755 index 0000000..960aa31 --- /dev/null +++ b/Hooks/scripts.d/generate-rss-snippet @@ -0,0 +1,12 @@ +#!/bin/bash + +firstRSSEntryPy=$(dirname "$0")/../../Python/first_rss_entry.py +if [ -d /var/lib/gitea/custom/public/assets/ ] && [ -d ./rss/ ]; then + sudo mkdir -p /var/lib/gitea/custom/public/assets/rss-snippets + for feed in `find ./rss/ -type f -name '*.xml'`; do + feedshort="$(basename "$feed" | sed 's/.xml$//')" + python3 "$firstRSSEntryPy" "$feed" > /tmp/${feedshort} + sudo mv /tmp/${feedshort} /var/lib/gitea/custom/public/assets/rss-snippets + done + sudo chown -R gitea: /var/lib/gitea/custom/public/assets/rss-snippets +fi diff --git a/Python/first_rss_entry.py b/Python/first_rss_entry.py new file mode 100644 index 0000000..0db1a9a --- /dev/null +++ b/Python/first_rss_entry.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import sys +import xml.etree.ElementTree as ET + +try: + tree = ET.parse(sys.argv[1]) + firstentry = tree.find('{http://www.w3.org/2005/Atom}entry') + appliedarr = {} + for child in firstentry: + shortattr = child.tag.replace('{http://www.w3.org/2005/Atom}','') + appliedarr.update({shortattr : child.text}) + print('

'+appliedarr['title']+'

' + +'

'+appliedarr['summary'] + + '

Updated: ' + appliedarr['updated'] + ' ' + +' Read More


') + sys.exit(0) +except Exception as e: + print(e) + sys.exit(1) diff --git a/Python/first_rss_entry_url.py b/Python/first_rss_entry_url.py new file mode 100644 index 0000000..997dcfd --- /dev/null +++ b/Python/first_rss_entry_url.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys +import urllib3 +import xml.etree.ElementTree as ET + +http = urllib3.PoolManager() +response = http.request('GET',sys.argv[1]) + +if response.status == 200: + + try: + tree = ET.fromstring(response.data.decode('utf-8')) + firstentry = tree.find('{http://www.w3.org/2005/Atom}entry') + appliedarr = {} + for child in firstentry: + shortattr = child.tag.replace('{http://www.w3.org/2005/Atom}','') + appliedarr.update({shortattr : child.text}) + print('

'+appliedarr['title']+'

' + +'

'+appliedarr['summary'] + + ' Updated: ' + appliedarr['updated'] + +' Read More


') + sys.exit(0) + except Exception as e: + print(e) + sys.exit(1) + +else: + print('Request failed.') + sys.exit(2)