import sqlalchemy
import progressbar
from tvmaze.Processor import loadjson

class Updater():
    def __init__(self):
        global jprocess
        
    def __del__(self):
        pass

class tvupdates():
    def __init__(self):
        pass        

    def update_tables(engine, updatesapi, tablename):
        jprocess = loadjson()
        print('Now retrieving available updates.')
        print('Creating update transaction.')
        ins = tablename.insert()
        seriesupdates = jprocess.load(updatesapi)
        updatelist = []
        seriesrange = len(seriesupdates) 
        with progressbar.ProgressBar(maxval=seriesrange, redirect_stdout=True) as p:
            for key, value in seriesupdates.items():
                i = int(key)
                updater = {}
                updater['seriesid'] = key
                updater['timestamp'] = value
                updatelist.append(updater)
                if i <= seriesrange:
                    p.update(i)
                #time.sleep(0.1) 
        print('Update of tvupdates table is complete.')
        print('Done.')
        return(ins, updatelist)

    def __del__(self):
        pass
