Improve guest accounts loading, add JSONL support

This commit is contained in:
Zed 2023-09-18 18:24:23 +00:00
parent 7abcb489f4
commit 7d14789910
5 changed files with 41 additions and 11 deletions

1
.gitignore vendored
View file

@ -10,4 +10,5 @@ nitter
/public/css/style.css /public/css/style.css
/public/md/*.html /public/md/*.html
nitter.conf nitter.conf
guest_accounts.json*
dump.rdb dump.rdb

View file

@ -0,0 +1,20 @@
import jsony
import ../types/guestaccount
from ../../types import GuestAccount
proc toGuestAccount(account: RawAccount): GuestAccount =
let id = account.oauthToken[0 ..< account.oauthToken.find('-')]
result = GuestAccount(
id: id,
oauthToken: account.oauthToken,
oauthSecret: account.oauthTokenSecret
)
proc parseGuestAccount*(raw: string): GuestAccount =
let rawAccount = raw.fromJson(RawAccount)
result = rawAccount.toGuestAccount
proc parseGuestAccounts*(path: string): seq[GuestAccount] =
let rawAccounts = readFile(path).fromJson(seq[RawAccount])
for account in rawAccounts:
result.add account.toGuestAccount

View file

@ -0,0 +1,4 @@
type
RawAccount* = object
oauthToken*: string
oauthTokenSecret*: string

View file

@ -3,7 +3,6 @@ import asyncdispatch, strformat, logging
from net import Port from net import Port
from htmlgen import a from htmlgen import a
from os import getEnv from os import getEnv
from json import parseJson
import jester import jester
@ -21,9 +20,8 @@ let
(cfg, fullCfg) = getConfig(configPath) (cfg, fullCfg) = getConfig(configPath)
accountsPath = getEnv("NITTER_ACCOUNTS_FILE", "./guest_accounts.json") accountsPath = getEnv("NITTER_ACCOUNTS_FILE", "./guest_accounts.json")
accounts = parseJson(readFile(accountsPath))
initAccountPool(cfg, parseJson(readFile(accountsPath))) initAccountPool(cfg, accountsPath)
if not cfg.enableDebug: if not cfg.enableDebug:
# Silence Jester's query warning # Silence Jester's query warning

View file

@ -1,6 +1,7 @@
#SPDX-License-Identifier: AGPL-3.0-only #SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, times, json, random, strutils, tables, sets import asyncdispatch, times, json, random, strutils, tables, sets, os
import types import types
import experimental/parser/guestaccount
# max requests at a time per account to avoid race conditions # max requests at a time per account to avoid race conditions
const const
@ -141,12 +142,18 @@ proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
account.apis[api] = RateLimit(remaining: remaining, reset: reset) account.apis[api] = RateLimit(remaining: remaining, reset: reset)
proc initAccountPool*(cfg: Config; accounts: JsonNode) = proc initAccountPool*(cfg: Config; path: string) =
enableLogging = cfg.enableDebug enableLogging = cfg.enableDebug
for account in accounts: let jsonlPath = if path.endsWith(".json"): (path & 'l') else: path
accountPool.add GuestAccount(
id: account{"user", "id_str"}.getStr, if fileExists(jsonlPath):
oauthToken: account{"oauth_token"}.getStr, log "Parsing JSONL guest accounts file: ", jsonlPath
oauthSecret: account{"oauth_token_secret"}.getStr, for line in jsonlPath.lines:
) accountPool.add parseGuestAccount(line)
elif fileExists(path):
log "Parsing JSON guest accounts file: ", path
accountPool = parseGuestAccounts(path)
else:
echo "[accounts] ERROR: ", path, " not found. This file is required to authenticate API requests."
quit 1