nitter/src/nitter.nim

77 lines
1.8 KiB
Nim
Raw Normal View History

2019-06-20 14:16:20 +00:00
import asyncdispatch, httpclient, times, strutils, hashes, random, uri
import jester, regex
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]
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
if profile.username.len == 0:
2019-06-20 14:16:20 +00:00
return ""
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"
let timeline = await showTimeline(@"name", @"after")
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"
let conversation = await getTweet(@"id")
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"
let url = decodeUrl(@"url")
if getHmac(url) != @"sig":
resp showError("Failed to verify signature")
let
client = newAsyncHttpClient()
pic = await client.getContent(url)
2019-06-21 00:30:57 +00:00
client.close()
2019-06-20 14:16:20 +00:00
resp pic, mimetype(url)
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()
pic = await client.getContent(url)
defer: client.close()
resp pic, mimetype(url)
runForever()