Retained statuses; per-minute expiry times

master
Thor 3 years ago
parent e412e6deb7
commit bfdfc901fe
  1. 2
      config.example.json
  2. 43
      fedidel.py

@ -1,5 +1,5 @@
{ {
"max_age": 48, "max_age": 90,
"instances": [ "instances": [
"mastodon.social" "mastodon.social"
] ]

@ -14,20 +14,16 @@ def log_print(source, text):
print(text) print(text)
def log_pprint(source, obj): def log_pprint(source, obj):
log(source, pprint.pformat(obj)) log_print(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)
def encode_time(dt): 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): 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: class Instance:
def __init__(self, name, config): def __init__(self, name, config):
@ -136,23 +132,32 @@ class Instance:
def purger(self): def purger(self):
while True: while True:
try: try:
deleted = False
timeslot_key, status_id = self.next_expired() timeslot_key, status_id = self.next_expired()
if not timeslot_key is None: if not timeslot_key is None:
log_print(self.name, "Deleting status {} in timeslot {}".format(status_id, timeslot_key)) try:
log_print(self.name, "Inspecting status {} in timeslot {}".format(status_id, timeslot_key))
try: status = self.api.status(status_id)
self.api.status_delete(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: except MastodonNotFoundError:
log_print(self.name, 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) self.expire_status(timeslot_key, status_id)
if deleted:
time.sleep(60) time.sleep(60)
else: else:
time.sleep(1) time.sleep(1)
except: except:
log_print(self.name, traceback.format_exc()) log_print(self.name, traceback.format_exc())
time.sleep(60) time.sleep(60)
@ -214,7 +219,7 @@ class Instance:
def next_expired(self): def next_expired(self):
now = datetime.now(timezone.utc) 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() self.state_lock.acquire()

Loading…
Cancel
Save