mirror of
https://github.com/zedeus/nitter
synced 2026-09-06 07:09:31 +00:00
Refactor header code
This commit is contained in:
parent
1871cebf1f
commit
bafe5fb550
9 changed files with 79 additions and 151 deletions
|
|
@ -1,12 +1,35 @@
|
|||
import httpclient, asyncdispatch, htmlparser
|
||||
import strutils, json, xmltree, uri
|
||||
|
||||
import consts
|
||||
|
||||
proc genHeaders*(headers: openArray[tuple[key: string, val: string]];
|
||||
agent: string; referer: Uri; lang=true;
|
||||
auth=false; xml=false): HttpHeaders =
|
||||
result = newHttpHeaders({
|
||||
"referer": $referer,
|
||||
"user-agent": agent,
|
||||
"x-twitter-active-user": "yes",
|
||||
})
|
||||
|
||||
if auth: result["authority"] = "twitter.com"
|
||||
if lang: result["accept-language"] = consts.lang
|
||||
if xml: result["x-requested-with"] = "XMLHttpRequest"
|
||||
|
||||
for (key, val) in headers:
|
||||
result[key] = val
|
||||
|
||||
proc genHeaders*(agent: string; referer: Uri; lang=true;
|
||||
auth=false; xml=false): HttpHeaders =
|
||||
genHeaders([], agent, referer, lang, auth, xml)
|
||||
|
||||
template newClient*() {.dirty.} =
|
||||
var client = newAsyncHttpClient()
|
||||
defer: client.close()
|
||||
client.headers = headers
|
||||
|
||||
proc fetchHtml*(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {.async.} =
|
||||
headers["accept"] = htmlAccept
|
||||
newClient()
|
||||
|
||||
var resp = ""
|
||||
|
|
@ -16,12 +39,11 @@ proc fetchHtml*(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {
|
|||
return nil
|
||||
|
||||
if jsonKey.len > 0:
|
||||
let json = parseJson(resp)[jsonKey].str
|
||||
return parseHtml(json)
|
||||
else:
|
||||
return parseHtml(resp)
|
||||
resp = parseJson(resp)[jsonKey].str
|
||||
return parseHtml(resp)
|
||||
|
||||
proc fetchJson*(url: Uri; headers: HttpHeaders): Future[JsonNode] {.async.} =
|
||||
headers["accept"] = jsonAccept
|
||||
newClient()
|
||||
|
||||
var resp = ""
|
||||
|
|
|
|||
Loading…
Reference in a new issue