feedgenerator

A simple tool to create various feeds
git clone https://git.ortlepp.eu/feedgenerator.git/
Log | Files | Refs | README | LICENSE

commit a981975b0d8a0af5b86e23bf944f2140ddcf825e
parent 32e83542ecbf7434429b4c9aaa2f5a45b7c52e0f
Author: Thorsten Ortlepp <post@ortlepp.eu>
Date:   Wed, 30 Mar 2022 14:04:35 +0200

Externalized some configuration options

Diffstat:
MREADME | 2+-
M__main__.py | 6+++---
Mcommon.py | 47+++++++++++++++++++++++++++++++++++++++++++++++
Mdeutschlandfunk.py | 2+-
Afeedgenerator.ini | 14++++++++++++++
Mheise.py | 4++--
6 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/README b/README @@ -23,7 +23,7 @@ Building & Distribution ----------------------- I use zipapp to run feedgenerator on my server. Building the app is easy: -pip3 install -r feedgenerator/requirements.txt --target feedgenerator/ +pip3 install --upgrade -r feedgenerator/requirements.txt --target feedgenerator/ python3 -m zipapp feedgenerator/ Executing feedgenerator after building: diff --git a/__main__.py b/__main__.py @@ -8,13 +8,13 @@ returncodes = [] config = Config() am_feed = AntenneMuensterFeed() -returncodes.append(am_feed.create_feed(config.get_workdir() + "/antenne_muenster.xml", 20)) +returncodes.append(am_feed.create_feed(config.get_antennemuenster_file(), config.get_antennemuenster_items())) dlf_feed = DeutschlandfunkFeed() -returncodes.append(dlf_feed.create_feed(config.get_workdir() + "/deutschlandfunk.xml", 25)) +returncodes.append(dlf_feed.create_feed(config.get_deutschlandfunk_file(), config.get_deutschlandfunk_items())) heise_feed = HeiseFeed() -returncodes.append(heise_feed.create_feed(config.get_workdir() + "/heise.xml", 20)) +returncodes.append(heise_feed.create_feed(config.get_heise_file(), config.get_heise_items())) if False in returncodes: sys.exit(1) diff --git a/common.py b/common.py @@ -3,6 +3,7 @@ import hashlib import html import pathlib +import configparser try: import zoneinfo @@ -10,14 +11,24 @@ except ImportError: from backports import zoneinfo +# This class is a singleton, see https://en.wikipedia.org/wiki/Singleton_pattern#Python class Config: + __INSTANCE = None __WORK_DIR = "" + __CONFIG = configparser.ConfigParser() + + def __new__(self, *args): + if self.__INSTANCE is None: + self.__INSTANCE = object.__new__(self, *args) + return self.__INSTANCE + def __init__(self): self.__WORK_DIR = str(pathlib.Path(__file__).parent.resolve()) if self.__WORK_DIR.endswith(".pyz"): self.__WORK_DIR = str(pathlib.Path(__file__).parent.resolve().parent) + self.__CONFIG.read(self.get_workdir() + "/feedgenerator.ini", encoding="utf-8") def get_workdir(self): return self.__WORK_DIR @@ -25,6 +36,42 @@ class Config: def get_database(self): return self.__WORK_DIR + "/feedgenerator.sqlite" + def get_antennemuenster_filename(self): + return self.__CONFIG.get("AntenneMuensterFeed", "filename", fallback="antenne_muenster.xml") + + def get_antennemuenster_file(self): + return self.get_workdir() + "/" + self.get_antennemuenster_filename() + + def get_antennemuenster_items(self): + return self.__CONFIG.getint("AntenneMuensterFeed", "items", fallback=10) + + def get_deutschlandfunk_filename(self): + return self.__CONFIG.get("DeutschlandfunkFeed", "filename", fallback="deutschlandfunk.xml") + + def get_deutschlandfunk_file(self): + return self.get_workdir() + "/" + self.get_deutschlandfunk_filename() + + def get_deutschlandfunk_items(self): + return self.__CONFIG.getint("DeutschlandfunkFeed", "items", fallback=10) + + def get_deutschlandfunk_ignoretopics(self): + return self.__CONFIG.get("DeutschlandfunkFeed", "ignore_topics", fallback="").split("|") + + def get_heise_filename(self): + return self.__CONFIG.get("HeiseFeed", "filename", fallback="heise.xml") + + def get_heise_file(self): + return self.get_workdir() + "/" + self.get_heise_filename() + + def get_heise_items(self): + return self.__CONFIG.getint("HeiseFeed", "items", fallback=10) + + def get_heise_ignoretitle(self): + return self.__CONFIG.get("HeiseFeed", "ignore_title", fallback="").split("|") + + def get_heise_ignoreurl(self): + return self.__CONFIG.get("HeiseFeed", "ignore_url", fallback="").split("|") + class FeedItem: diff --git a/deutschlandfunk.py b/deutschlandfunk.py @@ -24,7 +24,7 @@ class DeutschlandfunkFeed: SQLITE_SQL_CLEAN = "DELETE FROM deutschlandfunk WHERE created < ?" SQLITE_SQL_GET = "SELECT title, content, link, created FROM deutschlandfunk WHERE export = 1 ORDER BY created DESC" - UNWANTED_TOPICS = ["Wetter", "Fußball-Bundesliga", "Fußball", "Fußball Europa League", "DFB-Pokal", "Lottozahlen", "Lotto", "Bundesliga", "American Football", "Formel 1", "Champions League", "Tennis"] + UNWANTED_TOPICS = Config().get_deutschlandfunk_ignoretopics() def __read_article_content(self, link): diff --git a/feedgenerator.ini b/feedgenerator.ini @@ -0,0 +1,14 @@ +[AntenneMuensterFeed] +filename = antenne_muenster.xml +items = 10 + +[DeutschlandfunkFeed] +filename = deutschlandfunk.xml +items = 10 +ignore_topics = Wetter|Lotto|Lottozahlen + +[HeiseFeed] +filename = heise.xml +items = 10 +ignore_title = Anzeige:|heise-Angebot +ignore_url = www.techstage.de diff --git a/heise.py b/heise.py @@ -24,8 +24,8 @@ class HeiseFeed: SQLITE_SQL_CLEAN = "DELETE FROM heise WHERE created < ?" SQLITE_SQL_GET = "SELECT title, content, link, created FROM heise WHERE export = 1 ORDER BY created DESC" - IGNORE_TITLE = ["heise-Angebot", "Die Produktwerker", "Anzeige:", "c't uplink"] - IGNORE_URL = ["www.techstage.de"] + IGNORE_TITLE = Config().get_heise_ignoretitle() + IGNORE_URL = Config().get_heise_ignoreurl() def __is_on_ignore_list(self, string, list):