nitter/src/api.nim

389 lines
11 KiB
Nim
Raw Normal View History

2019-06-20 14:16:20 +00:00
import httpclient, asyncdispatch, htmlparser, times
import sequtils, strutils, json, xmltree, uri
2019-06-20 14:16:20 +00:00
import types, parser, parserutils, formatters, search
2019-06-20 14:16:20 +00:00
2019-06-21 01:51:14 +00:00
const
2019-06-29 12:11:23 +00:00
lang = "en-US,en;q=0.9"
2019-06-24 03:14:14 +00:00
auth = "Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw"
accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"
jsonAccept = "application/json, text/javascript, */*; q=0.01"
2019-06-24 03:14:14 +00:00
base = parseUri("https://twitter.com/")
apiBase = parseUri("https://api.twitter.com/1.1/")
timelineUrl = "i/profiles/show/$1/timeline/tweets"
2019-07-04 02:18:32 +00:00
timelineMediaUrl = "i/profiles/show/$1/media_timeline"
2019-06-21 01:51:14 +00:00
profilePopupUrl = "i/profiles/popup"
profileIntentUrl = "intent/user"
2019-08-23 00:15:25 +00:00
searchUrl = "i/search/timeline"
2019-06-27 18:13:46 +00:00
tweetUrl = "status"
2019-06-24 03:14:14 +00:00
videoUrl = "videos/tweet/config/$1.json"
tokenUrl = "guest/activate.json"
2019-06-29 12:11:23 +00:00
cardUrl = "i/cards/tfw/v1/$1"
pollUrl = cardUrl & "?cardname=poll2choice_text_only&lang=en"
2019-06-21 01:51:14 +00:00
2019-06-24 03:29:47 +00:00
var
guestToken = ""
tokenUses = 0
tokenMaxUses = 230
2019-06-24 03:29:47 +00:00
tokenUpdated: Time
tokenLifetime = initDuration(minutes=20)
2019-06-24 03:29:47 +00:00
2019-07-31 07:27:37 +00:00
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.} =
2019-07-31 07:27:37 +00:00
if thread == nil: return
2019-08-23 00:15:25 +00:00
var `media` = thread.content.filterIt(it.`media`.isSome)
2019-07-31 07:27:37 +00:00
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)
2019-08-13 17:45:19 +00:00
futs.add `single`(convo.tweet, agent, token)
futs.add `multi`(convo.before, agent, token=token)
futs.add `multi`(convo.after, agent, token=token)
futs.add convo.replies.mapIt(`multi`(it, agent, token=token))
2019-07-31 07:27:37 +00:00
else:
futs.add `single`(convo.tweet, agent)
futs.add `multi`(convo.before, agent)
futs.add `multi`(convo.after, agent)
futs.add convo.replies.mapIt(`multi`(it, agent))
await all(futs)
2019-06-24 03:29:47 +00:00
template newClient() {.dirty.} =
2019-06-21 01:51:14 +00:00
var client = newAsyncHttpClient()
defer: client.close()
client.headers = headers
2019-06-20 14:16:20 +00:00
2019-06-24 03:29:47 +00:00
proc fetchHtml(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {.async.} =
newClient()
2019-06-21 00:15:46 +00:00
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)
2019-06-24 03:14:14 +00:00
proc fetchJson(url: Uri; headers: HttpHeaders): Future[JsonNode] {.async.} =
2019-06-24 03:29:47 +00:00
newClient()
2019-06-24 03:14:14 +00:00
var resp = ""
try:
resp = await client.getContent($url)
2019-06-25 11:18:44 +00:00
result = parseJson(resp)
2019-06-24 03:14:14 +00:00
except:
return nil
2019-07-31 06:36:24 +00:00
proc getGuestToken(agent: string; force=false): Future[string] {.async.} =
if getTime() - tokenUpdated < tokenLifetime and
not force and tokenUses < tokenMaxUses:
return guestToken
2019-06-21 00:15:46 +00:00
2019-06-24 03:29:47 +00:00
tokenUpdated = getTime()
tokenUses = 0
2019-06-20 14:16:20 +00:00
2019-06-24 03:14:14 +00:00
let headers = newHttpHeaders({
"Accept": jsonAccept,
2019-06-24 03:14:14 +00:00
"Referer": $base,
"User-Agent": agent,
"Authorization": auth
})
2019-06-24 03:29:47 +00:00
newClient()
2019-06-24 03:14:14 +00:00
let
2019-06-29 12:11:23 +00:00
url = apiBase / tokenUrl
2019-06-24 03:14:14 +00:00
json = parseJson(await client.postContent($url))
result = json["guest_token"].to(string)
guestToken = result
2019-06-24 03:14:14 +00:00
2019-08-13 17:45:19 +00:00
proc getVideoFetch*(tweet: Tweet; agent, token: string) {.async.} =
2019-06-25 00:38:18 +00:00
if tweet.video.isNone(): return
2019-06-24 03:29:47 +00:00
2019-06-24 03:14:14 +00:00
let headers = newHttpHeaders({
"Accept": jsonAccept,
2019-07-01 21:14:36 +00:00
"Referer": $(base / getLink(tweet)),
2019-06-24 03:14:14 +00:00
"User-Agent": agent,
"Authorization": auth,
"x-guest-token": token
})
let url = apiBase / (videoUrl % tweet.id)
let json = await fetchJson(url, headers)
2019-06-24 03:14:14 +00:00
2019-06-27 19:07:29 +00:00
if json == nil:
if getTime() - tokenUpdated > initDuration(seconds=1):
tokenUpdated = getTime()
2019-07-31 06:36:24 +00:00
discard await getGuestToken(agent, force=true)
2019-08-13 17:45:19 +00:00
await getVideoFetch(tweet, agent, guestToken)
return
2019-07-15 14:03:01 +00:00
if tweet.card.isNone:
2019-08-06 17:02:38 +00:00
tweet.video = some(parseVideo(json, tweet.id))
2019-07-15 14:03:01 +00:00
else:
2019-08-06 17:02:38 +00:00
get(tweet.card).video = some(parseVideo(json, tweet.id))
2019-07-15 14:03:01 +00:00
tweet.video = none(Video)
tokenUses.inc
2019-06-24 03:14:14 +00:00
2019-08-06 17:02:38 +00:00
proc getVideoVar*(tweet: Tweet): var Option[Video] =
if tweet.card.isSome():
return get(tweet.card).video
else:
return tweet.video
2019-08-13 17:45:19 +00:00
proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} =
2019-08-06 17:02:38 +00:00
withDb:
try:
getVideoVar(tweet) = some(Video.getOne("videoId = ?", tweet.id))
except KeyError:
2019-08-13 17:45:19 +00:00
await getVideoFetch(tweet, agent, token)
2019-08-06 17:02:38 +00:00
var video = getVideoVar(tweet)
if video.isSome():
get(video).insert()
2019-07-31 06:36:24 +00:00
proc getPoll*(tweet: Tweet; agent: string) {.async.} =
2019-06-29 12:11:23 +00:00
if tweet.poll.isNone(): return
let headers = newHttpHeaders({
"Accept": accept,
2019-07-01 21:14:36 +00:00
"Referer": $(base / getLink(tweet)),
2019-06-29 12:11:23 +00:00
"User-Agent": agent,
"Authority": "twitter.com",
"Accept-Language": lang,
})
let url = base / (pollUrl % tweet.id)
let html = await fetchHtml(url, headers)
if html == nil: return
2019-06-24 03:29:47 +00:00
2019-06-29 12:11:23 +00:00
tweet.poll = some(parsePoll(html))
2019-07-31 06:36:24 +00:00
proc getCard*(tweet: Tweet; agent: string) {.async.} =
2019-07-11 17:22:23 +00:00
if tweet.card.isNone(): return
let headers = newHttpHeaders({
"Accept": accept,
2019-07-11 17:22:23 +00:00
"Referer": $(base / getLink(tweet)),
"User-Agent": agent,
"Authority": "twitter.com",
"Accept-Language": lang,
})
let query = get(tweet.card).query.replace("sensitive=true", "sensitive=false")
let html = await fetchHtml(base / query, headers)
2019-07-11 17:22:23 +00:00
if html == nil: return
parseCard(get(tweet.card), html)
2019-07-31 07:27:37 +00:00
genMediaGet(video, token=true)
genMediaGet(poll)
genMediaGet(card)
2019-07-11 17:22:23 +00:00
2019-07-31 06:36:24 +00:00
proc getPhotoRail*(username, agent: string): Future[seq[GalleryPhoto]] {.async.} =
2019-07-04 02:18:32 +00:00
let headers = newHttpHeaders({
"Accept": jsonAccept,
"Referer": $(base / username),
"User-Agent": agent,
"X-Requested-With": "XMLHttpRequest"
})
let params = {
"for_photo_rail": "true",
"oldest_unread_id": "0"
}
let url = base / (timelineMediaUrl % username) ? params
let html = await fetchHtml(url, headers, jsonKey="items_html")
result = parsePhotoRail(html)
2019-06-29 12:11:23 +00:00
proc getProfileFallback(username: string; headers: HttpHeaders): Future[Profile] {.async.} =
let url = base / profileIntentUrl ? {"screen_name": username}
let html = await fetchHtml(url, headers)
2019-06-27 19:07:29 +00:00
if html == nil: return Profile()
2019-06-24 22:55:41 +00:00
2019-06-24 03:29:47 +00:00
result = parseIntentProfile(html)
2019-07-31 06:36:24 +00:00
proc getProfile*(username, agent: string): Future[Profile] {.async.} =
2019-06-24 03:29:47 +00:00
let headers = newHttpHeaders({
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9",
"Referer": $(base / username),
"User-Agent": agent,
"X-Twitter-Active-User": "yes",
"X-Requested-With": "XMLHttpRequest",
2019-06-29 12:11:23 +00:00
"Accept-Language": lang
2019-06-24 03:29:47 +00:00
})
let
params = {
"screen_name": username,
"wants_hovercard": "true",
"_": $(epochTime().int)
}
url = base / profilePopupUrl ? params
html = await fetchHtml(url, headers, jsonKey="html")
2019-06-27 19:07:29 +00:00
if html == nil: return Profile()
2019-06-24 22:55:41 +00:00
2019-06-27 19:07:29 +00:00
if html.select(".ProfileCard-sensitiveWarningContainer") != nil:
2019-06-24 03:29:47 +00:00
return await getProfileFallback(username, headers)
result = parsePopupProfile(html)
2019-07-31 06:36:24 +00:00
proc getTweet*(username, id, agent: string): Future[Conversation] {.async.} =
2019-06-21 01:51:14 +00:00
let headers = newHttpHeaders({
"Accept": jsonAccept,
"Referer": $base,
2019-06-20 14:16:20 +00:00
"User-Agent": agent,
"X-Twitter-Active-User": "yes",
"X-Requested-With": "XMLHttpRequest",
"Accept-Language": lang,
"pragma": "no-cache",
"x-previous-page-name": "profile"
2019-06-20 14:16:20 +00:00
})
let
url = base / username / tweetUrl / id
html = await fetchHtml(url, headers)
if html == nil: return
result = parseConversation(html)
2019-07-11 17:22:23 +00:00
let
2019-07-31 06:36:24 +00:00
vidsFut = getConversationVideos(result, agent)
pollFut = getConversationPolls(result, agent)
cardFut = getConversationCards(result, agent)
2019-07-11 17:22:23 +00:00
await all(vidsFut, pollFut, cardFut)
2019-06-20 14:16:20 +00:00
2019-07-31 06:36:24 +00:00
proc finishTimeline(json: JsonNode; query: Option[Query]; after, agent: string): Future[Timeline] {.async.} =
2019-06-27 19:07:29 +00:00
if json == nil: return Timeline()
2019-06-20 14:16:20 +00:00
result = Timeline(
hasMore: json["has_more_items"].to(bool),
maxId: json.getOrDefault("max_position").getStr(""),
minId: json.getOrDefault("min_position").getStr("").cleanPos(),
query: query,
beginning: after.len == 0
)
if json["new_latent_count"].to(int) == 0: return
if not json.hasKey("items_html"): return
2019-07-01 01:13:12 +00:00
let
html = parseHtml(json["items_html"].to(string))
thread = parseThread(html)
2019-07-31 06:36:24 +00:00
vidsFut = getVideos(thread, agent)
pollFut = getPolls(thread, agent)
cardFut = getCards(thread, agent)
2019-06-29 12:11:23 +00:00
2019-07-11 17:22:23 +00:00
await all(vidsFut, pollFut, cardFut)
2019-08-23 00:15:25 +00:00
result.content = thread.content
2019-06-20 14:16:20 +00:00
2019-07-31 06:36:24 +00:00
proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
2019-06-21 01:51:14 +00:00
let headers = newHttpHeaders({
"Accept": jsonAccept,
"Referer": $(base / username),
2019-06-20 14:16:20 +00:00
"User-Agent": agent,
"X-Twitter-Active-User": "yes",
"X-Requested-With": "XMLHttpRequest",
"Accept-Language": lang
2019-06-20 14:16:20 +00:00
})
var params = toSeq({
"include_available_features": "1",
"include_entities": "1",
"include_new_items_bar": "false",
"reset_error_state": "false"
})
2019-06-20 14:16:20 +00:00
if after.len > 0:
params.add {"max_position": after}
let json = await fetchJson(base / (timelineUrl % username) ? params, headers)
2019-07-31 06:36:24 +00:00
result = await finishTimeline(json, none(Query), after, agent)
2019-06-29 12:11:23 +00:00
2019-08-06 15:41:06 +00:00
proc getTimelineSearch*(query: Query; after, agent: string): Future[Timeline] {.async.} =
let queryParam = genQueryParam(query)
let queryEncoded = encodeUrl(queryParam, usePlus=false)
let headers = newHttpHeaders({
"Accept": jsonAccept,
"Referer": $(base / ("search?f=tweets&vertical=default&q=$1&src=typd" % queryEncoded)),
"User-Agent": agent,
"X-Requested-With": "XMLHttpRequest",
"Authority": "twitter.com",
"Accept-Language": lang
})
let params = {
"f": "tweets",
"vertical": "default",
"q": queryParam,
"src": "typd",
"include_available_features": "1",
"include_entities": "1",
"max_position": if after.len > 0: genPos(after) else: "0",
"reset_error_state": "false"
}
2019-08-23 00:15:25 +00:00
let json = await fetchJson(base / searchUrl ? params, headers)
2019-07-31 06:36:24 +00:00
result = await finishTimeline(json, some(query), after, agent)
proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} =
let headers = newHttpHeaders({
"authority": "twitter.com",
"accept": accept,
"referer": "https://twitter.com/" & username,
"accept-language": lang
})
var url = base / username
if after.len > 0:
url = url ? {"max_position": after}
let
html = await fetchHtml(url, headers)
timeline = parseTimeline(html.select("#timeline > .stream-container"), after)
profile = parseTimelineProfile(html)
vidsFut = getVideos(timeline, agent)
pollFut = getPolls(timeline, agent)
cardFut = getCards(timeline, agent)
await all(vidsFut, pollFut, cardFut)
result = (profile, timeline)
proc getProfileFull*(username: string): Future[Profile] {.async.} =
let headers = newHttpHeaders({
"authority": "twitter.com",
"accept": accept,
"referer": "https://twitter.com/" & username,
"accept-language": lang
})
let html = await fetchHtml(base / username, headers)
if html == nil: return
result = parseTimelineProfile(html)