commit 3a96009467dfad2a2550aa6141bcc84ae7ec0e96 Author: eeemsi Date: Sun Feb 5 10:11:54 2023 +0100 initial commit diff --git a/mark_as_read.py b/mark_as_read.py new file mode 100644 index 0000000..f4ca016 --- /dev/null +++ b/mark_as_read.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" mark entries in miniflux as read that are older than n days via api calls +""" +import argparse +import configparser +import sys +import time +import requests + +def parse_commandline_args(): + """ assmble commandline arguments - for more infomration see correspnding help entries + """ + parser = argparse.ArgumentParser( + description='mark entries older than n days as read in miniflux using the api of miniflux' + ) + parser.add_argument( + '--config', type=str, help='path to the config file', required=True + ) + + return parser.parse_args() + + +def is_reachable_and_healthy(): + """ check if the api is reachable and healthy + """ + try: + request_healthcheck = requests.get(config['options']['url']+"/healthcheck") + except requests.exceptions.ConnectionError as error: + print(error) + sys.exit(1) + else: + if request_healthcheck.status_code != 200: + request_failed(request_healthcheck) + +def get_ids_older_ndays(): + """ find entries older then delta and add their id to id_array + """ + headers = {'X-Auth-Token': config['options']['token']} + delta = int(time.time()) - int(config['options']['age']) + params = {"status": "unread", "before" : delta} + request_unread = requests.get(config['options']['url']+'/v1/entries', params=params, headers=headers) + + if request_unread.status_code != 200: + request_failed(request_unread) + + entries = request_unread.json() + + if entries["entries"]: + for item in entries["entries"]: + if item['feed']['category']['title'] in config['options']['ignore']: + continue + + id_array.append(item["id"]) + +def mark_as_read(): + """ mark ids from id_array as read + """ + headers = {'X-Auth-Token': config['options']['token']} + payload = {"status": "read", "entry_ids" : id_array} + request_put_ids = requests.put(config['options']['url']+'/v1/entries', json=payload, headers=headers) + + if request_put_ids.status_code != 204: + request_failed(request_put_ids) + +def request_failed(request_input): + """ called when requests did not delive 200 or 204 + """ + print(request_input.status_code, request_input.text) + sys.exit(1) + +if __name__ == "__main__": + id_array = [] + args = parse_commandline_args() + config = configparser.ConfigParser() + config.read(args.config) + is_reachable_and_healthy() + get_ids_older_ndays() + + if id_array: + mark_as_read() + + print("entries marked as read:", len(id_array)) diff --git a/mark_as_read.service b/mark_as_read.service new file mode 100644 index 0000000..f9c8c23 --- /dev/null +++ b/mark_as_read.service @@ -0,0 +1,6 @@ +[Unit] +Description=mark certain entries as read in miniflux + +[Service] +Type=oneshot +ExecStart=/usr/bin/env python3 ./mark_as_read.py --config ./mark_as_read.ini diff --git a/mark_as_read.timer b/mark_as_read.timer new file mode 100644 index 0000000..a618abc --- /dev/null +++ b/mark_as_read.timer @@ -0,0 +1,8 @@ +[Unit] +Description=mark certain entries as read in miniflux + +[Timer] +OnCalendar=daily + +[Install] +WantedBy=timers.target