fix time parsing code
This commit is contained in:
parent
db687ce3f6
commit
3839ddd8b9
|
@ -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")
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue