nitter/src/nitter.nim

89 lines
2.2 KiB
Nim
Raw Normal View History

2019-06-24 22:40:48 +02:00
import asyncdispatch, asyncfile, httpclient, strutils, strformat, uri, os
import jester
2019-06-20 16:16:20 +02:00
2019-06-24 22:40:48 +02:00
import api, utils, types, cache, formatters
2019-06-20 16:16:20 +02:00
import views/[user, general, conversation]
const cacheDir {.strdefine.} = "/tmp/nitter"
2019-06-20 16:16:20 +02:00
proc showTimeline(name: string; num=""): Future[string] {.async.} =
let
username = name.strip(chars={'/'})
2019-06-20 20:04:18 +02:00
profileFut = getCachedProfile(username)
2019-06-20 16:16:20 +02:00
tweetsFut = getTimeline(username, after=num)
let profile = await profileFut
if profile.username.len == 0:
2019-06-20 16:16:20 +02:00
return ""
2019-06-24 22:40:48 +02:00
let profileHtml = renderProfile(profile, await tweetsFut, num.len == 0)
return renderMain(profileHtml, title=pageTitle(profile))
2019-06-20 16:16:20 +02:00
routes:
get "/":
2019-06-24 22:40:48 +02:00
resp renderMain(renderSearchPanel(), title=pageTitle("Search"))
2019-06-20 16:16:20 +02:00
post "/search":
if @"query".len == 0:
resp Http404, showError("Please enter a username.")
redirect("/" & @"query")
get "/@name/?":
cond '.' notin @"name"
2019-06-20 16:16:20 +02:00
let timeline = await showTimeline(@"name", @"after")
if timeline.len == 0:
2019-06-20 16:16:20 +02:00
resp Http404, showError("User \"" & @"name" & "\" not found")
resp timeline
get "/@name/status/@id":
cond '.' notin @"name"
2019-06-20 16:16:20 +02:00
let conversation = await getTweet(@"id")
if conversation.tweet.id.len == 0:
2019-06-20 16:16:20 +02:00
resp Http404, showError("Tweet not found")
2019-06-24 22:40:48 +02:00
let title = pageTitle(conversation.tweet.profile)
resp renderMain(renderConversation(conversation), title=title)
2019-06-20 16:16:20 +02: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 16:16:20 +02:00
if getHmac($uri) != @"sig":
2019-06-20 16:16:20 +02:00
resp showError("Failed to verify signature")
if not existsDir(cacheDir):
createDir(cacheDir)
2019-06-20 16:16:20 +02:00
if not existsFile(filename):
let client = newAsyncHttpClient()
await client.downloadFile($uri, filename)
client.close()
2019-06-21 02:30:57 +02:00
sendFile(filename)
2019-06-20 16:16:20 +02: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 16:16:20 +02:00
defer: client.close()
resp video, mimetype(url)
2019-06-20 16:16:20 +02:00
runForever()