nitter/src/nitter.nim

201 lines
6.4 KiB
Nim
Raw Normal View History

import asyncdispatch, asyncfile, httpclient, uri, os
import sequtils, strformat, strutils
2019-07-31 00:15:43 +00:00
from net import Port
2019-06-20 14:16:20 +00:00
2019-07-31 00:15:43 +00:00
import jester, regex
2019-06-24 21:25:21 +00:00
2019-08-13 17:44:29 +00:00
import api, utils, types, cache, formatters, search, config, prefs, agents
import views/[general, profile, status, preferences]
2019-06-20 14:16:20 +00:00
2019-07-31 00:15:43 +00:00
const configPath {.strdefine.} = "./nitter.conf"
let cfg = getConfig(configPath)
2019-08-13 17:44:29 +00:00
proc showSingleTimeline(name, after, agent: string; query: Option[Query];
prefs: Prefs): Future[string] {.async.} =
2019-08-06 15:41:06 +00:00
let railFut = getPhotoRail(name, agent)
var timeline: Timeline
var profile: Profile
var cachedProfile = hasCachedProfile(name)
if cachedProfile.isSome:
profile = get(cachedProfile)
if query.isNone:
if cachedProfile.isSome:
timeline = await getTimeline(name, after, agent)
else:
(profile, timeline) = await getProfileAndTimeline(name, agent, after)
cache(profile)
else:
var timelineFut = getTimelineSearch(get(query), after, agent)
if cachedProfile.isNone:
profile = await getCachedProfile(name, agent)
timeline = await timelineFut
2019-06-20 14:16:20 +00:00
if profile.username.len == 0:
2019-06-20 14:16:20 +00:00
return ""
2019-08-13 17:44:29 +00:00
let profileHtml = renderProfile(profile, timeline, await railFut, prefs)
2019-08-19 18:25:00 +00:00
return renderMain(profileHtml, prefs, cfg.title, pageTitle(profile), pageDesc(profile))
2019-06-20 14:16:20 +00:00
2019-08-13 17:44:29 +00:00
proc showMultiTimeline(names: seq[string]; after, agent: string; query: Option[Query];
prefs: Prefs): Future[string] {.async.} =
2019-08-06 15:41:06 +00:00
var q = query
if q.isSome:
get(q).fromUser = names
else:
q = some(Query(kind: multi, fromUser: names, excludes: @["replies"]))
2019-08-13 17:44:29 +00:00
var timeline = renderMulti(await getTimelineSearch(get(q), after, agent),
names.join(","), prefs)
2019-08-06 15:41:06 +00:00
2019-08-19 18:25:00 +00:00
return renderMain(timeline, prefs, cfg.title, "Multi")
2019-08-13 17:44:29 +00:00
proc showTimeline(name, after: string; query: Option[Query];
prefs: Prefs): Future[string] {.async.} =
2019-08-06 15:41:06 +00:00
let agent = getAgent()
2019-08-06 17:02:38 +00:00
let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
2019-08-06 15:41:06 +00:00
if names.len == 1:
2019-08-13 17:44:29 +00:00
return await showSingleTimeline(names[0], after, agent, query, prefs)
2019-08-06 15:41:06 +00:00
else:
2019-08-13 17:44:29 +00:00
return await showMultiTimeline(names, after, agent, query, prefs)
2019-08-06 15:41:06 +00:00
template respTimeline(timeline: typed) =
if timeline.len == 0:
2019-08-15 21:17:13 +00:00
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
resp timeline
2019-08-15 21:17:13 +00:00
template cookiePrefs(): untyped {.dirty.} =
2019-08-13 17:44:29 +00:00
getPrefs(request.cookies.getOrDefault("preferences"))
2019-07-31 00:15:43 +00:00
setProfileCacheTime(cfg.profileCacheTime)
settings:
port = Port(cfg.port)
staticDir = cfg.staticDir
bindAddr = cfg.address
2019-06-20 14:16:20 +00:00
routes:
get "/":
2019-08-19 18:25:00 +00:00
resp renderMain(renderSearch(), Prefs(), cfg.title)
2019-06-20 14:16:20 +00:00
post "/search":
if @"query".len == 0:
2019-08-15 21:17:13 +00:00
resp Http404, showError("Please enter a username.", cfg.title)
2019-06-20 14:16:20 +00:00
redirect("/" & @"query")
2019-08-13 17:44:29 +00:00
post "/saveprefs":
2019-08-15 21:17:13 +00:00
var prefs = cookiePrefs()
2019-08-13 17:44:29 +00:00
genUpdatePrefs()
setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps)
redirect(decodeUrl(@"referer"))
2019-08-13 17:44:29 +00:00
2019-08-15 17:13:54 +00:00
post "/resetprefs":
2019-08-15 21:17:13 +00:00
var prefs = cookiePrefs()
2019-08-15 17:13:54 +00:00
resetPrefs(prefs)
setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps)
2019-08-15 17:13:54 +00:00
redirect("/settings")
2019-08-13 17:44:29 +00:00
get "/settings":
let refUri = request.headers.getOrDefault("Referer").parseUri()
var path =
if refUri.path.len > 0 and "/settings" notin refUri.path: refUri.path
else: "/"
if refUri.query.len > 0: path &= &"?{refUri.query}"
2019-08-19 18:25:00 +00:00
let prefs = cookiePrefs()
resp renderMain(renderPreferences(prefs, path), prefs, cfg.title, "Preferences")
2019-08-13 17:44:29 +00:00
2019-06-20 14:16:20 +00:00
get "/@name/?":
cond '.' notin @"name"
2019-08-15 21:17:13 +00:00
respTimeline(await showTimeline(@"name", @"after", none(Query), cookiePrefs()))
2019-07-04 09:55:19 +00:00
get "/@name/search":
cond '.' notin @"name"
2019-08-15 21:17:13 +00:00
let prefs = cookiePrefs()
2019-07-04 09:55:19 +00:00
let query = initQuery(@"filter", @"include", @"not", @"sep", @"name")
2019-08-15 21:17:13 +00:00
respTimeline(await showTimeline(@"name", @"after", some(query), cookiePrefs()))
2019-06-20 14:16:20 +00:00
get "/@name/replies":
cond '.' notin @"name"
2019-08-15 21:17:13 +00:00
let prefs = cookiePrefs()
respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")), cookiePrefs()))
get "/@name/media":
cond '.' notin @"name"
2019-08-15 21:17:13 +00:00
let prefs = cookiePrefs()
respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")), cookiePrefs()))
2019-06-20 14:16:20 +00:00
get "/@name/status/@id":
cond '.' notin @"name"
2019-08-15 21:17:13 +00:00
let prefs = cookiePrefs()
2019-07-31 06:36:24 +00:00
let conversation = await getTweet(@"name", @"id", getAgent())
2019-06-27 19:07:29 +00:00
if conversation == nil or conversation.tweet.id.len == 0:
2019-08-15 21:17:13 +00:00
resp Http404, showError("Tweet not found", cfg.title)
2019-06-20 14:16:20 +00:00
2019-06-24 20:40:48 +00:00
let title = pageTitle(conversation.tweet.profile)
2019-08-07 20:02:19 +00:00
let desc = conversation.tweet.text
2019-08-13 17:44:29 +00:00
let html = renderConversation(conversation, prefs)
2019-08-07 20:02:19 +00:00
if conversation.tweet.video.isSome():
let thumb = get(conversation.tweet.video).thumb
2019-08-07 20:27:24 +00:00
let vidUrl = getVideoEmbed(conversation.tweet.id)
2019-08-19 18:25:00 +00:00
resp renderMain(html, prefs, cfg.title, title, desc, images = @[thumb],
2019-08-15 21:17:13 +00:00
`type`="video", video=vidUrl)
2019-08-07 20:27:24 +00:00
elif conversation.tweet.gif.isSome():
let thumb = get(conversation.tweet.gif).thumb
let vidUrl = getVideoEmbed(conversation.tweet.id)
2019-08-19 18:25:00 +00:00
resp renderMain(html, prefs, cfg.title, title, desc, images = @[thumb],
2019-08-15 21:17:13 +00:00
`type`="video", video=vidUrl)
2019-08-07 20:02:19 +00:00
else:
2019-08-19 18:25:00 +00:00
resp renderMain(html, prefs, cfg.title, title, desc, images=conversation.tweet.photos)
2019-06-20 14:16:20 +00:00
get "/pic/@sig/@url":
cond "http" in @"url"
cond "twimg" in @"url"
let
uri = parseUri(decodeUrl(@"url"))
path = uri.path.split("/")[2 .. ^1].join("/")
2019-07-31 00:15:43 +00:00
filename = cfg.cacheDir / cleanFilename(path & uri.query)
2019-06-20 14:16:20 +00:00
if getHmac($uri) != @"sig":
2019-08-15 21:17:13 +00:00
resp showError("Failed to verify signature", cfg.title)
2019-06-20 14:16:20 +00:00
2019-07-31 00:15:43 +00:00
if not existsDir(cfg.cacheDir):
createDir(cfg.cacheDir)
2019-06-20 14:16:20 +00:00
if not existsFile(filename):
let client = newAsyncHttpClient()
await client.downloadFile($uri, filename)
client.close()
2019-06-21 00:30:57 +00:00
2019-06-25 13:09:43 +00:00
if not existsFile(filename):
resp Http404
let file = openAsync(filename)
2019-08-07 18:58:17 +00:00
let buf = await readAll(file)
file.close()
2019-07-31 00:15:43 +00:00
2019-08-07 18:58:17 +00:00
resp buf, mimetype(filename)
2019-06-20 14:16:20 +00:00
get "/video/@sig/@url":
cond "http" in @"url"
2019-08-19 18:53:47 +00:00
var url = decodeUrl(@"url")
let prefs = cookiePrefs()
2019-06-20 14:16:20 +00:00
if getHmac(url) != @"sig":
2019-08-15 21:17:13 +00:00
resp showError("Failed to verify signature", cfg.title)
2019-06-20 14:16:20 +00:00
2019-08-13 17:45:02 +00:00
let client = newAsyncHttpClient()
2019-08-19 18:53:47 +00:00
var content = await client.getContent(url)
2019-06-20 14:16:20 +00:00
2019-08-19 18:53:47 +00:00
if ".m3u8" in url:
content = proxifyVideo(content, prefs.proxyVideos)
client.close()
resp content, mimetype(url)
2019-06-20 14:16:20 +00:00
runForever()