import sqlite3
from jjsr.crawler import CrawlerBot
class DataBase:
    def __init__(self,name):
        self.name=name
        self.con = sqlite3.connect(self.name)
        self.cur = self.con.cursor()
        self.setup()
    def setup(self):
        self.cur.execute("""CREATE TABLE IF NOT EXISTS URLS (
        url NOT NULL UNIQUE,
        title,
        description
        );
        """)
    def select_all_data(self):
        res = self.cur.execute("SELECT * FROM URLS;")
        return res.fetchall()
    def insert_data(self,url):
        try:
            bot=CrawlerBot("JJSRBOT")
            url_content= bot.get_content_from_urls([url])[0]
            data= [(url,url_content[0],url_content[1])]
            
            self.cur.executemany("INSERT INTO URLS VALUES(?, ?, ?)", data)
            self.con.commit()
            return f"{data} Inserted Successfully"
        except BaseException as e:
            return str(e)
    def update_data(self):
        pass
    def delete_data(self,url):
        try:
            self.cur.execute(f"DELETE FROM URLS WHERE url='{url}';")
            self.con.commit()
            return "Deleted Successfully"
        except BaseException as e:
            return str(e)
    def reset(self):
        try:
            self.cur.execute(f"DROP TABLE URLS;")
            self.con.commit()
            return "Reset Successfully"
        except BaseException as e:
            return str(e)
if __name__=="__main__":
    pass
