only update the needed tweets for a given user when they are submitted

This commit is contained in:
Christine Dodrill 2016-02-10 10:50:01 -08:00
parent 313c79f3cc
commit d5ffe8db52
2 changed files with 14 additions and 14 deletions

View File

@ -57,6 +57,18 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
return res return res
proc updateTweetsByUser*(db: DBConn, username, url: string) {. gcsafe .} =
try:
let
tweets = getTweetsFrom(url, username)
for tweet in tweets.items():
try:
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
except: discard
except:
echo getCurrentExceptionMsg()
proc updateTweetsOnce*() {. gcsafe .} = proc updateTweetsOnce*() {. gcsafe .} =
var db = open("./data/twtxt.db", nil, nil, nil) var db = open("./data/twtxt.db", nil, nil, nil)
@ -65,15 +77,4 @@ proc updateTweetsOnce*() {. gcsafe .} =
for user in users.items(): for user in users.items():
echo "updating " & $user echo "updating " & $user
try: db.updateTweetsByUser(user[1], user[2])
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().int(), tweet.message)
except: discard
except:
echo getCurrentExceptionMsg()

View File

@ -91,8 +91,7 @@ routes:
username = $(request.formData.mget "username").body username = $(request.formData.mget "username").body
url = $(request.formData.mget "url").body url = $(request.formData.mget "url").body
discard url.getTweetsFrom(username) db.updateTweetsByUser(username, url)
db.exec(sql"insert into users values (null, ?, ?)", username, url) db.exec(sql"insert into users values (null, ?, ?)", username, url)
redirect "/users/" & username & "/0" redirect "/users/" & username & "/0"