فهرست منبع

Improve guest accounts loading, add JSONL support

Zed 3 سال پیش
والد
کامیت
7d14789910
5فایلهای تغییر یافته به همراه41 افزوده شده و 11 حذف شده
  1. 1 0
      .gitignore
  2. 20 0
      src/experimental/parser/guestaccount.nim
  3. 4 0
      src/experimental/types/guestaccount.nim
  4. 1 3
      src/nitter.nim
  5. 15 8
      src/tokens.nim

+ 1 - 0
.gitignore

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

+ 20 - 0
src/experimental/parser/guestaccount.nim

@@ -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 - 0
src/experimental/types/guestaccount.nim

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

+ 1 - 3
src/nitter.nim

@@ -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

+ 15 - 8
src/tokens.nim

@@ -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