From 938d9d7d6228dfc30bfc99668db296fc4c4886db Mon Sep 17 00:00:00 2001 From: Ari Gato Date: Sat, 3 Aug 2024 19:07:14 +0200 Subject: [PATCH 1/3] Get access token from environment variable --- README.md | 2 +- find_posts.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ccb0794..1d837903 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ Please find the list of all configuration options, including descriptions, below Option | Required? | Notes | |:----------------------------------------------------|-----------|:------| -|`access-token` | Yes | The access token. If using GitHub action, this needs to be provided as a Secret called `ACCESS_TOKEN`. If running as a cron job or a container, you can supply this option as array, to [fetch posts for multiple users](https://blog.thms.uk/2023/04/muli-user-support-for-fedifetcher) on your instance. | +|`access-token` | Yes | The access token. If using GitHub action, this needs to be provided as a Secret called `ACCESS_TOKEN`. If running as a cron job or a container, you can supply this option as array, to [fetch posts for multiple users](https://blog.thms.uk/2023/04/muli-user-support-for-fedifetcher) on your instance. You may also provide this option as an environment variable starting with `ACCESS_TOKEN`. To fetch posts for multiple users, define multiple environment variables, eg. `ACCESS_TOKEN_USER1=…` and `ACCESS_TOKEN_USER2=…`| |`server`|Yes|The domain only of your mastodon server (without `https://` prefix) e.g. `mstdn.thms.uk`. | |`home-timeline-length` | No | Provide to fetch remote replies to posts in the API-Key owner's home timeline. Determines how many posts we'll fetch replies for. Recommended value: `200`. | `max-bookmarks` | No | Provide to fetch remote replies to any posts you have bookmarked. Determines how many of your bookmarks you want to get replies to. Recommended value: `80`. Requires an access token with `read:bookmarks` scope. diff --git a/find_posts.py b/find_posts.py index c42a32d9..13ec1bd1 100644 --- a/find_posts.py +++ b/find_posts.py @@ -1488,6 +1488,9 @@ def fetch_timeline_context(timeline_posts, token, parsed_urls, seen_hosts, seen_ logger.critical(f"Config file {arguments.config} doesn't exist") sys.exit(1) + if tokens := [token for envvar, token in os.environ.items() if envvar.startswith("ACCESS_TOKEN")]: + arguments.access_token = tokens + logger.info(f"Starting FediFetcher") if(arguments.server == None or arguments.access_token == None): From d246f25bfcc892c8b134e4842edb239d7e4bc982 Mon Sep 17 00:00:00 2001 From: Ari Gato Date: Fri, 9 Aug 2024 12:53:25 +0200 Subject: [PATCH 2/3] Allow all settings to be set via env variables --- README.md | 6 ++++-- find_posts.py | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1d837903..0c73ef31 100644 --- a/README.md +++ b/README.md @@ -132,11 +132,13 @@ If you configure FediFetcher this way, it'll fetch missing remote replies to the #### Advanced Options -Please find the list of all configuration options, including descriptions, below: +Below is a list of all configuration options, including their descriptions. +Please note that configuration options may also be specified as environment variables starting with the prefix `FF_`, case-insensitive, and with `-` replaced with `_`. +For example `max-favourites` can be set via `FF_MAX_FAVOURITES` or `ff_max_favourites`) Option | Required? | Notes | |:----------------------------------------------------|-----------|:------| -|`access-token` | Yes | The access token. If using GitHub action, this needs to be provided as a Secret called `ACCESS_TOKEN`. If running as a cron job or a container, you can supply this option as array, to [fetch posts for multiple users](https://blog.thms.uk/2023/04/muli-user-support-for-fedifetcher) on your instance. You may also provide this option as an environment variable starting with `ACCESS_TOKEN`. To fetch posts for multiple users, define multiple environment variables, eg. `ACCESS_TOKEN_USER1=…` and `ACCESS_TOKEN_USER2=…`| +|`access-token` | Yes | The access token. If using GitHub action, this needs to be provided as a Secret called `ACCESS_TOKEN`. If running as a cron job or a container, you can supply this option as array, to [fetch posts for multiple users](https://blog.thms.uk/2023/04/muli-user-support-for-fedifetcher) on your instance. To set tokens for multiple users using environment variables, define multiple environment variables with `FF_ACCESS_TOKEN` prefix, eg. `FF_ACCESS_TOKEN_USER1=…` and `FF_ACCESS_TOKEN_USER2=…`| |`server`|Yes|The domain only of your mastodon server (without `https://` prefix) e.g. `mstdn.thms.uk`. | |`home-timeline-length` | No | Provide to fetch remote replies to posts in the API-Key owner's home timeline. Determines how many posts we'll fetch replies for. Recommended value: `200`. | `max-bookmarks` | No | Provide to fetch remote replies to any posts you have bookmarked. Determines how many of your bookmarks you want to get replies to. Recommended value: `80`. Requires an access token with `read:bookmarks` scope. diff --git a/find_posts.py b/find_posts.py index 13ec1bd1..6975d412 100644 --- a/find_posts.py +++ b/find_posts.py @@ -1488,7 +1488,26 @@ def fetch_timeline_context(timeline_posts, token, parsed_urls, seen_hosts, seen_ logger.critical(f"Config file {arguments.config} doesn't exist") sys.exit(1) - if tokens := [token for envvar, token in os.environ.items() if envvar.startswith("ACCESS_TOKEN")]: + for envvar, value in os.environ.items(): + envvar = envvar.lower() + if envvar.startswith("ff_") and not envvar.startswith("ff_access_token"): + envvar = envvar[3:] + # most settings are numerical + if envvar not in [ + "server", + "lock_file", + "state_dir", + "on_start", + "on_done", + "on_fail", + "log_level", + "log_format" + ]: + value = int(value) + setattr(arguments, envvar, value) + + # remains special-cased for specifying multiple tokens + if tokens := [token for envvar, token in os.environ.items() if envvar.lower().startswith("ff_access_token")]: arguments.access_token = tokens logger.info(f"Starting FediFetcher") From 676eaa48d7674dd50f5b0bb90eb2811475f3ee76 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 9 Aug 2024 13:07:16 +0100 Subject: [PATCH 3/3] Clarify instructions README.md --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c73ef31..72e41bdc 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,22 @@ FediFetcher has quite a few configuration options, so here is my quick configura If you configure FediFetcher this way, it'll fetch missing remote replies to the last 200 posts in your home timeline. It'll additionally backfill profiles of the last 80 people you followed, and of every account who appeared in your notifications during the past hour. +#### Providing configuration options + +Unless you are running FediFetcher as GitHub Action (please see above for instructions on configuring FediFetcher with GitHub Actions), there are a three ways in which you provide configuration options: + +1. Configuration File:
+ You can provide a `json` file with configuration options. Then run the script like so:
`python find_posts.py -c=/path/to/config.json` +2. Command line flags:
+ You can provide all options directly in the command line. Simply run the script with te correct options supplied:
`python find_posts.py --server=example.com --home-timeline-length=80`. +3. Environment variables:
+ You can supply your options as environment variables. To do so take the option name from the table below, replace `-` with `_` and prefix with `FF_`. For example `max-favourites` can be set via `FF_MAX_FAVOURITES`. (Environment variables are not case sensitive.) + + + #### Advanced Options Below is a list of all configuration options, including their descriptions. -Please note that configuration options may also be specified as environment variables starting with the prefix `FF_`, case-insensitive, and with `-` replaced with `_`. -For example `max-favourites` can be set via `FF_MAX_FAVOURITES` or `ff_max_favourites`) Option | Required? | Notes | |:----------------------------------------------------|-----------|:------|