import sqlite3
from tqdm import tqdm

def dataProducer():
    con = sqlite3.connect("database-dirty.db")
    cur = con.cursor()
    for levelNum, title, author, \
        authorID, numplays, numCompleted, \
        rating, target, darts, leveldata in cur.execute('SELECT * FROM levels;'):
        yield (
            levelNum,
            title,
            author.split(", by ")[-1].split(" (view ")[0],
            authorID,
            numplays,
            numCompleted,
            rating if rating != "?" else None,
            target,
            darts,
            leveldata,
        )

    # Be sure to close the connection
    con.close()
    exit()

from game.models import Level, Author

bar = tqdm(desc="Bloons db import", total=151902)

for d in dataProducer():
    author = Author(name=d[2],
                    authorId=d[3])
    author.save()
    Level(author=author,
              title=d[1],
              levelId=d[0],
              plays=d[4],
              completions=d[5],
              rating=d[6],
              target=d[7],
              darts=d[8],
              data=d[9]).save()
    bar.update(1)

