fix time parsing code

This commit is contained in:
Christine Dodrill 2016-02-12 11:25:54 -08:00
parent db687ce3f6
commit 3839ddd8b9
3 changed files with 20 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import times
proc fixTime(p0: cstring): ptr clonglong {. importc, dynlib: "./gotime.so", noSideEffect .} proc fixTime(p0: cstring): ptr clonglong {. importc, dynlib: "./gotime.so", noSideEffect .}
proc parseTime*(inp: string): TimeInfo = proc parseTime*(inp: string): int =
let let
cst = inp.cstring cst = inp.cstring
res = cst.fixTime res = cst.fixTime
@ -12,9 +12,8 @@ proc parseTime*(inp: string): TimeInfo =
let let
resdep = res[] resdep = res[]
restime = resdep.fromSeconds
return restime.timeToTimeInfo return resdep.int
when isMainModule: when isMainModule:
let myTime = fixTime("2006-01-02T15:04:05.000000-07:00") let myTime = fixTime("2006-01-02T15:04:05.000000-07:00")

View File

@ -6,17 +6,17 @@ const
type type
Tweet* = object of RootObj Tweet* = object of RootObj
date*: TimeInfo date*: int
message*: string message*: string
username*: string username*: string
proc `$`*(t: Tweet): 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 = proc `%`*(t: Tweet): JsonNode =
%* %*
{ {
"date": t.date.format(ISOTime), "date": t.date,
"message": t.message, "message": t.message,
"username": t.username, "username": t.username,
} }
@ -32,7 +32,7 @@ proc parseTweet*(user, inp: string): Tweet =
splitTweet = inp.split '\t' splitTweet = inp.split '\t'
if splitTweet.len != 2: if splitTweet.len != 2:
raise newException(ValueError, "Invalid tweet") raise newException(ValueError, "Invalid tweet " & inp)
let let
tdate = splitTweet[0].parseTime tdate = splitTweet[0].parseTime
@ -43,7 +43,7 @@ proc parseTweet*(user, inp: string): Tweet =
username: user) username: user)
proc fromDBRow*(r: Row): Tweet = proc fromDBRow*(r: Row): Tweet =
Tweet(date: r[2].split(".")[0].parseInt().fromSeconds().timeToTimeInfo(), Tweet(date: r[2].split(".")[0].parseInt(),
username: r[1], username: r[1],
message: r[3]) message: r[3])
@ -65,9 +65,20 @@ proc updateTweetsByUser*(db: DBConn, username, url: string) {. gcsafe .} =
let let
tweets = getTweetsFrom(url, username) tweets = getTweetsFrom(url, username)
var counter = 0
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().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 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: except:
echo username & " " & getCurrentExceptionMsg() echo username & " " & getCurrentExceptionMsg()

View File

@ -103,7 +103,7 @@ routes:
db.exec(sql"insert into users values (null, ?, ?)", username, url) db.exec(sql"insert into users values (null, ?, ?)", username, url)
db.exec(sql"insert into tweets values (null, 'twtxtlist', ?, ?)", db.exec(sql"insert into tweets values (null, 'twtxtlist', ?, ?)",
$(getTime().getGMTime().timeinfoToTime().toSeconds()), $(getTime().getGMTime().timeinfoToTime().toSeconds()),
"Added user @" & username & " " & url) "Added user @<" & username & " " & url & ">")
redirect "/users/" & username & "/0" redirect "/users/" & username & "/0"
except: fail() except: fail()