Add t.co and /cards link resolvers

For t.co links: https://t.co/.. -> nitter.net/t.co/..
For card links: https://cards.twitter.com/cards/.. -> nitter.net/cards/...
This commit is contained in:
Zed 2019-12-30 11:41:09 +01:00
parent d27ab68a90
commit 1aa1487402
5 changed files with 46 additions and 3 deletions

View File

@ -1,2 +1,2 @@
import api/[profile, timeline, tweet, search, media, list] import api/[profile, timeline, tweet, search, media, list, resolver]
export profile, timeline, tweet, search, media, list export profile, timeline, tweet, search, media, list, resolver

13
src/api/resolver.nim Normal file
View File

@ -0,0 +1,13 @@
import asyncdispatch, httpclient
import ".."/[formatters, types]
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()

View File

@ -9,6 +9,8 @@ from unicode import Rune, `$`
const const
ytRegex = re"([A-z.]+\.)?youtu(be.com|.be)" ytRegex = re"([A-z.]+\.)?youtu(be.com|.be)"
twRegex = re"(www.|mobile.)?twitter.com" twRegex = re"(www.|mobile.)?twitter.com"
cards = "https://cards.twitter.com/cards"
tco = "https://t.co"
nbsp = $Rune(0x000A0) nbsp = $Rune(0x000A0)
proc stripText*(text: string): string = proc stripText*(text: string): string =
@ -33,6 +35,8 @@ proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
result = result.replace(ytRegex, prefs.replaceYouTube) result = result.replace(ytRegex, prefs.replaceYouTube)
if prefs.replaceTwitter.len > 0: if prefs.replaceTwitter.len > 0:
result = result.replace(twRegex, prefs.replaceTwitter) result = result.replace(twRegex, prefs.replaceTwitter)
result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co")
result = result.replace(cards, "https://" & prefs.replaceTwitter & "/cards")
if absolute.len > 0: if absolute.len > 0:
result = result.replace("href=\"/", "href=\"https://" & absolute & "/") result = result.replace("href=\"/", "href=\"https://" & absolute & "/")

View File

@ -6,7 +6,8 @@ import jester
import types, config, prefs, formatters import types, config, prefs, formatters
import views/[general, about] import views/[general, about]
import routes/[ import routes/[
preferences, timeline, status, media, search, rss, list, unsupported, embed] preferences, timeline, status, media, search, rss, list,
unsupported, embed, resolver]
const configPath {.strdefine.} = "./nitter.conf" const configPath {.strdefine.} = "./nitter.conf"
let cfg = getConfig(configPath) let cfg = getConfig(configPath)
@ -14,6 +15,7 @@ let cfg = getConfig(configPath)
setHmacKey(cfg.hmacKey) setHmacKey(cfg.hmacKey)
createUnsupportedRouter(cfg) createUnsupportedRouter(cfg)
createResolverRouter(cfg)
createPrefRouter(cfg) createPrefRouter(cfg)
createTimelineRouter(cfg) createTimelineRouter(cfg)
createListRouter(cfg) createListRouter(cfg)
@ -51,6 +53,7 @@ routes:
extend unsupported, "" extend unsupported, ""
extend preferences, "" extend preferences, ""
extend resolver, ""
extend rss, "" extend rss, ""
extend search, "" extend search, ""
extend timeline, "" extend timeline, ""

23
src/routes/resolver.nim Normal file
View File

@ -0,0 +1,23 @@
import strutils
import jester
import router_utils
import ".."/[query, types, api, agents]
import ../views/general
template respResolved*(url, kind: string): untyped =
if url.len == 0:
resp showError("Invalid $1 link" % kind, cfg)
else:
redirect(url)
proc createResolverRouter*(cfg: Config) =
router resolver:
get "/cards/@card/@id":
let url = "https://cards.twitter.com/cards/$1/$2" % [@"card", @"id"]
respResolved(await resolve(url, cookiePrefs()), "card")
get "/t.co/@url":
let url = "https://t.co/" & @"url"
respResolved(await resolve(url, cookiePrefs()), "t.co")