소스 검색

Use old API endpoint to fetch tokens

Zed 6 년 전
부모
커밋
4c1270b377
1개의 변경된 파일16개의 추가작업 그리고 16개의 파일을 삭제
  1. 16 16
      src/tokens.nim

+ 16 - 16
src/tokens.nim

@@ -1,5 +1,5 @@
-import asyncdispatch, httpclient, times, sequtils, strutils
-import types
+import asyncdispatch, httpclient, times, sequtils, strutils, json
+import types, agents, consts
 
 var tokenPool {.threadvar.}: seq[Token]
 
@@ -9,27 +9,27 @@ proc fetchToken(): Future[Token] {.async.} =
       "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
       "accept-language": "en-US,en;q=0.5",
       "connection": "keep-alive",
-      "user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0"
+      "user-agent": getAgent(),
+      "authorization": auth
     })
     client = newAsyncHttpClient(headers=headers)
 
-  var resp: string
+  var
+    resp: string
+    tok: string
 
   try:
-    resp = await client.getContent("https://twitter.com")
-    client.close()
-  except:
-    echo "fetching token failed"
-    return Token()
+    resp = await client.postContent(activate)
+    tok = parseJson(resp)["guest_token"].getStr
 
-  let pos = resp.rfind("gt=")
-  if pos == -1:
-    echo "token parse fail"
+    let time = getTime()
+    result = Token(tok: tok, remaining: 187, reset: time + 15.minutes,
+                   init: time, lastUse: time)
+  except Exception as e:
+    echo "fetching token failed: ", e.msg
     return Token()
-
-  let time = getTime()
-  result = Token(tok: resp[pos+3 .. pos+21], remaining: 187,
-                 reset: time + 15.minutes, init: time, lastUse: time)
+  finally:
+    client.close()
 
 proc expired(token: Token): bool {.inline.} =
   const