-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
971 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,85 @@ | ||
import api/[profile, timeline, tweet, search, media, list, resolver] | ||
export profile, timeline, tweet, search, media, list, resolver | ||
import asyncdispatch, httpclient, uri, json, strutils, options | ||
import types, query, formatters, consts, apiutils, parser | ||
|
||
proc getGraphProfile*(username: string): Future[Profile] {.async.} = | ||
let | ||
variables = %*{"screen_name": username, "withHighlightedLabel": true} | ||
js = await fetch(graphUser ? {"variables": $variables}) | ||
result = parseGraphProfile(js, username) | ||
|
||
proc getGraphList*(name, list: string): Future[List] {.async.} = | ||
let | ||
variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false} | ||
js = await fetch(graphList ? {"variables": $variables}) | ||
result = parseGraphList(js) | ||
|
||
proc getGraphListById*(id: string): Future[List] {.async.} = | ||
let | ||
variables = %*{"listId": id, "withHighlightedLabel": false} | ||
js = await fetch(graphListId ? {"variables": $variables}) | ||
result = parseGraphList(js) | ||
|
||
proc getListTimeline*(id: string; after=""): Future[Timeline] {.async.} = | ||
let | ||
ps = genParams({"list_id": id, "ranking_mode": "reverse_chronological"}, after) | ||
url = listTimeline ? ps | ||
result = parseTimeline(await fetch(url), after) | ||
|
||
proc getListMembers*(list: List; after=""): Future[Result[Profile]] {.async.} = | ||
if list.id.len == 0: return | ||
let | ||
ps = genParams({"list_id": list.id}, after) | ||
url = listMembers ? ps | ||
result = parseListMembers(await fetch(url, oldApi=true), after) | ||
|
||
proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} = | ||
let | ||
ps = genParams({"userId": id, "include_tweet_replies": $replies}, after) | ||
url = timeline / (id & ".json") ? ps | ||
result = parseTimeline(await fetch(url), after) | ||
|
||
proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} = | ||
let url = mediaTimeline / (id & ".json") ? genParams(cursor=after) | ||
result = parseTimeline(await fetch(url), after) | ||
|
||
proc getPhotoRail*(id: string): Future[PhotoRail] {.async.} = | ||
result = parsePhotoRail(await getMediaTimeline(id)) | ||
|
||
proc getSearch*[T](query: Query; after=""): Future[Result[T]] {.async.} = | ||
when T is Profile: | ||
const | ||
searchMode = ("result_filter", "user") | ||
parse = parseUsers | ||
else: | ||
const | ||
searchMode = ("tweet_search_mode", "live") | ||
parse = parseTimeline | ||
|
||
let | ||
q = genQueryParam(query) | ||
url = search ? genParams(searchParams & @[("q", q), searchMode], after) | ||
result = parse(await fetch(url), after) | ||
result.query = query | ||
|
||
proc getTweetImpl(id: string; after=""): Future[Conversation] {.async.} = | ||
let url = tweet / (id & ".json") ? genParams(cursor=after) | ||
result = parseConversation(await fetch(url), id) | ||
|
||
proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} = | ||
result = (await getTweetImpl(id, after)).replies | ||
result.beginning = after.len == 0 | ||
|
||
proc getTweet*(id: string; after=""): Future[Conversation] {.async.} = | ||
result = await getTweetImpl(id) | ||
if after.len > 0: | ||
result.replies = await getReplies(id, after) | ||
|
||
proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} = | ||
let client = newAsyncHttpClient(maxRedirects=0) | ||
try: | ||
let resp = await client.request(url, $HttpHead) | ||
result = resp.headers["location"].replaceUrl(prefs) | ||
except: | ||
discard | ||
finally: | ||
client.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import httpclient, asyncdispatch, options, times, strutils, json, uri | ||
import types, agents, tokens, consts | ||
|
||
proc genParams*(pars: openarray[(string, string)] = @[]; | ||
cursor=""): seq[(string, string)] = | ||
result = timelineParams | ||
for p in pars: | ||
result &= p | ||
if cursor.len > 0: | ||
result &= ("cursor", cursor) | ||
|
||
proc genHeaders*(token: Token): HttpHeaders = | ||
result = newHttpHeaders({ | ||
"DNT": "1", | ||
"authorization": auth, | ||
"content-type": "application/json", | ||
"user-agent": getAgent(), | ||
"x-guest-token": if token == nil: "" else: token.tok, | ||
"x-twitter-active-user": "yes", | ||
"authority": "api.twitter.com", | ||
"accept-language": "en-US,en;q=0.9", | ||
"accept": "*/*", | ||
}) | ||
|
||
proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} = | ||
var | ||
token = await getToken() | ||
keepToken = true | ||
proxy: Proxy = when defined(proxy): newProxy(prox) else: nil | ||
client = newAsyncHttpClient(proxy=proxy, headers=genHeaders(token)) | ||
|
||
try: | ||
let | ||
resp = await client.get($url) | ||
body = await resp.body | ||
|
||
const rl = "x-rate-limit-" | ||
if not oldApi and resp.headers.hasKey(rl & "limit"): | ||
token.limit = parseInt(resp.headers[rl & "limit"]) | ||
token.remaining = parseInt(resp.headers[rl & "remaining"]) | ||
token.reset = fromUnix(parseInt(resp.headers[rl & "reset"])) | ||
|
||
if resp.status != $Http200: | ||
if "Bad guest token" in body: | ||
return | ||
elif not body.startsWith('{'): | ||
echo resp.status, " ", body | ||
return | ||
|
||
result = parseJson(body) | ||
|
||
if result{"errors"} != nil and result{"errors"}[0]{"code"}.getInt == 200: | ||
keepToken = false | ||
echo "bad token" | ||
except: | ||
echo "error: ", url | ||
return nil | ||
finally: | ||
if keepToken: | ||
token.release() | ||
|
||
try: client.close() | ||
except: discard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import uri, sequtils | ||
|
||
const | ||
auth* = "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" | ||
|
||
api = parseUri("https://api.twitter.com") | ||
graphql = api / "graphql" | ||
timelineApi = api / "2/timeline" | ||
graphUser* = graphql / "E4iSsd6gypGFWx2eUhSC1g/UserByScreenName" | ||
graphList* = graphql / "ErWsz9cObLel1BF-HjuBlA/ListBySlug" | ||
graphListId* = graphql / "JADTh6cjebfgetzvF3tQvQ/List" | ||
timeline* = timelineApi / "profile" | ||
mediaTimeline* = timelineApi / "media" | ||
listTimeline* = timelineApi / "list.json" | ||
listMembers* = api / "1.1/lists/members.json" | ||
tweet* = timelineApi / "conversation" | ||
search* = api / "2/search/adaptive.json" | ||
|
||
timelineParams* = { | ||
"include_profile_interstitial_type": "0", | ||
"include_blocking": "0", | ||
"include_blocked_by": "0", | ||
"include_followed_by": "1", | ||
"include_want_retweets": "0", | ||
"include_mute_edge": "0", | ||
"include_can_dm": "0", | ||
"include_can_media_tag": "1", | ||
"skip_status": "1", | ||
"cards_platform": "Web-12", | ||
"include_cards": "1", | ||
"include_composer_source": "false", | ||
"include_ext_alt_text": "true", | ||
"include_reply_count": "1", | ||
"tweet_mode": "extended", | ||
"include_entities": "true", | ||
"include_user_entities": "true", | ||
"include_ext_media_color": "false", | ||
"include_ext_media_availability": "true", | ||
"send_error_codes": "true", | ||
"simple_quoted_tweet": "true", | ||
"count": "20", | ||
"ext": "mediaStats,highlightedLabel,cameraMoment", | ||
"include_quote_count": "true" | ||
}.toSeq | ||
|
||
searchParams* = { | ||
"query_source": "typed_query", | ||
"pc": "1", | ||
"spelling_corrections": "1" | ||
}.toSeq | ||
## top: nothing | ||
## latest: "tweet_search_mode: live" | ||
## user: "result_filter: user" | ||
## photos: "result_filter: photos" | ||
## videos: "result_filter: videos" |
Oops, something went wrong.