From c0d680258ff0fe141fbabcf14a60eee8994e8d18 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Sun, 28 Jun 2020 14:04:29 +0200 Subject: [PATCH] Add support for archiving toots into JSON files --- README.md | 1 + example-config.yaml | 3 ++- lib/ephemetoot.py | 11 ++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5015369..bfa6890 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ You can now enter the configuration details for each user: | toots_to_keep | A list of toot ids indicating toots to be kept regardless of other settings. The ID of a toot is the last part of its individual URL. e.g. for [https://ausglam.space/@hugh/101294246770105799](https://ausglam.space/@hugh/101294246770105799) the id is `101294246770105799` | | hashtags_to_keep | A list of hashtags, where any toots with any of these hashtags will be kept regardless of age. Do not include the '#' symbol. Do remember the [rules for hashtags](https://docs.joinmastodon.org/user/posting/#hashtags) | | visibility_to_keep | Toots with any of the visibility settings in this list will be kept regardless of age. Options are: `public`, `unlisted`, `private`, `direct`. | +| archive | The full toot is archived into individual files named by the Toot's `id` in this writeable directory. | All values other than `access_token`, `username` and `base_url` are optional, however if you include `toots_to_keep`, `hashtags_to_keep`, or `visibility_to_keep` you must make each a list, even if it is empty: diff --git a/example-config.yaml b/example-config.yaml index 450fd4e..36fa56c 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -6,6 +6,7 @@ # toots_to_keep : a list of toot ids indicating toots to be kept regardless of other settings # hashtags_to_keep : a list of hashtags, where any toots with any of these hashtags will be kept. Do not include the '#' symbol # visibility_to_keep : any toots with visibility settings in this list will be kept. Options are: 'public', 'unlisted', 'private', 'direct' +# archive : path to a writeable directory into which toots are "archived" as JSON files # you can list only one user, or multiple users # each user account should be preceded by a single dash, and indented, as per below @@ -32,4 +33,4 @@ access_token : AZ-Yj3aBD8U8Cm7lKUp-lm9O9BmDgdhHzDeqsY8tlL9 username : bob base_url : aus.social - days_to_keep : 30 \ No newline at end of file + days_to_keep : 30 diff --git a/lib/ephemetoot.py b/lib/ephemetoot.py index 9c90d79..9706626 100644 --- a/lib/ephemetoot.py +++ b/lib/ephemetoot.py @@ -79,8 +79,17 @@ def checkToots(config, options, retry_count=0): + config['base_url'] ) + def jsondefault(obj): + if isinstance(obj, (date, datetime)): + return obj.isoformat() def checkBatch(timeline, deleted_count=0): for toot in timeline: + if 'id' in toot and 'archive' in config: + print(toot) + filename = os.path.join(config['archive'], str(toot['id']) + '.json') + with open(filename, "w") as f: + f.write(json.dumps(toot, indent=4, default=jsondefault)) + f.close() toot_tags = set() for tag in toot.tags: toot_tags.add(tag.name) @@ -317,4 +326,4 @@ def checkToots(config, options, retry_count=0): print( 'Attempt ' + str(retry_count + 1) ) checkToots(config, options, retry_count) else: - print('Gave up waiting for network') \ No newline at end of file + print('Gave up waiting for network')