2019-06-23 00:46:46 +00:00
|
|
|
import asyncdispatch, asyncfile, httpclient, strutils, uri, os
|
|
|
|
import jester
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-20 18:04:18 +00:00
|
|
|
import api, utils, types, cache
|
2019-06-20 14:16:20 +00:00
|
|
|
import views/[user, general, conversation]
|
|
|
|
|
2019-06-23 00:46:46 +00:00
|
|
|
const cacheDir {.strdefine.} = "/tmp/nitter"
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
proc showTimeline(name: string; num=""): Future[string] {.async.} =
|
|
|
|
let
|
|
|
|
username = name.strip(chars={'/'})
|
2019-06-20 18:04:18 +00:00
|
|
|
profileFut = getCachedProfile(username)
|
2019-06-20 14:16:20 +00:00
|
|
|
tweetsFut = getTimeline(username, after=num)
|
|
|
|
|
|
|
|
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-06-21 00:16:10 +00:00
|
|
|
return renderMain(renderProfile(profile, await tweetsFut, num.len == 0))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
routes:
|
|
|
|
get "/":
|
|
|
|
resp renderMain(renderSearchPanel())
|
|
|
|
|
|
|
|
post "/search":
|
|
|
|
if @"query".len == 0:
|
|
|
|
resp Http404, showError("Please enter a username.")
|
|
|
|
|
|
|
|
redirect("/" & @"query")
|
|
|
|
|
|
|
|
get "/@name/?":
|
|
|
|
cond '.' notin @"name"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
let timeline = await showTimeline(@"name", @"after")
|
2019-06-21 00:16:10 +00:00
|
|
|
if timeline.len == 0:
|
2019-06-20 14:16:20 +00:00
|
|
|
resp Http404, showError("User \"" & @"name" & "\" not found")
|
|
|
|
|
|
|
|
resp timeline
|
|
|
|
|
|
|
|
get "/@name/status/@id":
|
|
|
|
cond '.' notin @"name"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
let conversation = await getTweet(@"id")
|
2019-06-21 00:16:10 +00:00
|
|
|
if conversation.tweet.id.len == 0:
|
2019-06-20 14:16:20 +00:00
|
|
|
resp Http404, showError("Tweet not found")
|
|
|
|
|
|
|
|
resp renderMain(renderConversation(conversation))
|
|
|
|
|
|
|
|
get "/pic/@sig/@url":
|
|
|
|
cond "http" in @"url"
|
|
|
|
cond "twimg" in @"url"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
url = decodeUrl(@"url")
|
|
|
|
path = parseUri(url).path.split("/")[2 .. ^1].join("/")
|
|
|
|
filename = cacheDir / cleanFilename(path)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
if getHmac(url) != @"sig":
|
|
|
|
resp showError("Failed to verify signature")
|
|
|
|
|
2019-06-23 00:46:46 +00:00
|
|
|
if not existsDir(cacheDir):
|
|
|
|
createDir(cacheDir)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-23 00:46:46 +00:00
|
|
|
if not existsFile(filename):
|
|
|
|
let client = newAsyncHttpClient()
|
|
|
|
await client.downloadFile(url, filename)
|
|
|
|
client.close()
|
2019-06-21 00:30:57 +00:00
|
|
|
|
2019-06-23 00:46:46 +00:00
|
|
|
sendFile(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()
|
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()
|