First RSS hook with Python

This commit is contained in:
DarkFeather 2024-08-20 00:34:25 -05:00
parent e4ac1b6517
commit 4105e9bac1
Signed by: DarkFeather
GPG Key ID: 1CC1E3F4ED06F296
3 changed files with 64 additions and 0 deletions

View File

@ -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

21
Python/first_rss_entry.py Normal file
View File

@ -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('<h2>'+appliedarr['title']+'</h2>'
+'<p>'+appliedarr['summary']
+ ' <br/><br/><b>Updated: ' + appliedarr['updated'] + '</b> '
+' <a href="'+appliedarr['id']
+ '">Read More</a></p><hr style="margin-top: 50px;" />')
sys.exit(0)
except Exception as e:
print(e)
sys.exit(1)

View File

@ -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('<h2>'+appliedarr['title']+'</h2>'
+'<p>'+appliedarr['summary']
+ ' Updated: ' + appliedarr['updated']
+' <a href="'+appliedarr['id']
+ '">Read More</a></p><hr style="margin-top: 50px;" />')
sys.exit(0)
except Exception as e:
print(e)
sys.exit(1)
else:
print('Request failed.')
sys.exit(2)