mirror of
https://github.com/zedeus/nitter
synced 2026-09-06 07:09:31 +00:00
Refactor api code
This commit is contained in:
parent
014f01bf88
commit
43bf6735d4
13 changed files with 414 additions and 397 deletions
32
src/api/utils.nim
Normal file
32
src/api/utils.nim
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import httpclient, asyncdispatch, htmlparser
|
||||
import strutils, json, xmltree, uri
|
||||
|
||||
template newClient*() {.dirty.} =
|
||||
var client = newAsyncHttpClient()
|
||||
defer: client.close()
|
||||
client.headers = headers
|
||||
|
||||
proc fetchHtml*(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {.async.} =
|
||||
newClient()
|
||||
|
||||
var resp = ""
|
||||
try:
|
||||
resp = await client.getContent($url)
|
||||
except:
|
||||
return nil
|
||||
|
||||
if jsonKey.len > 0:
|
||||
let json = parseJson(resp)[jsonKey].str
|
||||
return parseHtml(json)
|
||||
else:
|
||||
return parseHtml(resp)
|
||||
|
||||
proc fetchJson*(url: Uri; headers: HttpHeaders): Future[JsonNode] {.async.} =
|
||||
newClient()
|
||||
|
||||
var resp = ""
|
||||
try:
|
||||
resp = await client.getContent($url)
|
||||
result = parseJson(resp)
|
||||
except:
|
||||
return nil
|
||||
Loading…
Reference in a new issue