aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2022-02-06 16:55:58 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2022-02-06 16:55:58 +0100
commit1b3996f2faa77e97e4c6bfc88067a2b484611c85 (patch)
tree0083aff79351b1e9daf66ec3498e13983249f818
parentf49913549a72edbfc99e1745ac6ac09e9f46c3ed (diff)
downloadatom_to_gitea-1b3996f2faa77e97e4c6bfc88067a2b484611c85.tar.gz
atom
-rw-r--r--rss_to_gitea/atom.py80
-rw-r--r--rss_to_gitea/main.py32
-rwxr-xr-xsetup.py3
3 files changed, 111 insertions, 4 deletions
diff --git a/rss_to_gitea/atom.py b/rss_to_gitea/atom.py
new file mode 100644
index 0000000..a19a969
--- /dev/null
+++ b/rss_to_gitea/atom.py
@@ -0,0 +1,80 @@
+import xml.etree.ElementTree as ET
+import requests
+import json
+
+from datetime import datetime
+
+def containts_any_of(_str: str, _list: list[str]):
+ return True in map(lambda a: a in _str, _list)
+
+def date_compare(date1: str, date2: str):
+ d1 = parse_date(date1)
+ d2 = parse_date(date2)
+ return d1 > d2
+
+def parse_date(date: str):
+ '''
+ Parse RFC 3339 Date
+ '''
+ return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
+
+class FeedException(Exception):
+ pass
+
+class AtomFeed:
+ def __init__(self, _url):
+ self.url = _url
+ xml = self._get_feed()
+ entries = self._parse(xml)
+ self.entries = entries
+ pass
+
+ def _get_feed(self):
+ resp = requests.get(self.url)
+ if resp.status_code >= 400:
+ raise FeedException(f'Failed to get Feed: {resp.status_code}')
+
+ return resp.content
+
+ def get_latest(self, _exclude: list[str] = [], _include: list[str] = []):
+ '''
+ _include is ignored if empty. NOT REGEX!!
+ '''
+ latest ={ }
+
+ for entry in self.entries:
+ if len(latest) <= 0 or date_compare(entry['updated'], latest['updated']):
+ if containts_any_of(entry['title'], _exclude):
+ continue
+ if not (len(_include) == 0 or containts_any_of(entry['title'], _include)):
+ continue
+
+ latest = entry
+
+ return latest
+
+
+ def _parse(self, xml):
+ tree = ET.fromstring(xml)
+
+ entries = []
+
+ for entry in tree:
+ if not entry.tag.endswith('entry'):
+ continue
+
+ data = {}
+
+ for att in entry:
+ if att.tag.endswith('updated'):
+ data['updated'] = att.text
+
+ if att.tag.endswith('title'):
+ data['title'] = att.text
+
+ if att.tag.endswith('link'):
+ data['link'] = att.attrib['href']
+
+ entries.append(data)
+
+ return entries
diff --git a/rss_to_gitea/main.py b/rss_to_gitea/main.py
index 93c16de..f4425a2 100644
--- a/rss_to_gitea/main.py
+++ b/rss_to_gitea/main.py
@@ -1,8 +1,34 @@
import xml
import sys
+import yaml
+import os
+import sys
+
from .gitea import GiteaAPI
+from .atom import AtomFeed
+
+
+feeds = [
+ {
+ 'name':'Gitea',
+ 'url':'https://github.com/go-gitea/gitea/releases.atom',
+ 'exclude':['dev', 'rc'],
+ 'assign':''
+ }
+]
+
+def load_yaml(_file: str):
+ required = ['feeds', 'token', 'url']
+ config = {}
+
+ with open(_file, 'r') as f:
+ config = yaml.load(f.read(), Loader=yaml.FullLoader)
+
+ return config
def main():
- token = sys.argv[1]
- api = GiteaAPI("https://gitea.my.cum.re", token)
- api.searchIssue('infra', 'ansible', '', 'update')
+ #token = sys.argv[1]
+ #api = GiteaAPI("https://gitea.my.cum.re", token)
+ #api.searchIssue('infra', 'ansible', '', 'update')
+ feed = AtomFeed(feeds[0]['url'])
+ print(feed.get_latest([]))
diff --git a/setup.py b/setup.py
index 1d7400c..689394b 100755
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,8 @@ setup(
'console_scripts': ['rsstogitea=rss_to_gitea.main:main']
},
install_requires=[
- "requests>=2.25.1"
+ "requests>=2.25.1",
+ "pyyaml"
],
license='All rights reserved',
long_description=open('README.md').read(),