#!/usr/bin/env python
import sqlite3
import subprocess

DATABASE_FILE = "youtube-videos.db"

conn = sqlite3.connect(DATABASE_FILE)
cur = conn.cursor()

cur.execute("select * from videos_by_id where downloaded = 0")
undownloaded = cur.fetchall()
conn.close()

if len(undownloaded) == 0:
    exit()

conn = sqlite3.connect(DATABASE_FILE)
cur = conn.cursor()
for video in undownloaded:
    subprocess.call(["./download.sh", video[0]])
    cur.execute("""update videos_by_id set downloaded = 1 where id = ?""", (video[0],))
conn.commit()
cur.close()
conn.close()
