From a05cf3cfe0411aa6e0c3ba567168f3d39a6b724f Mon Sep 17 00:00:00 2001 From: myumyu <23904-myumyu@users.noreply.gitgud.io> Date: Sat, 13 Nov 2021 18:03:38 +0000 Subject: [PATCH] Updated README, added new package and changed small things --- README.md | 25 +++++++++++++++-------- components/__init__.py | 0 evaluators.py => components/evaluators.py | 8 -------- components/notifiers.py | 18 ++++++++++++++++ watchers.py => components/watchers.py | 2 ++ config/config_example.py | 2 +- main.py | 13 ++++++++---- 7 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 components/__init__.py rename evaluators.py => components/evaluators.py (80%) create mode 100644 components/notifiers.py rename watchers.py => components/watchers.py (99%) diff --git a/README.md b/README.md index df7545e..0003b41 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,28 @@ # globalafk -A simple python script that sends ugly notifications when something happens on a jschan imageboard that you moderate. +A python script that sends ugly notifications when something happens on a [*jschan*](https://gitgud.io/fatchan/jschan) imageboard that you moderate. +It has been developed around composition principles in order to help you to expand or change it to fit your needs and goals. Most of the components are easily swappable. + +## Features +For now, this project has one (1) basic feature, send notifications. +That said, you can configure it to send notifications when: +- New reports +- A new post includes some fine-grained pre-configured entry or some (clear or partially obfuscated) url that you have not whitelisted + +All of these features can be turned off independently. ## Requirements -A *Global Staff* account is obviously required. +You need to have **moderation privileges** in at least one board and an environment with **python3** and **the dependencies listed in [*requirements.txt*](./requirements.txt) file** in order to run this script. -### Linux -You must have **notify-send** installed to receive notifications on linux. +To send notifications on Linux you must have **notify-send** installed to receive notifications on linux. Run `notify-send test` to test it. -### Termux (Android) -You must have **Termux:API** installed to receive notifications on android. +To send notifications on Android (assuming you are using [*Termux*](https://termux.com/)) you must have **Termux:API** installed to receive notifications on android. Run `termux-notification --title test` to test it. ## Getting Started 1) (Optional) Create and activate a virtual environment 2) Run `pip3 install -r requirements.txt` to install the dependencies -3) Make a copy of [*config_example.py*](./config/config_example.py) and rename it to *config.py* -4) Run `python3 main.py` \ No newline at end of file +3) Install (if is not already installed) *notify-send* or *termux-notification* accordingly to your needs +4) Make a copy of [*config_example.py*](./config/config_example.py) and rename it to *config.py* +5) Fill the account details and configure the script behavior in your new *config.py* file +6) Run `python3 main.py` diff --git a/components/__init__.py b/components/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/evaluators.py b/components/evaluators.py similarity index 80% rename from evaluators.py rename to components/evaluators.py index cc6ad3d..20e4fd7 100755 --- a/evaluators.py +++ b/components/evaluators.py @@ -4,8 +4,6 @@ from abc import ABC, abstractmethod from urlextract import URLExtract -from config import config - class Evaluator(ABC): @abstractmethod @@ -13,12 +11,6 @@ class Evaluator(ABC): raise NotImplementedError -def _format_match(match): - s, e = match.start(0), match.end(0) - m = match.string[:s] + config.TRIGGER_WRAPPER + match.string[s:e] + config.TRIGGER_WRAPPER + match.string[e:] - return m[s - config.TRIGGER_OFFSET or 0:e if e + config.TRIGGER_OFFSET > len(m) else e + config.TRIGGER_OFFSET] - - class PostEvaluator(Evaluator): def __init__(self, blacklist=(), url_whitelist=()): self.blacklist_re = re.compile('|'.join(blacklist), re.IGNORECASE) if blacklist else None diff --git a/components/notifiers.py b/components/notifiers.py new file mode 100644 index 0000000..c5b454c --- /dev/null +++ b/components/notifiers.py @@ -0,0 +1,18 @@ +import subprocess +from abc import ABC, abstractmethod + + +class Notifier(ABC): + @abstractmethod + def notify(self, title, content, *args, **kwargs): + raise NotImplementedError + + +class TermuxNotifier(Notifier): + def notify(self, title, content, *args, **kwargs): + subprocess.call(['termux-notification', '--title', title, '--content', content]) + + +class NotifySendNotifier(Notifier): + def notify(self, title, content, *args, **kwargs): + subprocess.call(['notify-send', title, content]) diff --git a/watchers.py b/components/watchers.py similarity index 99% rename from watchers.py rename to components/watchers.py index 50264ac..b6945fb 100644 --- a/watchers.py +++ b/components/watchers.py @@ -83,7 +83,9 @@ class ReportsWatcher(Watcher): self.notify(f'New reports!', "\n".join([ f'{get_path(p)} {[r["reason"] for r in (p["globalreports"] if "globalreports" in p else p["reports"])]}' for p in reported_posts])) + self.known_reports = num_reported_posts + except RequestException as e: logging.error(f'Exception {e} occurred while fetching reports') self.notify(f'Error while fetching reports', f'Trying to reconnect') diff --git a/config/config_example.py b/config/config_example.py index f76a2dc..bf42721 100755 --- a/config/config_example.py +++ b/config/config_example.py @@ -8,7 +8,7 @@ BOARDS: tuple = ( # (None,) if global or boards that you moderate """Notifications""" USE_TERMUX_API: bool = False -"""Reports watcher """ +"""Reports watcher""" WATCH_REPORTS: bool = True FETCH_REPORTS_INTERVAL: int = 60 * 2 # interval between reports fetch (in seconds) diff --git a/main.py b/main.py index c61bb94..c5ab333 100755 --- a/main.py +++ b/main.py @@ -2,13 +2,18 @@ import logging from config import config from session import ModSession -from watchers import ReportsWatcher, RecentWatcher -from notifiers import TermuxNotifier, NotifySendNotifier -from evaluators import PostEvaluator +from components.watchers import ReportsWatcher, RecentWatcher +from components.notifier import TermuxNotifier, NotifySendNotifier +from components.evaluators import PostEvaluator + + +def format_match(match): + s, e = match.start(0), match.end(0) + m = match.string[:s] + config.TRIGGER_WRAPPER + match.string[s:e] + config.TRIGGER_WRAPPER + match.string[e:] + return m[s - config.TRIGGER_OFFSET or 0:e if e + config.TRIGGER_OFFSET > len(m) else e + config.TRIGGER_OFFSET] def main(): - # filename = "log.log", filemode = 'a' logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s %(funcName)s] %(message)s (%(name)s)')