
class tvshow():
    
    def __init__(self):
        pass
    
    def process(self, metadata, seriesdata, seriesid):
        webresult = []
        networkresult = []
        webworkchannel = []
        networkchannel = []
        if seriesdata == None:
            pass
        else:
            networkid = None
            try:
                network = {}
                for key, value in seriesdata['network'].items():
                    if key == 'country':
                        for key, value in seriesdata['network']['country'].items():
                            if key == 'name':
                                newkey = 'country'
                                network[newkey] = seriesdata['network']['country'][key]
                            else:
                                newkey = 'country' + key
                                network[newkey] = seriesdata['network']['country'][key]
                    else:
                        newkey = 'network' + key
                        network[newkey] = seriesdata['network'][key]
            except:
                #print('Failed to insert network information.')
                #seriesdata['networkid'] = '0'
                pass
            else:    
                seriesdata.pop('network')
                seriesdata['networkid'] = network['networkid']
                networkresult.append(network)
                networkchannel.append(metadata.tables['updates.tvnetworknewvals'])
                #insertresult = insert_data(network, tables[4])
            try:
                webchannel = {}
                if seriesdata['webChannel']:
                    for key, value in seriesdata['webChannel'].items():
                        if key == 'country':
                                if seriesdata['webChannel'][key] is None:
                                    pass
                                else:
                                    for key, value in seriesdata['webChannel']['country'].items():
                                        newkey = 'webChannelcountry'+ key
                                        webchannel[newkey] = value 
                        else:
                            webchannel[key] = value
                    if key == 'name':
                        newkey = 'webChannel' + key
                        webchannel[newkey] = value 
                    if key == 'timezone':
                        newkey = 'webChannelcountry'+ key
                        webchannel[newkey] = value
            except:
                #print('Failed to insert webchannel information.')
                seriesdata['webchannelid'] = '0'
                pass
            else:
                #seriesdata.pop('webChannel')
                webchannel['id'] = '0'
                seriesdata['webchannelid'] = webchannel['id']
                webchannel['name'] = 'Not Applicable'
                webchannel['webchannelname'] = webchannel['name']
                #webchannel['networkid'] = webchannel['id']
                try:
                    webresult.append(webchannel)
                    webworkchannel.append(metadata.tables['updates.tvwebchannelnewvals'])
                    #insertresult = insert_data(webchannel, tables[5])
                except sqlalchemy.exc.IntegrityError as e:
                    pass
                except Exception as e:
                    #print('Failed to insert webchannel information.')
                    pass
            try:
                seriesdata['seriesid'] = seriesdata['id']
            except:
                pass
            
            #print('TV Show: ' + seriesdata['seriesname'])
            
            try:
                if seriesdata['image']:
                    for key, value in seriesdata['image'].items():
                        newkey = key + 'image' 
                        seriesdata[newkey] = value
                    seriesdata.pop('image')
                else:
                    seriesdata.pop('image')
            except:
                pass
            
            try:
                if seriesdata['externals']:
                    for key, value in seriesdata['externals'].items():
                        seriesdata[key] = value
                    seriesdata.pop('externals')
                else:
                    seriesdata.pop('externals')
            except KeyError as e:
                pass
            
            try:
                if seriesdata['rating']:
                    for key, value in seriesdata['rating'].items():
                        seriesdata['rating'] = value
                
                if seriesdata['schedule']:
                    seriesdata['time'] = seriesdata['schedule']['time']
                    seriesdata['days'] = ", ".join(seriesdata['schedule']['days'])
                    seriesdata.pop('schedule')
            except KeyError as e:
                pass
                   
            try:
                for key, value in seriesdata['_links']['self'].items():
                    seriesdata[key] = value    
                seriesdata.pop('_links')
            except KeyError as e:
                pass
            else:
                seriesdata.pop('id')
            
            if seriesdata['genres']:
                seriesdata['genres'] = ", ".join(seriesdata['genres'])
            
            # Finish transforming to match table columns
            seriesdata['seriesname'] = seriesdata.pop('name')
            seriesdata['tvrageid'] = seriesdata.pop('tvrage')
            seriesdata['tvdbid'] = seriesdata.pop('thetvdb')
            seriesdata['imdbid'] = seriesdata.pop('imdb')
            seriesdata['serieslanguage'] = seriesdata.pop('language')
            seriesdata['seriesstatus'] = seriesdata.pop('status')
            seriesdata['airdays'] = seriesdata.pop('days')
            seriesdata['airtime'] = seriesdata.pop('time')
            seriesdata['showtype'] = seriesdata.pop('type')
            seriesdata['overview'] = seriesdata.pop('summary')
            seriesdata['showurl'] = seriesdata.pop('url')
            seriesdata['apilink'] = seriesdata.pop('href')
            seriesdata['officialsite'] = seriesdata.pop('officialSite')
            
            results = {'networkdata': networkresult, 'networktable': networkchannel,
                       'webdata': webresult, 'webtable': webchannel,
                       'seriesdata': seriesdata}
            #results.append(tableresult)
            #results.append(seriesdata)
            return (results)

    
    def __del__(self):
        pass

