submissions, individual tweet viewing
This commit is contained in:
parent
55ca42fd9b
commit
4ac0e3ee2a
|
@ -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>
|
|
@ -8,7 +8,7 @@
|
||||||
<div class="entries">
|
<div class="entries">
|
||||||
<a href="/users/{{ username }}/0">{{ username }}</a> - {{ time }}
|
<a href="/users/{{ username }}/0">{{ username }}</a> - {{ time }}
|
||||||
<p>{{ tweet }}</p>
|
<p>{{ tweet }}</p>
|
||||||
<small><a href="/tweets/{{ permalink }}">permalink</a></small>
|
<small><a href="/tweet/{{ permalink }}">permalink</a></small>
|
||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
{{/ tweets }}
|
{{/ tweets }}
|
||||||
|
|
|
@ -52,26 +52,26 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
proc updateTweets*() =
|
proc updateTweetsOnce*() =
|
||||||
var db = open("./data/twtxt.db", nil, nil, nil)
|
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():
|
for user in users.items():
|
||||||
echo "updating " & $user
|
echo "updating " & $user
|
||||||
|
|
||||||
let
|
let
|
||||||
username = user[1]
|
username = user[1]
|
||||||
url = user[2]
|
url = user[2]
|
||||||
tweets = getTweetsFrom(url, username)
|
tweets = getTweetsFrom(url, username)
|
||||||
|
|
||||||
for tweet in tweets.items():
|
for tweet in tweets.items():
|
||||||
try:
|
try:
|
||||||
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds(), tweet.message)
|
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
|
||||||
except: discard
|
except: discard
|
||||||
|
|
||||||
sleep 300_000 # 5 minutes
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
updateTweets()
|
while true:
|
||||||
|
updateTweetsOnce()
|
||||||
|
|
||||||
|
sleep 300_000 # 5 minutes
|
||||||
|
|
|
@ -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
|
strutils, tables, times, twtxt, typetraits
|
||||||
|
|
||||||
const
|
const
|
||||||
|
@ -7,6 +7,7 @@ const
|
||||||
indexTemplate*: string = staticRead "./templates/index.mustache"
|
indexTemplate*: string = staticRead "./templates/index.mustache"
|
||||||
usersTemplate*: string = staticRead "./templates/users.mustache"
|
usersTemplate*: string = staticRead "./templates/users.mustache"
|
||||||
tweetsTemplate*: string = staticRead "./templates/tweets.mustache"
|
tweetsTemplate*: string = staticRead "./templates/tweets.mustache"
|
||||||
|
submitTemplate*: string = staticRead "./templates/submit.mustache"
|
||||||
|
|
||||||
let
|
let
|
||||||
db = open("data/twtxt.db", nil, nil, nil)
|
db = open("data/twtxt.db", nil, nil, nil)
|
||||||
|
@ -74,9 +75,27 @@ routes:
|
||||||
get "/timeline":
|
get "/timeline":
|
||||||
redirect "/timeline/0"
|
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":
|
get "/users":
|
||||||
var
|
var
|
||||||
users = db.getAllRows(sql"select username, url from users")
|
users = db.getAllRows(sql"select username, url from users order by username")
|
||||||
ctx = newContext()
|
ctx = newContext()
|
||||||
userList = newSeq[Context]()
|
userList = newSeq[Context]()
|
||||||
|
|
||||||
|
@ -169,4 +188,32 @@ routes:
|
||||||
|
|
||||||
except: fail()
|
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()
|
runForever()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
--define:ssl
|
Loading…
Reference in New Issue