このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
Nitter-mod/src/api.nim

104 行
3.7 KiB
Nim
Raw 通常表示 履歴

2021-12-27 10:37:38 +09:00
# SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, httpclient, uri, strutils
import packedjson
2020-06-01 09:16:24 +09:00
import types, query, formatters, consts, apiutils, parser
2021-10-02 17:13:56 +09:00
proc getGraphListBySlug*(name, list: string): Future[List] {.async.} =
2020-06-01 09:16:24 +09:00
let
variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false}
2022-01-06 06:48:45 +09:00
url = graphListBySlug ? {"variables": $variables}
result = parseGraphList(await fetch(url, Api.listBySlug))
2020-06-01 09:16:24 +09:00
2021-10-02 17:13:56 +09:00
proc getGraphList*(id: string): Future[List] {.async.} =
2020-06-01 09:16:24 +09:00
let
variables = %*{"listId": id, "withHighlightedLabel": false}
2022-01-06 06:48:45 +09:00
url = graphList ? {"variables": $variables}
result = parseGraphList(await fetch(url, Api.list))
2020-06-01 09:16:24 +09:00
proc getListTimeline*(id: string; after=""): Future[Timeline] {.async.} =
2021-12-29 16:03:00 +09:00
if id.len == 0: return
2020-06-01 09:16:24 +09:00
let
ps = genParams({"list_id": id, "ranking_mode": "reverse_chronological"}, after)
url = listTimeline ? ps
2022-01-06 06:48:45 +09:00
result = parseTimeline(await fetch(url, Api.timeline), after)
2020-06-01 09:16:24 +09:00
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
2022-01-06 06:48:45 +09:00
result = parseListMembers(await fetch(url, Api.listMembers), after)
2020-06-01 09:16:24 +09:00
proc getProfile*(username: string): Future[Profile] {.async.} =
let
ps = genParams({"screen_name": username})
2022-01-06 06:48:45 +09:00
js = await fetch(userShow ? ps, Api.userShow)
2022-01-06 06:17:14 +09:00
result = parseUserShow(js, username=username)
proc getProfileById*(userId: string): Future[Profile] {.async.} =
let
ps = genParams({"user_id": userId})
2022-01-06 06:48:45 +09:00
js = await fetch(userShow ? ps, Api.userShow)
2022-01-06 06:17:14 +09:00
result = parseUserShow(js, id=userId)
2020-06-01 09:16:24 +09:00
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
2022-01-06 06:48:45 +09:00
result = parseTimeline(await fetch(url, Api.timeline), after)
2020-06-01 09:16:24 +09:00
proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} =
let url = mediaTimeline / (id & ".json") ? genParams(cursor=after)
2022-01-06 06:48:45 +09:00
result = parseTimeline(await fetch(url, Api.timeline), after)
2020-06-01 09:16:24 +09:00
2020-06-17 07:20:34 +09:00
proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
let
ps = genParams({"screen_name": name, "trim_user": "true"},
count="18", ext=false)
url = photoRail ? ps
2022-01-06 06:48:45 +09:00
result = parsePhotoRail(await fetch(url, Api.photoRail))
2020-06-01 09:16:24 +09:00
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)
if q.len == 0 or q == emptyQuery:
return Result[T](beginning: true, query: query)
let url = search ? genParams(searchParams & @[("q", q), searchMode], after)
try:
2022-01-06 06:48:45 +09:00
result = parse(await fetch(url, Api.search), after)
result.query = query
except InternalError:
return Result[T](beginning: true, query: query)
2020-06-01 09:16:24 +09:00
proc getTweetImpl(id: string; after=""): Future[Conversation] {.async.} =
let url = tweet / (id & ".json") ? genParams(cursor=after)
2022-01-06 06:48:45 +09:00
result = parseConversation(await fetch(url, Api.tweet), id)
2020-06-01 09:16:24 +09:00
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:
2021-12-20 11:11:12 +09:00
let resp = await client.request(url, HttpHead)
2021-12-27 10:27:49 +09:00
result = resp.headers["location"].replaceUrls(prefs)
2020-06-01 09:16:24 +09:00
except:
discard
finally:
client.close()