From bfdfc901fe3a344b9ec4928a759d49a2b2adcc41 Mon Sep 17 00:00:00 2001 From: Thor Harald Johansen Date: Sat, 5 Dec 2020 10:14:34 +0100 Subject: [PATCH] Retained statuses; per-minute expiry times --- config.example.json | 2 +- fedidel.py | 43 ++++++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/config.example.json b/config.example.json index d01ff06..4bc291e 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,5 @@ { - "max_age": 48, + "max_age": 90, "instances": [ "mastodon.social" ] diff --git a/fedidel.py b/fedidel.py index 8dadf4e..3a9d634 100644 --- a/fedidel.py +++ b/fedidel.py @@ -14,20 +14,16 @@ def log_print(source, text): print(text) def log_pprint(source, obj): - log(source, pprint.pformat(obj)) - -# Floor datetime to nearest hour -#def floor_dt(dt): -# return dt - timedelta( -# minutes = dt.minute, -# seconds = dt.second, -# microseconds = dt.microsecond) + log_print(source, pprint.pformat(obj)) def encode_time(dt): - return int(dt.strftime("%Y%m%d%H")) + return int(dt.strftime("%Y%m%d%H%M")) def decode_time(value): - return dt.strptime(str(value), "%Y%m%d%H") + if len(value) == 12: + return dt.strptime(str(value), "%Y%m%d%H%M") + else: + return dt.strptime(str(value), "%Y%m%d%H") class Instance: def __init__(self, name, config): @@ -136,23 +132,32 @@ class Instance: def purger(self): while True: try: - + deleted = False timeslot_key, status_id = self.next_expired() - + if not timeslot_key is None: - log_print(self.name, "Deleting status {} in timeslot {}".format(status_id, timeslot_key)) - - try: - self.api.status_delete(status_id) - + try: + log_print(self.name, "Inspecting status {} in timeslot {}".format(status_id, timeslot_key)) + status = self.api.status(status_id) + + if status["favourited"]: + log_print(self.name, "Keeping favourited status {} in timeslot {}".format(status_id, timeslot_key)) + else: + log_print(self.name, "Deleting status {} in timeslot {}".format(status_id, timeslot_key)) + self.api.status_delete(status_id) + deleted = True + except MastodonNotFoundError: log_print(self.name, - "Cannot delete missing status {} from server".format(status_id)) + "Cannot find status {} on server".format(status_id)) self.expire_status(timeslot_key, status_id) + + if deleted: time.sleep(60) else: time.sleep(1) + except: log_print(self.name, traceback.format_exc()) time.sleep(60) @@ -214,7 +219,7 @@ class Instance: def next_expired(self): now = datetime.now(timezone.utc) - min_timeslot_key = encode_time(now - timedelta(hours = self.config["max_age"])) + min_timeslot_key = encode_time(now - timedelta(minutes = self.config["max_age"])) self.state_lock.acquire()