Skip to content

Commit

Permalink
Improve guest accounts loading, add JSONL support
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Sep 18, 2023
1 parent 7abcb48 commit 7d14789
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ nitter
/public/css/style.css
/public/md/*.html
nitter.conf
guest_accounts.json*
dump.rdb
20 changes: 20 additions & 0 deletions src/experimental/parser/guestaccount.nim
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions src/experimental/types/guestaccount.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type
RawAccount* = object
oauthToken*: string
oauthTokenSecret*: string
4 changes: 1 addition & 3 deletions src/nitter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
23 changes: 15 additions & 8 deletions src/tokens.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7d14789

Please sign in to comment.