From 222bf53b25778dbabe6df169e1f92c698f412a24 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 14 Jan 2023 21:49:58 +0200 Subject: [PATCH] (Added) TOML configuration file. (Updated) Paths, name, years. --- CHANGELOG.org | 4 +++ README.org | 27 ++++++++++++++-- README.rst | 32 +++++++++++++++++-- pyproject.toml | 6 ++-- src/config.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/db.py | 2 +- src/main.py | 40 ++++++++++------------- src/notify.py | 2 +- src/scraper.py | 2 +- 9 files changed, 170 insertions(+), 32 deletions(-) create mode 100644 src/config.py diff --git a/CHANGELOG.org b/CHANGELOG.org index 58f7dd5..ca2b1e1 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -25,3 +25,7 @@ Added img/ folder. ** 0.6.0 <2022-08-11> Updated the cli interface +** 1.0.1 <2023-01-14> + Added TOML configuration file. + Updated paths of files and folders. + Updated my name to my fullname and release years. diff --git a/README.org b/README.org index d6a474a..9313e63 100644 --- a/README.org +++ b/README.org @@ -1,10 +1,33 @@ * Anitube Simple Notification Anitube Simple Notification is a application made for getting notification when a content is updated on the web-site (anitube.in.ua). -*Not ready for using as applications.* + +* Usage +In application folder create file with name =config.toml=. If a value +is wrong, then a error will be shown. If there no value or a wrong value +then it will be default. + +Example of a config file for all options: +#+BEGIN_SRC toml + POSTERS = true + WAITING_PERIOD = 3600 + URLS = [ + "https://anitube.in.ua/4110-chainsaw-man.html", + "https://anitube.in.ua/4010-overlord-iv.html", + "https://anitube.in.ua/4097-mob-varyat-100-3-sezon.html", + "https://anitube.in.ua/4087-spy-x-family-part-2.html", + ] +#+END_SRC +The last comma of the urls list can be ommited. + +Run the program by one of the commands: +#+BEGIN_SRC shell + python3 main.py + python main.py +#+END_SRC * Author -Kostya Klochko (c) 2022 +Kostiantyn Klochko (c) 2022-2023 * Donation Monero: 8BCZr3LaciDZUwNUbC8M5gNZTtnPKoT9cMH95YcBoo2k8sg4qaxejYL4Qvp6V21ViqMHj5sHLiuRwhMYxHTVW1HUNAawV6c diff --git a/README.rst b/README.rst index c50fd03..83e6e56 100644 --- a/README.rst +++ b/README.rst @@ -3,12 +3,40 @@ Anitube Simple Notification Anitube Simple Notification is a application made for getting notification when a content is updated on the web-site (anitube.in.ua). -**Not ready for using as applications.** + +Usage +===== + +In application folder create file with name ``config.toml``. If a value +is wrong, then a error will be shown. If there no value or a wrong value +then it will be default. + +Example of a config file for all options: + +.. code:: toml + + POSTERS = true + WAITING_PERIOD = 3600 + URLS = [ + "https://anitube.in.ua/4110-chainsaw-man.html", + "https://anitube.in.ua/4010-overlord-iv.html", + "https://anitube.in.ua/4097-mob-varyat-100-3-sezon.html", + "https://anitube.in.ua/4087-spy-x-family-part-2.html", + ] + +The last comma of the urls list can be ommited. + +Run the program by one of the commands: + +.. code:: shell + + python3 main.py + python main.py Author ====== -Kostya Klochko (c) 2022 +Kostiantyn Klochko (c) 2022-2023 Donation ======== diff --git a/pyproject.toml b/pyproject.toml index 4d83227..ba5b04d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,9 +4,9 @@ build-backend = "setuptools.build_meta" [project] name = "anitube-simple-notification" -version = "0.5.2" +version = "1.0.0" authors = [ - { name="Kostya Klochko", email="kostya_klochko@ukr.net" }, + { name="Kostiantyn Klochko", email="kostya_klochko@ukr.net" }, ] description = "Getting notification from anitube.in.ua." readme = "README.rst" @@ -22,6 +22,8 @@ dependencies = [ "bs4", "notify-py", "rich", + "tomli", + "loguru" ] [project.urls] diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..5e9ae8d --- /dev/null +++ b/src/config.py @@ -0,0 +1,87 @@ +########################################################################## +# Copyright (C) 2022-2023 Kostiantyn Klochko # +# # +# This file is part of Anitube Simple Notification. # +# # +# Anitube Simple Notification is free software: you can redistribute # +# it and/or modify it under the terms of the GNU General Public # +# License as published by the Free Software Foundation, either version # +# 3 of the License, or (at your option) any later version. # +# # +# Anitube Simple Notification is distributed in the hope that it will # +# be useful, but WITHOUT ANY WARRANTY; without even the implied # +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See # +# the GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with Anitube Simple Notification. If not, see # +# . # +########################################################################## + +""" +This module has all for simplify work with the toml configuration file. +""" + +from rich.console import Console +import tomli +import os + +class Config: + @staticmethod + def config_validation(config: dict) -> bool: + """Return False if config is invalid. Empty values is valid.""" + validations = { + "POSTERS": lambda v: isinstance(v, bool), + "WAITING_PERIOD": lambda v: isinstance(v, int) and v >= 0, + "URLS": lambda l: isinstance(l, list) and len(l) == len(list(filter(str,l))), + } + return all(validations.get(k, lambda x: False)(v) for (k, v) in config.items()) + + @staticmethod + def config_validation_detail(config: dict) -> dict: + """Return a dict where key is a field and value is valid status.""" + validations = { + "POSTERS": lambda v: isinstance(v, bool), + "WAITING_PERIOD": lambda v: isinstance(v, int) and v >= 0, + "URLS": lambda l: isinstance(l, list) and + len(list(filter(lambda v: isinstance(v,str), l))) and + len(list(filter(lambda v: v.startswith("https://anitube.in.ua/"), l))) + #len(l) == len(list(filter(str,l))), + } + return {k:validations.get(k, lambda x: False)(v) for (k, v) in config.items()} + + @staticmethod + def config_validation_error(config: dict): + """Show error and remove invalid fields.""" + validation = Config.config_validation_detail(config) + if not validation.pop("POSTERS", True): + Console().print(f"[red][ERROR] Please, POSTER must be false or true.[/]") + del config["POSTERS"] + if not validation.pop("WAITING_PERIOD", True): + Console().print( + ''.join(["[red][ERROR] ", + "Please, WAITING_PERIOD must be an integer that greater", + "than zero or equals to.[/]"])) + del config["WAITING_PERIOD"] + if not validation.pop("URLS", True): + Console().print( + "".join(["[red][ERROR] Please, URLS must be a list of strings" + "which ones are page urls of anitube.in.ua.[/]"])) + del config["URLS"] + + @staticmethod + def get_config(config_path:str, console:Console) -> dict: + """ + Read the configuration file and return dict as configuration. + The configuration file must be in the application folder. + """ + config = {} + try: + with open(config_path, "rb") as file: + config = tomli.load(file) + except FileNotFoundError: + console.print(f"[red][ERROR] Please, create configuration file.[/]") + except tomli.TOMLDecodeError: + console.print(f"[red][ERROR] Please, check configuration file for correctness.[/]") + Config.config_validation_error(config) + return config diff --git a/src/db.py b/src/db.py index 0239e3a..21b2a44 100644 --- a/src/db.py +++ b/src/db.py @@ -1,5 +1,5 @@ ########################################################################## -# Copyright (C) 2022 Kostya Klochko # +# Copyright (C) 2022-2023 Kostiantyn Klochko # # # # This file is part of Anitube Simple Notification. # # # diff --git a/src/main.py b/src/main.py index 0335922..e0bcb58 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,5 @@ ########################################################################## -# Copyright (C) 2022 Kostya Klochko # +# Copyright (C) 2022-2023 Kostiantyn Klochko # # # # This file is part of Anitube Simple Notification. # # # @@ -20,43 +20,37 @@ from db import DataBase from scraper import Scraper from notify import Notification +from config import Config from rich.console import Console from rich.progress import track import time - -def get_urls(file_path): - """Read file that containes urls.""" - urls = [] - with open(file_path, 'r') as file: - for line in file: - if '\n' in line: - line = line.replace('\n', '') - urls.append(line) - return urls +import os def main(): # Const sections. + # Console initialising + console = Console() + BASE_DIR = os.path.dirname(__file__) + DB_PATH = os.path.join(BASE_DIR, 'user.db') + POSTERS_PATH = os.path.join(BASE_DIR, 'posters') + CONFIG_PATH = os.path.join(BASE_DIR, 'config.toml') + config = Config.get_config(CONFIG_PATH, console) + # Here you can change testing headers to yours. HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' } + # Default if empty. MESSAGE = "New episode." - URL_FILE = "urls" - WAITING_PERIOD = 60 # seconds - POSTERS = True # For use poster as icons of notifications + WAITING_PERIOD = config.pop('WAITING_PERIOD', 60) # seconds + POSTERS = config.pop('POSTERS', True) # for use poster as icons of notifications + urls = config.pop('URLS', []) # Initialising objects - scr = Scraper(HEADERS) - db = DataBase() - - # Reading the URL_FILE for making list - urls = get_urls(URL_FILE) - #print(f"{urls}") - - # Console initialising - console = Console() + scr = Scraper(HEADERS, POSTERS_PATH) + db = DataBase(DB_PATH) # Checks for new urls in file and add as current state # If one of page has updated then notifing. diff --git a/src/notify.py b/src/notify.py index 5335849..7c8242c 100644 --- a/src/notify.py +++ b/src/notify.py @@ -1,5 +1,5 @@ ########################################################################## -# Copyright (C) 2022 Kostya Klochko # +# Copyright (C) 2022-2023 Kostiantyn Klochko # # # # This file is part of Anitube Simple Notification. # # # diff --git a/src/scraper.py b/src/scraper.py index ea9a264..6974f72 100644 --- a/src/scraper.py +++ b/src/scraper.py @@ -1,5 +1,5 @@ ########################################################################## -# Copyright (C) 2022 Kostya Klochko # +# Copyright (C) 2022-2023 Kostiantyn Klochko # # # # This file is part of Anitube Simple Notification. # # #