2019-09-06 01:37:12 +00:00
|
|
|
import httpclient, asyncdispatch, times, sequtils, strutils, json, uri
|
|
|
|
|
|
|
|
import ".."/[types, parser, formatters]
|
|
|
|
import utils, consts
|
|
|
|
|
|
|
|
var
|
|
|
|
guestToken = ""
|
|
|
|
tokenUses = 0
|
|
|
|
tokenMaxUses = 230
|
|
|
|
tokenUpdated: Time
|
|
|
|
tokenLifetime = initDuration(minutes=20)
|
|
|
|
|
|
|
|
macro genMediaGet(media: untyped; token=false) =
|
|
|
|
let
|
|
|
|
mediaName = capitalizeAscii($media)
|
|
|
|
multi = ident("get" & mediaName & "s")
|
|
|
|
convo = ident("getConversation" & mediaName & "s")
|
|
|
|
single = ident("get" & mediaName)
|
|
|
|
|
|
|
|
quote do:
|
|
|
|
proc `multi`*(thread: Thread | Timeline; agent: string; token="") {.async.} =
|
|
|
|
if thread == nil: return
|
|
|
|
var `media` = thread.content.filterIt(it.`media`.isSome)
|
|
|
|
when `token`:
|
|
|
|
var gToken = token
|
|
|
|
if gToken.len == 0: gToken = await getGuestToken(agent)
|
|
|
|
await all(`media`.mapIt(`single`(it, token, agent)))
|
|
|
|
else:
|
|
|
|
await all(`media`.mapIt(`single`(it, agent)))
|
|
|
|
|
|
|
|
proc `convo`*(convo: Conversation; agent: string) {.async.} =
|
|
|
|
var futs: seq[Future[void]]
|
|
|
|
when `token`:
|
|
|
|
var token = await getGuestToken(agent)
|
|
|
|
futs.add `single`(convo.tweet, agent, token)
|
|
|
|
futs.add `multi`(convo.before, agent, token=token)
|
|
|
|
futs.add `multi`(convo.after, agent, token=token)
|
2019-09-24 13:39:04 +00:00
|
|
|
futs.add convo.replies.content.mapIt(`multi`(it, agent, token=token))
|
2019-09-06 01:37:12 +00:00
|
|
|
else:
|
|
|
|
futs.add `single`(convo.tweet, agent)
|
|
|
|
futs.add `multi`(convo.before, agent)
|
|
|
|
futs.add `multi`(convo.after, agent)
|
2019-09-24 13:39:04 +00:00
|
|
|
futs.add convo.replies.content.mapIt(`multi`(it, agent))
|
2019-09-06 01:37:12 +00:00
|
|
|
await all(futs)
|
|
|
|
|
|
|
|
proc getGuestToken(agent: string; force=false): Future[string] {.async.} =
|
|
|
|
if getTime() - tokenUpdated < tokenLifetime and
|
|
|
|
not force and tokenUses < tokenMaxUses:
|
|
|
|
return guestToken
|
|
|
|
|
|
|
|
tokenUpdated = getTime()
|
|
|
|
tokenUses = 0
|
|
|
|
|
|
|
|
let
|
2019-10-02 08:13:17 +00:00
|
|
|
headers = genHeaders({"authorization": auth}, agent, base, lang=false)
|
2019-10-02 20:28:53 +00:00
|
|
|
json = await fetchJson(apiBase / tokenUrl, headers)
|
2019-09-06 01:37:12 +00:00
|
|
|
|
2019-10-02 08:13:17 +00:00
|
|
|
if json != nil:
|
|
|
|
result = json["guest_token"].to(string)
|
|
|
|
guestToken = result
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
proc getVideoFetch(tweet: Tweet; agent, token: string) {.async.} =
|
|
|
|
if tweet.video.isNone(): return
|
|
|
|
|
2019-10-02 08:13:17 +00:00
|
|
|
let
|
|
|
|
headers = genHeaders({"authorization": auth, "x-guest-token": token},
|
|
|
|
agent, base / getLink(tweet), lang=false)
|
|
|
|
url = apiBase / (videoUrl % tweet.id)
|
|
|
|
json = await fetchJson(url, headers)
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
if json == nil:
|
|
|
|
if getTime() - tokenUpdated > initDuration(seconds=1):
|
|
|
|
tokenUpdated = getTime()
|
|
|
|
discard await getGuestToken(agent, force=true)
|
|
|
|
await getVideoFetch(tweet, agent, guestToken)
|
|
|
|
return
|
|
|
|
|
|
|
|
if tweet.card.isNone:
|
2019-09-18 18:54:07 +00:00
|
|
|
tweet.video = some parseVideo(json, tweet.id)
|
2019-09-06 01:37:12 +00:00
|
|
|
else:
|
2019-09-18 18:54:07 +00:00
|
|
|
get(tweet.card).video = some parseVideo(json, tweet.id)
|
|
|
|
tweet.video = none Video
|
2019-09-06 01:37:12 +00:00
|
|
|
tokenUses.inc
|
|
|
|
|
|
|
|
proc getVideoVar(tweet: Tweet): var Option[Video] =
|
|
|
|
if tweet.card.isSome():
|
|
|
|
return get(tweet.card).video
|
|
|
|
else:
|
|
|
|
return tweet.video
|
|
|
|
|
|
|
|
proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} =
|
2019-09-08 10:22:52 +00:00
|
|
|
withCustomDb("cache.db", "", "", ""):
|
2019-09-06 01:37:12 +00:00
|
|
|
try:
|
2019-09-18 18:54:07 +00:00
|
|
|
getVideoVar(tweet) = some Video.getOne("videoId = ?", tweet.id)
|
2019-09-06 01:37:12 +00:00
|
|
|
except KeyError:
|
|
|
|
await getVideoFetch(tweet, agent, token)
|
|
|
|
var video = getVideoVar(tweet)
|
|
|
|
if video.isSome():
|
|
|
|
get(video).insert()
|
|
|
|
|
|
|
|
proc getPoll*(tweet: Tweet; agent: string) {.async.} =
|
|
|
|
if tweet.poll.isNone(): return
|
|
|
|
|
2019-10-02 08:13:17 +00:00
|
|
|
let
|
|
|
|
headers = genHeaders(agent, base / getLink(tweet), auth=true)
|
|
|
|
url = base / (pollUrl % tweet.id)
|
|
|
|
html = await fetchHtml(url, headers)
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
if html == nil: return
|
2019-09-18 18:54:07 +00:00
|
|
|
tweet.poll = some parsePoll(html)
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
proc getCard*(tweet: Tweet; agent: string) {.async.} =
|
|
|
|
if tweet.card.isNone(): return
|
|
|
|
|
2019-10-02 08:13:17 +00:00
|
|
|
let
|
|
|
|
headers = genHeaders(agent, base / getLink(tweet), auth=true)
|
|
|
|
query = get(tweet.card).query.replace("sensitive=true", "sensitive=false")
|
|
|
|
html = await fetchHtml(base / query, headers)
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
if html == nil: return
|
|
|
|
parseCard(get(tweet.card), html)
|
|
|
|
|
|
|
|
proc getPhotoRail*(username, agent: string): Future[seq[GalleryPhoto]] {.async.} =
|
2019-10-02 08:13:17 +00:00
|
|
|
let
|
|
|
|
headers = genHeaders({"x-requested-with": "XMLHttpRequest"}, agent, base / username)
|
|
|
|
params = {"for_photo_rail": "true", "oldest_unread_id": "0"}
|
|
|
|
url = base / (timelineMediaUrl % username) ? params
|
|
|
|
html = await fetchHtml(url, headers, jsonKey="items_html")
|
2019-09-06 01:37:12 +00:00
|
|
|
|
|
|
|
result = parsePhotoRail(html)
|
|
|
|
|
|
|
|
genMediaGet(video, token=true)
|
|
|
|
genMediaGet(poll)
|
|
|
|
genMediaGet(card)
|