this should be called worker
This commit is contained in:
parent
bdba4bb60c
commit
4abe9b8ba3
|
@ -49,7 +49,7 @@ proc fromDBRow*(r: Row): Tweet =
|
||||||
proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
|
proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
|
||||||
var res = newSeq[Tweet]()
|
var res = newSeq[Tweet]()
|
||||||
|
|
||||||
let body = url.getContent()
|
let body = url.getContent(timeout=20_000)
|
||||||
|
|
||||||
for line in body.splitLines().items():
|
for line in body.splitLines().items():
|
||||||
if line.len > 0:
|
if line.len > 0:
|
||||||
|
@ -57,7 +57,7 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
proc updateTweetsOnce*() =
|
proc updateTweetsOnce*() {. gcsafe .} =
|
||||||
var db = open("./data/twtxt.db", nil, nil, nil)
|
var db = open("./data/twtxt.db", nil, nil, nil)
|
||||||
|
|
||||||
var users = db.getAllRows sql"select * from users"
|
var users = db.getAllRows sql"select * from users"
|
||||||
|
@ -65,20 +65,14 @@ proc updateTweetsOnce*() =
|
||||||
for user in users.items():
|
for user in users.items():
|
||||||
echo "updating " & $user
|
echo "updating " & $user
|
||||||
|
|
||||||
let
|
|
||||||
username = user[1]
|
|
||||||
url = user[2]
|
|
||||||
tweets = getTweetsFrom(url, username)
|
|
||||||
|
|
||||||
for tweet in tweets.items():
|
|
||||||
try:
|
|
||||||
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
|
|
||||||
except: discard
|
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
while true:
|
|
||||||
try:
|
try:
|
||||||
updateTweetsOnce()
|
let
|
||||||
except: discard
|
username = user[1]
|
||||||
|
url = user[2]
|
||||||
|
tweets = getTweetsFrom(url, username)
|
||||||
|
|
||||||
sleep 300_000 # 5 minutes
|
for tweet in tweets.items():
|
||||||
|
try:
|
||||||
|
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
|
||||||
|
except: discard
|
||||||
|
except: discard
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
--define:ssl
|
--define:ssl
|
||||||
--threads:on
|
--threads:on
|
||||||
|
--threadAnalysis:off
|
||||||
|
|
|
@ -95,8 +95,6 @@ routes:
|
||||||
|
|
||||||
db.exec(sql"insert into users values (null, ?, ?)", username, url)
|
db.exec(sql"insert into users values (null, ?, ?)", username, url)
|
||||||
|
|
||||||
updateTweetsOnce()
|
|
||||||
|
|
||||||
redirect "/users/" & username & "/0"
|
redirect "/users/" & username & "/0"
|
||||||
except: fail()
|
except: fail()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import os, threadpool, twtxt
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
while true:
|
||||||
|
try:
|
||||||
|
spawn updateTweetsOnce()
|
||||||
|
except: discard
|
||||||
|
|
||||||
|
sleep 300_000 # 5 minutes
|
|
@ -0,0 +1,3 @@
|
||||||
|
--define:ssl
|
||||||
|
--threads:on
|
||||||
|
--threadAnalysis:off
|
|
@ -5,7 +5,7 @@ author = "Christine Dodrill"
|
||||||
description = "a list of twtxt hacker feeds"
|
description = "a list of twtxt hacker feeds"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
srcDir = "src"
|
srcDir = "src"
|
||||||
bin = @["twtxtlist", "twtxt"]
|
bin = @["twtxtlist", "worker"]
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
|
@ -18,6 +18,4 @@ task db, "SQLite database prompt":
|
||||||
|
|
||||||
task run, "Run app":
|
task run, "Run app":
|
||||||
exec "nimble build"
|
exec "nimble build"
|
||||||
exec "./twtxt &"
|
|
||||||
exec "./twtxtlist"
|
exec "./twtxtlist"
|
||||||
exec "killall twtxt"
|
|
||||||
|
|
Loading…
Reference in New Issue