(Added) TOML configuration file. (Updated) Paths, name, years.

main
KKlochko 2 years ago
parent 5c16001824
commit 222bf53b25

@ -25,3 +25,7 @@
Added img/ folder. Added img/ folder.
** 0.6.0 <2022-08-11> ** 0.6.0 <2022-08-11>
Updated the cli interface 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.

@ -1,10 +1,33 @@
* Anitube Simple Notification * Anitube Simple Notification
Anitube Simple Notification is a application made for getting Anitube Simple Notification is a application made for getting
notification when a content is updated on the web-site (anitube.in.ua). 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 * Author
Kostya Klochko (c) 2022 Kostiantyn Klochko (c) 2022-2023
* Donation * Donation
Monero: 8BCZr3LaciDZUwNUbC8M5gNZTtnPKoT9cMH95YcBoo2k8sg4qaxejYL4Qvp6V21ViqMHj5sHLiuRwhMYxHTVW1HUNAawV6c Monero: 8BCZr3LaciDZUwNUbC8M5gNZTtnPKoT9cMH95YcBoo2k8sg4qaxejYL4Qvp6V21ViqMHj5sHLiuRwhMYxHTVW1HUNAawV6c

@ -3,12 +3,40 @@ Anitube Simple Notification
Anitube Simple Notification is a application made for getting Anitube Simple Notification is a application made for getting
notification when a content is updated on the web-site (anitube.in.ua). 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 Author
====== ======
Kostya Klochko (c) 2022 Kostiantyn Klochko (c) 2022-2023
Donation Donation
======== ========

@ -4,9 +4,9 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "anitube-simple-notification" name = "anitube-simple-notification"
version = "0.5.2" version = "1.0.0"
authors = [ authors = [
{ name="Kostya Klochko", email="kostya_klochko@ukr.net" }, { name="Kostiantyn Klochko", email="kostya_klochko@ukr.net" },
] ]
description = "Getting notification from anitube.in.ua." description = "Getting notification from anitube.in.ua."
readme = "README.rst" readme = "README.rst"
@ -22,6 +22,8 @@ dependencies = [
"bs4", "bs4",
"notify-py", "notify-py",
"rich", "rich",
"tomli",
"loguru"
] ]
[project.urls] [project.urls]

@ -0,0 +1,87 @@
##########################################################################
# Copyright (C) 2022-2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# #
# 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 #
# <https://www.gnu.org/licenses/>. #
##########################################################################
"""
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

@ -1,5 +1,5 @@
########################################################################## ##########################################################################
# Copyright (C) 2022 Kostya Klochko <kostya_klochko@ukr.net> # # Copyright (C) 2022-2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# # # #
# This file is part of Anitube Simple Notification. # # This file is part of Anitube Simple Notification. #
# # # #

@ -1,5 +1,5 @@
########################################################################## ##########################################################################
# Copyright (C) 2022 Kostya Klochko <kostya_klochko@ukr.net> # # Copyright (C) 2022-2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# # # #
# This file is part of Anitube Simple Notification. # # This file is part of Anitube Simple Notification. #
# # # #
@ -20,43 +20,37 @@
from db import DataBase from db import DataBase
from scraper import Scraper from scraper import Scraper
from notify import Notification from notify import Notification
from config import Config
from rich.console import Console from rich.console import Console
from rich.progress import track from rich.progress import track
import time import time
import os
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
def main(): def main():
# Const sections. # 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. # Here you can change testing headers to yours.
HEADERS = { HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' '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." MESSAGE = "New episode."
URL_FILE = "urls" WAITING_PERIOD = config.pop('WAITING_PERIOD', 60) # seconds
WAITING_PERIOD = 60 # seconds POSTERS = config.pop('POSTERS', True) # for use poster as icons of notifications
POSTERS = True # For use poster as icons of notifications urls = config.pop('URLS', [])
# Initialising objects # Initialising objects
scr = Scraper(HEADERS) scr = Scraper(HEADERS, POSTERS_PATH)
db = DataBase() db = DataBase(DB_PATH)
# Reading the URL_FILE for making list
urls = get_urls(URL_FILE)
#print(f"{urls}")
# Console initialising
console = Console()
# Checks for new urls in file and add as current state # Checks for new urls in file and add as current state
# If one of page has updated then notifing. # If one of page has updated then notifing.

@ -1,5 +1,5 @@
########################################################################## ##########################################################################
# Copyright (C) 2022 Kostya Klochko <kostya_klochko@ukr.net> # # Copyright (C) 2022-2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# # # #
# This file is part of Anitube Simple Notification. # # This file is part of Anitube Simple Notification. #
# # # #

@ -1,5 +1,5 @@
########################################################################## ##########################################################################
# Copyright (C) 2022 Kostya Klochko <kostya_klochko@ukr.net> # # Copyright (C) 2022-2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# # # #
# This file is part of Anitube Simple Notification. # # This file is part of Anitube Simple Notification. #
# # # #

Loading…
Cancel
Save