Improve guest accounts loading, add JSONL support
This commit is contained in:
parent
7abcb489f4
commit
7d14789910
5 changed files with 41 additions and 11 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -10,4 +10,5 @@ nitter
|
|||
/public/css/style.css
|
||||
/public/md/*.html
|
||||
nitter.conf
|
||||
guest_accounts.json*
|
||||
dump.rdb
|
||||
|
|
20
src/experimental/parser/guestaccount.nim
Normal file
20
src/experimental/parser/guestaccount.nim
Normal 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
|
4
src/experimental/types/guestaccount.nim
Normal file
4
src/experimental/types/guestaccount.nim
Normal file
|
@ -0,0 +1,4 @@
|
|||
type
|
||||
RawAccount* = object
|
||||
oauthToken*: string
|
||||
oauthTokenSecret*: string
|
|
@ -3,7 +3,6 @@ import asyncdispatch, strformat, logging
|
|||
from net import Port
|
||||
from htmlgen import a
|
||||
from os import getEnv
|
||||
from json import parseJson
|
||||
|
||||
import jester
|
||||
|
||||
|
@ -21,9 +20,8 @@ let
|
|||
(cfg, fullCfg) = getConfig(configPath)
|
||||
|
||||
accountsPath = getEnv("NITTER_ACCOUNTS_FILE", "./guest_accounts.json")
|
||||
accounts = parseJson(readFile(accountsPath))
|
||||
|
||||
initAccountPool(cfg, parseJson(readFile(accountsPath)))
|
||||
initAccountPool(cfg, accountsPath)
|
||||
|
||||
if not cfg.enableDebug:
|
||||
# Silence Jester's query warning
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#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 experimental/parser/guestaccount
|
||||
|
||||
# max requests at a time per account to avoid race conditions
|
||||
const
|
||||
|
@ -141,12 +142,18 @@ proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
|
|||
|
||||
account.apis[api] = RateLimit(remaining: remaining, reset: reset)
|
||||
|
||||
proc initAccountPool*(cfg: Config; accounts: JsonNode) =
|
||||
proc initAccountPool*(cfg: Config; path: string) =
|
||||
enableLogging = cfg.enableDebug
|
||||
|
||||
for account in accounts:
|
||||
accountPool.add GuestAccount(
|
||||
id: account{"user", "id_str"}.getStr,
|
||||
oauthToken: account{"oauth_token"}.getStr,
|
||||
oauthSecret: account{"oauth_token_secret"}.getStr,
|
||||
)
|
||||
let jsonlPath = if path.endsWith(".json"): (path & 'l') else: path
|
||||
|
||||
if fileExists(jsonlPath):
|
||||
log "Parsing JSONL guest accounts file: ", jsonlPath
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue