https   = require "ssl.https"
json    = require "dkjson"
socket  = require "socket"
sqlite3 = require "lsqlite3"

authors = {}

with fin = io.open "../raw/authors.txt", "r"
  while true
    line = fin\read "*l"
    break unless line

    table.insert authors, line

assert #authors == 193

db = assert sqlite3.open "../db/posts.db"

insert_stmt = assert db\prepare "INSERT INTO Users VALUES (NULL, ?, ?, ?)"

db\exec [[ BEGIN TRANSACTION; ]]

for i, author in pairs authors
  print "fetching for #{author}..."

  reply, code, headers = https.request "https://derpibooru.org/profiles/#{author}.json"
  if code ~= 200
    db\exec [[ ROLLBACK; ]]
    error reply

  person, _, err = json.decode reply
  if err
    db\exec [[ ROLLBACK; ]]
    error err

  print "found #{person.name}"

  do
    insert_stmt\bind_values person.id, person.name, person.avatar
    insert_stmt\step!
    insert_stmt\reset!

  print "done at #{os.date!}"

  socket.sleep 1

db\exec [[ COMMIT; ]]
