2020-06-01 00:16:24 +00:00
|
|
|
import asyncdispatch, httpclient, times, sequtils, strutils
|
|
|
|
import types
|
|
|
|
|
|
|
|
var tokenPool: seq[Token]
|
|
|
|
|
|
|
|
proc fetchToken(): Future[Token] {.async.} =
|
|
|
|
let
|
|
|
|
headers = newHttpHeaders({
|
|
|
|
"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"
|
|
|
|
})
|
|
|
|
client = newAsyncHttpClient(headers=headers)
|
|
|
|
|
2020-06-02 18:37:55 +00:00
|
|
|
var resp: string
|
|
|
|
|
|
|
|
try:
|
|
|
|
resp = await client.getContent("https://twitter.com")
|
|
|
|
client.close()
|
|
|
|
except:
|
|
|
|
echo "fetching token failed"
|
|
|
|
return Token()
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-02 18:37:55 +00:00
|
|
|
let pos = resp.rfind("gt=")
|
2020-06-01 11:54:14 +00:00
|
|
|
if pos == -1:
|
|
|
|
echo "token parse fail"
|
|
|
|
return Token()
|
|
|
|
|
2020-06-01 11:40:26 +00:00
|
|
|
result = Token(tok: resp[pos+3 .. pos+21], remaining: 187,
|
2020-06-01 00:16:24 +00:00
|
|
|
reset: getTime() + 15.minutes, init: getTime())
|
|
|
|
|
|
|
|
proc expired(token: Token): bool {.inline.} =
|
2020-06-01 11:40:26 +00:00
|
|
|
const expirationTime = 2.hours
|
2020-06-01 00:16:24 +00:00
|
|
|
result = token.init < getTime() - expirationTime
|
|
|
|
|
|
|
|
proc isLimited(token: Token): bool {.inline.} =
|
2020-06-06 02:39:22 +00:00
|
|
|
token == nil or (token.remaining <= 1 and token.reset > getTime()) or
|
2020-06-01 00:16:24 +00:00
|
|
|
token.expired
|
|
|
|
|
|
|
|
proc release*(token: Token) =
|
|
|
|
if token != nil and not token.expired:
|
|
|
|
tokenPool.insert(token)
|
|
|
|
|
|
|
|
proc getToken*(): Future[Token] {.async.} =
|
|
|
|
for i in 0 ..< tokenPool.len:
|
|
|
|
if not result.isLimited: break
|
|
|
|
result.release()
|
|
|
|
result = tokenPool.pop()
|
|
|
|
|
|
|
|
if result.isLimited:
|
|
|
|
result.release()
|
|
|
|
result = await fetchToken()
|
|
|
|
|
|
|
|
proc poolTokens*(amount: int) {.async.} =
|
|
|
|
var futs: seq[Future[Token]]
|
|
|
|
for i in 0 ..< amount:
|
|
|
|
futs.add fetchToken()
|
|
|
|
|
|
|
|
for token in futs:
|
|
|
|
release(await token)
|
|
|
|
|
|
|
|
proc initTokenPool*(cfg: Config) {.async.} =
|
|
|
|
while true:
|
2020-06-01 11:40:26 +00:00
|
|
|
if tokenPool.countIt(not it.isLimited) < cfg.minTokens:
|
2020-06-01 11:54:45 +00:00
|
|
|
await poolTokens(min(4, cfg.minTokens - tokenPool.len))
|
|
|
|
await sleepAsync(2000)
|