Skip to content

Commit 8d5d971

Browse files
authored
Merge pull request #154 from arachnist/token-from-env
Get access token from environment variable
2 parents db0886d + 676eaa4 commit 8d5d971

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,26 @@ FediFetcher has quite a few configuration options, so here is my quick configura
130130

131131
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.
132132

133+
#### Providing configuration options
134+
135+
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:
136+
137+
1. Configuration File: <br>
138+
You can provide a `json` file with configuration options. Then run the script like so: <br>`python find_posts.py -c=/path/to/config.json`
139+
2. Command line flags: <br>
140+
You can provide all options directly in the command line. Simply run the script with te correct options supplied: <br>`python find_posts.py --server=example.com --home-timeline-length=80`.
141+
3. Environment variables: <br>
142+
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.)
143+
144+
145+
133146
#### Advanced Options
134147

135-
Please find the list of all configuration options, including descriptions, below:
148+
Below is a list of all configuration options, including their descriptions.
136149

137150
Option | Required? | Notes |
138151
|:----------------------------------------------------|-----------|:------|
139-
|`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. |
152+
|`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=…`|
140153
|`server`|Yes|The domain only of your mastodon server (without `https://` prefix) e.g. `mstdn.thms.uk`. |
141154
|`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`.
142155
| `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.

find_posts.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,28 @@ def fetch_timeline_context(timeline_posts, token, parsed_urls, seen_hosts, seen_
14881488
logger.critical(f"Config file {arguments.config} doesn't exist")
14891489
sys.exit(1)
14901490

1491+
for envvar, value in os.environ.items():
1492+
envvar = envvar.lower()
1493+
if envvar.startswith("ff_") and not envvar.startswith("ff_access_token"):
1494+
envvar = envvar[3:]
1495+
# most settings are numerical
1496+
if envvar not in [
1497+
"server",
1498+
"lock_file",
1499+
"state_dir",
1500+
"on_start",
1501+
"on_done",
1502+
"on_fail",
1503+
"log_level",
1504+
"log_format"
1505+
]:
1506+
value = int(value)
1507+
setattr(arguments, envvar, value)
1508+
1509+
# remains special-cased for specifying multiple tokens
1510+
if tokens := [token for envvar, token in os.environ.items() if envvar.lower().startswith("ff_access_token")]:
1511+
arguments.access_token = tokens
1512+
14911513
logger.info(f"Starting FediFetcher")
14921514

14931515
if(arguments.server == None or arguments.access_token == None):

0 commit comments

Comments
 (0)