nitter/src/api/timeline.nim

54 lines
1.8 KiB
Nim
Raw Normal View History

2019-09-21 01:08:30 +02:00
import httpclient, asyncdispatch, htmlparser, strformat
2019-10-02 22:28:53 +02:00
import sequtils, strutils, json, uri
2019-09-06 03:37:12 +02:00
2019-09-13 22:24:58 +02:00
import ".."/[types, parser, parserutils, formatters, query]
2019-09-20 15:03:18 +02:00
import utils, consts, media, search
2019-09-06 03:37:12 +02:00
2019-10-02 22:28:53 +02:00
proc getMedia(thread: Thread | Timeline; agent: string) {.async.} =
await all(getVideos(thread, agent),
getCards(thread, agent),
getPolls(thread, agent))
2019-09-19 02:23:22 +02:00
proc finishTimeline*(json: JsonNode; query: Query; after, agent: string): Future[Timeline] {.async.} =
2019-09-20 15:03:18 +02:00
result = getResult[Tweet](json, query, after)
if json == nil: return
2019-09-06 03:37:12 +02:00
if json["new_latent_count"].to(int) == 0: return
if not json.hasKey("items_html"): return
2019-10-02 22:28:53 +02:00
let html = parseHtml(json["items_html"].to(string))
let thread = parseThread(html)
2019-09-06 03:37:12 +02:00
2019-10-02 22:28:53 +02:00
await getMedia(thread, agent)
2019-09-06 03:37:12 +02:00
result.content = thread.content
proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
var params = toSeq({
"include_available_features": "1",
"include_entities": "1",
"include_new_items_bar": "false",
"reset_error_state": "false"
})
if after.len > 0:
params.add {"max_position": after}
2019-10-02 22:28:53 +02:00
let headers = genHeaders(agent, base / username, xml=true)
2019-09-06 03:37:12 +02:00
let json = await fetchJson(base / (timelineUrl % username) ? params, headers)
2019-10-02 22:28:53 +02:00
2019-09-19 02:23:22 +02:00
result = await finishTimeline(json, Query(), after, agent)
2019-09-06 03:37:12 +02:00
proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} =
var url = base / username
if after.len > 0:
url = url ? {"max_position": after}
let
2019-10-02 10:13:17 +02:00
headers = genHeaders(agent, base / username, auth=true)
2019-09-06 03:37:12 +02:00
html = await fetchHtml(url, headers)
timeline = parseTimeline(html.select("#timeline > .stream-container"), after)
profile = parseTimelineProfile(html)
2019-10-02 22:28:53 +02:00
await getMedia(timeline, agent)
2019-09-06 03:37:12 +02:00
result = (profile, timeline)