twtxtlist/src/twtxt.nim

74 lines
1.4 KiB
Nim
Raw Normal View History

import db_sqlite, json, httpclient, os, strutils, times
2016-02-06 15:36:53 +00:00
const
ISOTime*: string = "yyyy-MM-dd'T'HH:mm:ss'.000000'zzz"
type
Tweet* = object of RootObj
date*: TimeInfo
message*: string
username*: string
proc `$`*(t: Tweet): string =
t.date.format(ISOTime) & "\t" & t.message
proc `%`*(t: Tweet): JsonNode =
%*
{
"date": t.date.format(ISOTime),
"message": t.message,
"username": t.username,
}
proc `%`*(s: seq[Tweet]): JsonNode =
result = newJArray()
for t in s.items():
result.add %t
proc parseTweet*(user, inp: string): Tweet =
let
splitTweet = inp.split '\t'
if splitTweet.len != 2:
raise newException(ValueError, "Invalid tweet")
let
tdate = splitTweet[0].parse(ISOTime)
message = splitTweet[1]
Tweet(date: tdate,
message: message,
username: user)
proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
var res = newSeq[Tweet]()
let body = url.getContent()
for line in body.splitLines().items():
if line.len > 0:
res.add(username.parseTweet(line))
return res
proc updateTweets*() =
var db = open("./data/twtxt.db", nil, nil, nil)
2016-02-06 15:36:53 +00:00
while true:
var users = db.getAllRows sql"select * from users"
2016-02-06 15:36:53 +00:00
for user in users.items():
echo user
let
username = user[1]
url = user[2]
tweets = getTweetsFrom(url, username)
2016-02-06 15:36:53 +00:00
echo tweets
2016-02-06 15:36:53 +00:00
sleep 300_000 # 5 minutes
when isMainModule:
updateTweets()