submissions, individual tweet viewing

This commit is contained in:
Christine Dodrill 2016-02-06 10:18:57 -08:00
parent 55ca42fd9b
commit 4ac0e3ee2a
5 changed files with 77 additions and 19 deletions

View File

@ -0,0 +1,10 @@
<form method="POST" action="/submit" enctype="multipart/form-data">
<dl>
<dt>username</dt>
<dd><input type="text" name="username" /></dd>
<dt>url</dt>
<dd><input type="text" name="url" /></dd>
<dd><input type=submit value="submit"></dd>
</form>

View File

@ -8,7 +8,7 @@
<div class="entries">
<a href="/users/{{ username }}/0">{{ username }}</a> - {{ time }}
<p>{{ tweet }}</p>
<small><a href="/tweets/{{ permalink }}">permalink</a></small>
<small><a href="/tweet/{{ permalink }}">permalink</a></small>
</div>
<br />
{{/ tweets }}

View File

@ -52,26 +52,26 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
return res
proc updateTweets*() =
proc updateTweetsOnce*() =
var db = open("./data/twtxt.db", nil, nil, nil)
while true:
var users = db.getAllRows sql"select * from users"
var users = db.getAllRows sql"select * from users"
for user in users.items():
echo "updating " & $user
for user in users.items():
echo "updating " & $user
let
username = user[1]
url = user[2]
tweets = getTweetsFrom(url, username)
let
username = user[1]
url = user[2]
tweets = getTweetsFrom(url, username)
for tweet in tweets.items():
try:
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds(), tweet.message)
except: discard
sleep 300_000 # 5 minutes
for tweet in tweets.items():
try:
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
except: discard
when isMainModule:
updateTweets()
while true:
updateTweetsOnce()
sleep 300_000 # 5 minutes

View File

@ -1,4 +1,4 @@
import asyncdispatch, db_sqlite, jester, moustachu, os, shorturl,
import asyncdispatch, db_sqlite, httpclient, jester, moustachu, os, shorturl,
strutils, tables, times, twtxt, typetraits
const
@ -7,6 +7,7 @@ const
indexTemplate*: string = staticRead "./templates/index.mustache"
usersTemplate*: string = staticRead "./templates/users.mustache"
tweetsTemplate*: string = staticRead "./templates/tweets.mustache"
submitTemplate*: string = staticRead "./templates/submit.mustache"
let
db = open("data/twtxt.db", nil, nil, nil)
@ -74,9 +75,27 @@ routes:
get "/timeline":
redirect "/timeline/0"
get "/submit":
renderMustache "submit", submitTemplate, newContext()
post "/submit":
try:
var
username = $(request.formData.mget "username").body
url = $(request.formData.mget "url").body
discard url.getTweetsFrom(username)
db.exec(sql"insert into users values (null, ?, ?)", username, url)
updateTweetsOnce()
redirect "/users/" & username & "/0"
except: fail()
get "/users":
var
users = db.getAllRows(sql"select username, url from users")
users = db.getAllRows(sql"select username, url from users order by username")
ctx = newContext()
userList = newSeq[Context]()
@ -169,4 +188,32 @@ routes:
except: fail()
get "/tweet/@tid":
try:
var
tweets = db.getAllRows(sql"select * from tweets where id=? limit 1", (@"tid").decodeURLSimple())
ctx = newContext()
tweetList = newSeq[Context]()
if tweets.len == 0:
raise newException(ValueError, "no such tweet")
for tweet in tweets.items():
var c = newContext()
let time = tweet[2].split(".")[0].parseInt()
c["id"] = tweet[0]
c["permalink"] = tweet[0].parseInt().encodeURLSimple()
c["username"] = tweet[1]
c["time"] = time
c["tweet"] = tweet[3]
tweetList.add c
ctx["tweets"] = tweetList
renderMustache("tweet " & @"tid", tweetsTemplate, ctx)
except: fail()
runForever()

View File

@ -0,0 +1 @@
--define:ssl