import db_sqlite, json, httpclient, os, strutils, times 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) while true: var users = db.getAllRows sql"select * from users" for user in users.items(): echo user let username = user[1] url = user[2] tweets = getTweetsFrom(url, username) echo tweets sleep 300_000 # 5 minutes when isMainModule: updateTweets()