2019-06-24 20:40:48 +00:00
|
|
|
import asyncdispatch, asyncfile, httpclient, strutils, strformat, uri, os
|
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-07-31 06:36:24 +00:00
|
|
|
import api, utils, types, cache, formatters, search, config, agents
|
2019-07-10 22:42:31 +00:00
|
|
|
import views/[general, profile, status]
|
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-06-23 00:46:46 +00:00
|
|
|
|
2019-07-03 09:46:03 +00:00
|
|
|
proc showTimeline(name, after: string; query: Option[Query]): Future[string] {.async.} =
|
2019-06-20 14:16:20 +00:00
|
|
|
let
|
2019-07-31 06:36:24 +00:00
|
|
|
agent = getAgent()
|
2019-06-20 14:16:20 +00:00
|
|
|
username = name.strip(chars={'/'})
|
2019-07-31 06:36:24 +00:00
|
|
|
profileFut = getCachedProfile(username, agent)
|
|
|
|
railFut = getPhotoRail(username, agent)
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
var timelineFut: Future[Timeline]
|
|
|
|
if query.isNone:
|
2019-07-31 06:36:24 +00:00
|
|
|
timelineFut = getTimeline(username, after, agent)
|
2019-07-03 09:46:03 +00:00
|
|
|
else:
|
2019-07-31 06:36:24 +00:00
|
|
|
timelineFut = getTimelineSearch(username, after, agent, get(query))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
let profile = await profileFut
|
2019-06-21 00:16:10 +00:00
|
|
|
if profile.username.len == 0:
|
2019-06-20 14:16:20 +00:00
|
|
|
return ""
|
|
|
|
|
2019-07-10 22:42:31 +00:00
|
|
|
let profileHtml = renderProfile(profile, await timelineFut, await railFut)
|
2019-07-31 00:15:43 +00:00
|
|
|
return renderMain(profileHtml, title=cfg.title, titleText=pageTitle(profile))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-07-03 09:46:03 +00:00
|
|
|
template respTimeline(timeline: typed) =
|
|
|
|
if timeline.len == 0:
|
2019-07-31 00:15:43 +00:00
|
|
|
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
|
2019-07-03 09:46:03 +00:00
|
|
|
resp timeline
|
|
|
|
|
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-07-31 00:15:43 +00:00
|
|
|
resp renderMain(renderSearch(), title=cfg.title, titleText="Search")
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
post "/search":
|
|
|
|
if @"query".len == 0:
|
2019-07-31 00:15:43 +00:00
|
|
|
resp Http404, showError("Please enter a username.", cfg.title)
|
2019-06-20 14:16:20 +00:00
|
|
|
redirect("/" & @"query")
|
|
|
|
|
|
|
|
get "/@name/?":
|
|
|
|
cond '.' notin @"name"
|
2019-07-03 09:46:03 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", none(Query)))
|
2019-06-23 00:46:46 +00:00
|
|
|
|
2019-07-04 09:55:19 +00:00
|
|
|
get "/@name/search":
|
2019-07-03 09:46:03 +00:00
|
|
|
cond '.' notin @"name"
|
2019-07-04 09:55:19 +00:00
|
|
|
let query = initQuery(@"filter", @"include", @"not", @"sep", @"name")
|
2019-07-03 09:46:03 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", some(query)))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-07-03 09:46:03 +00:00
|
|
|
get "/@name/replies":
|
|
|
|
cond '.' notin @"name"
|
|
|
|
respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name"))))
|
|
|
|
|
|
|
|
get "/@name/media":
|
|
|
|
cond '.' notin @"name"
|
|
|
|
respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name"))))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
get "/@name/status/@id":
|
|
|
|
cond '.' notin @"name"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
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-07-31 00:15:43 +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-07-31 01:19:30 +00:00
|
|
|
resp renderMain(renderConversation(conversation), title=cfg.title, titleText=title)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
get "/pic/@sig/@url":
|
|
|
|
cond "http" in @"url"
|
|
|
|
cond "twimg" in @"url"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
|
|
|
let
|
2019-06-23 23:59:52 +00:00
|
|
|
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
|
|
|
|
2019-06-23 23:59:52 +00:00
|
|
|
if getHmac($uri) != @"sig":
|
2019-07-31 00:15:43 +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
|
|
|
|
2019-06-23 00:46:46 +00:00
|
|
|
if not existsFile(filename):
|
|
|
|
let client = newAsyncHttpClient()
|
2019-06-23 23:59:52 +00:00
|
|
|
await client.downloadFile($uri, filename)
|
2019-06-23 00:46:46 +00:00
|
|
|
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)
|
|
|
|
defer: file.close()
|
2019-07-31 00:15:43 +00:00
|
|
|
|
2019-06-25 13:09:43 +00:00
|
|
|
resp await readAll(file), mimetype(filename)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
get "/video/@sig/@url":
|
|
|
|
cond "http" in @"url"
|
|
|
|
cond "video.twimg" in @"url"
|
|
|
|
let url = decodeUrl(@"url")
|
|
|
|
|
|
|
|
if getHmac(url) != @"sig":
|
2019-07-31 00:15:43 +00:00
|
|
|
resp showError("Failed to verify signature", cfg.title)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
client = newAsyncHttpClient()
|
2019-06-23 00:46:46 +00:00
|
|
|
video = await client.getContent(url)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
defer: client.close()
|
2019-06-23 00:46:46 +00:00
|
|
|
resp video, mimetype(url)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
runForever()
|