import requests
from bs4 import BeautifulSoup
import sqlite3
from progress.bar import IncrementalBar
from tqdm import tqdm

Database = sqlite3.connect("Film_list.db")
cur = Database.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS Film_list (
            ID INTEGER PRIMARY KEY,
            NAME TEXT,
            YEAR TEXT,
            LINK TEXT)""")
Database.commit()


url = f"https://hd-38.lordfilm-s.me/films/"
r = requests.get(url)
soup = BeautifulSoup(r.text, "lxml")

number_of_films = int(soup.find("div", {"class": "navigation"}).text.split()[-1])


name_year = [""]*24*number_of_films
link = [""]*24*number_of_films
counter = 0
number = 1



bar = IncrementalBar('Download links ', max = number_of_films)

while number <= number_of_films:

    r = requests.get(url+f"page/{number}/")
    soup = BeautifulSoup(r.text, "lxml")

    for i, j, k in zip(soup.find_all("div", class_ = "th-title"),soup.find_all("div", class_ = "th-year"),soup.find_all("div", class_ = "th-item")):
        name_year[counter] = i.text + ' ('+ j.text+')'
        link[counter] = k.find("a", class_ = "th-in with-mask").get('href')
        counter += 1
    number += 1
    bar.next()
bar.finish()

for i in tqdm(range(24*number_of_films), desc="Upload in Database"):
    cur.execute("""INSERT INTO Film_list (NAME, LINK) VALUES(?, ?);""", (name_year[i],  link[i]))
    Database.commit()

