From 3839ddd8b9f6929198f93417464e13d1ea985f46 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 12 Feb 2016 11:25:54 -0800 Subject: [PATCH] fix time parsing code --- src/time/gotime.nim | 5 ++--- src/twtxt.nim | 23 +++++++++++++++++------ src/twtxtlist.nim | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/time/gotime.nim b/src/time/gotime.nim index 66fc107..4502577 100644 --- a/src/time/gotime.nim +++ b/src/time/gotime.nim @@ -2,7 +2,7 @@ import times proc fixTime(p0: cstring): ptr clonglong {. importc, dynlib: "./gotime.so", noSideEffect .} -proc parseTime*(inp: string): TimeInfo = +proc parseTime*(inp: string): int = let cst = inp.cstring res = cst.fixTime @@ -12,9 +12,8 @@ proc parseTime*(inp: string): TimeInfo = let resdep = res[] - restime = resdep.fromSeconds - return restime.timeToTimeInfo + return resdep.int when isMainModule: let myTime = fixTime("2006-01-02T15:04:05.000000-07:00") diff --git a/src/twtxt.nim b/src/twtxt.nim index 11c0059..181b106 100644 --- a/src/twtxt.nim +++ b/src/twtxt.nim @@ -6,17 +6,17 @@ const type Tweet* = object of RootObj - date*: TimeInfo + date*: int message*: string username*: string proc `$`*(t: Tweet): string = - return t.date.format(ISOTime) & " " & t.message + return t.date.fromSeconds.timeToTimeInfo.format(ISOTime) & " " & t.message proc `%`*(t: Tweet): JsonNode = %* { - "date": t.date.format(ISOTime), + "date": t.date, "message": t.message, "username": t.username, } @@ -32,7 +32,7 @@ proc parseTweet*(user, inp: string): Tweet = splitTweet = inp.split '\t' if splitTweet.len != 2: - raise newException(ValueError, "Invalid tweet") + raise newException(ValueError, "Invalid tweet " & inp) let tdate = splitTweet[0].parseTime @@ -43,7 +43,7 @@ proc parseTweet*(user, inp: string): Tweet = username: user) proc fromDBRow*(r: Row): Tweet = - Tweet(date: r[2].split(".")[0].parseInt().fromSeconds().timeToTimeInfo(), + Tweet(date: r[2].split(".")[0].parseInt(), username: r[1], message: r[3]) @@ -65,9 +65,20 @@ proc updateTweetsByUser*(db: DBConn, username, url: string) {. gcsafe .} = let tweets = getTweetsFrom(url, username) + var counter = 0 + for tweet in tweets.items(): try: - db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message) + db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date, tweet.message) + echo "Adding tweet " & $(%tweet) + counter = counter + 1 except: discard + + if counter > 0: + let + msg = "Added " & $counter & " tweets from @<" & username & " " & url & ">" + now = getTime().getGMTime().timeInfoToTime().toSeconds() + + db.exec(sql"insert into tweets values(null, 'twtxtlist', ?, ?);", now, msg) except: echo username & " " & getCurrentExceptionMsg() diff --git a/src/twtxtlist.nim b/src/twtxtlist.nim index d9de822..2aee00e 100644 --- a/src/twtxtlist.nim +++ b/src/twtxtlist.nim @@ -103,7 +103,7 @@ routes: db.exec(sql"insert into users values (null, ?, ?)", username, url) db.exec(sql"insert into tweets values (null, 'twtxtlist', ?, ?)", $(getTime().getGMTime().timeinfoToTime().toSeconds()), - "Added user @" & username & " " & url) + "Added user @<" & username & " " & url & ">") redirect "/users/" & username & "/0" except: fail()