nitter/src/nitter.nim

113 lines
3.1 KiB
Nim
Raw Normal View History

2019-06-24 20:40:48 +00:00
import asyncdispatch, asyncfile, httpclient, strutils, strformat, uri, os
import jester, regex
2019-06-20 14:16:20 +00:00
import api, utils, types, cache, formatters, search
2019-06-24 21:25:21 +00:00
include views/"user.nimf"
include views/"general.nimf"
2019-06-20 14:16:20 +00:00
const cacheDir {.strdefine.} = "/tmp/nitter"
proc showTimeline(name, after: string; query: Option[Query]): Future[string] {.async.} =
2019-06-20 14:16:20 +00:00
let
username = name.strip(chars={'/'})
2019-06-20 18:04:18 +00:00
profileFut = getCachedProfile(username)
var timelineFut: Future[Timeline]
if query.isNone:
timelineFut = getTimeline(username, after)
else:
timelineFut = getTimelineSearch(username, after, get(query))
2019-06-20 14:16:20 +00:00
let profile = await profileFut
if profile.username.len == 0:
2019-06-20 14:16:20 +00:00
return ""
let profileHtml = renderProfile(profile, await timelineFut, after.len == 0)
2019-06-24 20:40:48 +00:00
return renderMain(profileHtml, title=pageTitle(profile))
2019-06-20 14:16:20 +00:00
template respTimeline(timeline: typed) =
if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found")
resp timeline
2019-06-20 14:16:20 +00:00
routes:
get "/":
2019-06-24 20:40:48 +00:00
resp renderMain(renderSearchPanel(), title=pageTitle("Search"))
2019-06-20 14:16:20 +00:00
post "/search":
if @"query".len == 0:
resp Http404, showError("Please enter a username.")
redirect("/" & @"query")
get "/@name/?":
cond '.' notin @"name"
respTimeline(await showTimeline(@"name", @"after", none(Query)))
get "/@name/search/?":
cond '.' notin @"name"
let query = initQuery(@"filter", @"sep", @"name")
respTimeline(await showTimeline(@"name", @"after", some(query)))
2019-06-20 14:16:20 +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-27 18:13:46 +00:00
let conversation = await getTweet(@"name", @"id")
2019-06-27 19:07:29 +00:00
if conversation == nil or conversation.tweet.id.len == 0:
2019-06-20 14:16:20 +00:00
resp Http404, showError("Tweet not found")
2019-06-24 20:40:48 +00:00
let title = pageTitle(conversation.tweet.profile)
resp renderMain(renderConversation(conversation), title=title)
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("/")
filename = cacheDir / cleanFilename(path & uri.query)
2019-06-20 14:16:20 +00:00
if getHmac($uri) != @"sig":
2019-06-20 14:16:20 +00:00
resp showError("Failed to verify signature")
if not existsDir(cacheDir):
createDir(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)
defer: file.close()
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":
resp showError("Failed to verify signature")
let
client = newAsyncHttpClient()
video = await client.getContent(url)
2019-06-20 14:16:20 +00:00
defer: client.close()
resp video, mimetype(url)
2019-06-20 14:16:20 +00:00
runForever()