from public import addNolink,addDir3,addLink,lang,user_dataDir

import threading,urllib,os
import re,logging,sys,time,requests
import xbmcaddon,xbmc,xbmcgui
import cache,xbmcplugin,json
Addon = xbmcaddon.Addon()



domain_s='https://'
from general import fix_q,post_trakt
from general import call_trakt,BASE_LOGO,base_header


def adv_gen_window(url):
    import pyxbmct
    class adv_gen_window(pyxbmct.AddonDialogWindow):
        def __init__(self,type):
            super(adv_gen_window, self).__init__('choose')
            wd=int(1250)
            hd=int(700)
            px=int(10)
            py=int(10)
            self.type=type
            self.fromy=''
            self.toy=''
            self.all_clicked=[]
            self.setGeometry(wd, hd, 22, 5,pos_x=px, pos_y=py)
            self.set_info_controls()
            #self.set_active_controls()
            self.set_navigation()
            
            # Connect a key action (Backspace) to close the window..
            self.connect(pyxbmct.ACTION_PREVIOUS_MENU, self.click_c)
            self.connect(pyxbmct.ACTION_NAV_BACK, self.click_c)
            #self.connect(pyxbmct.ACTION_MOUSE_RIGHT_CLICK, self.click_c)
            
        def click_c(self):
            for items in self.all_radio:
                if self.all_radio[items]['button'].isSelected():
                    self.all_clicked.append(str(self.all_radio[items]['id']))
            
            
            self.fromy=self.edit_from.getText()
            self.toy=self.edit_to.getText()
            
            self.close()
        def radio_update(self,radiobutton,id):
            # Update radiobutton caption on toggle

            if radiobutton.isSelected():
                self.all_clicked.append(id)
            else:
                if id in self.all_clicked:
                    self.all_clicked.pop(id)
        def set_info_controls(self):
            # Edit
            edit_label = pyxbmct.Label('From year')
            self.placeControl(edit_label, 0, 0, rowspan=2)
            
            self.edit_from = pyxbmct.Edit('2000')
            self.placeControl(self.edit_from, 0, 1, rowspan=2)
            # Additional properties must be changed after (!) displaying a control.
            self.edit_from.setText('2000')
            
            # Edit
            edit_label = pyxbmct.Label('To year')
            self.placeControl(edit_label, 2, 0, rowspan=2)
            
            self.edit_to = pyxbmct.Edit('2019')
            self.placeControl(self.edit_to, 2, 1, rowspan=2)
            # Additional properties must be changed after (!) displaying a control.
            self.edit_to.setText('2019')
            
            #genere
            edit_label = pyxbmct.Label('Genre')
            self.placeControl(edit_label, 0, 3)
            
            url='http://api.themoviedb.org/3/genre/%s/list?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&page=1'%(self.type,lang)
            x=requests.get(url).json()
            self.all_g=[]
            for items in x['genres']:
               
                self.all_g.append((items['name'],items['id']))
            self.all_radio={}
            i=1
            self.all_it=[]
            for name,id in self.all_g:
                # RadioButton
                self.all_it.append(name)
                self.all_radio[name]={}
                self.all_radio[name]['button'] = pyxbmct.RadioButton(name)
                self.all_radio[name]['id']=id
                self.placeControl(self.all_radio[name]['button'], i, 3)
                #self.connect(self.all_radio[name], self.radio_functions[name])
                i+=1
            self.button = pyxbmct.Button('Search')
            self.placeControl(self.button, 21, 4, rowspan=2)
            # Connect control to close the window.
            self.connect(self.button, self.click_c)
        def set_navigation(self):
            self.edit_from.controlUp(self.edit_to)
            self.edit_from.controlDown(self.edit_to)
            
            self.edit_to.controlUp(self.edit_from)
            self.edit_to.controlDown(self.edit_from)
            
            self.edit_to.controlRight(self.all_radio[self.all_g[0][0]]['button'])
            self.edit_to.controlLeft(self.all_radio[self.all_g[0][0]]['button'])
            
            self.edit_from.controlRight(self.all_radio[self.all_g[0][0]]['button'])
            self.edit_from.controlLeft(self.all_radio[self.all_g[0][0]]['button'])
            
            
            
                
            
            for items in self.all_it:
                ind=self.all_it.index(items)
                if ind==0:
                    pre=len(self.all_it)-1
                    next=1
                elif ind==(len(self.all_it)-1):
                    pre=ind-1
                    next=0
                else:
                    pre=ind-1
                    next=ind+1
               
                self.all_radio[items]['button'].controlUp(self.all_radio[self.all_it[pre]]['button'])
                self.all_radio[items]['button'].controlDown(self.all_radio[self.all_it[next]]['button'])
                
                self.all_radio[items]['button'].controlLeft(self.edit_from)
                self.all_radio[items]['button'].controlRight(self.button)
            
            self.setFocus(self.edit_from)
    window = adv_gen_window(url)
    window.doModal()
    all_g=window.all_clicked
    start_y=window.fromy
    end_y=window.toy

    del window
    return all_g,start_y,end_y
class Thread(threading.Thread):
    def __init__(self, target, *args):
       
        self._target = target
        self._args = args
        
        
        threading.Thread.__init__(self)
        
    def run(self):
        
        self._target(*self._args)
def get_html_g():
    try:
        url_g='https://api.themoviedb.org/3/genre/tv/list?api_key=34142515d9d23817496eeb4ff1d223d0&language='+lang
        html_g_tv=requests.get(url_g).json()
         
   
        url_g='https://api.themoviedb.org/3/genre/movie/list?api_key=34142515d9d23817496eeb4ff1d223d0&language='+lang
        html_g_movie=requests.get(url_g).json()
    except Exception as e:
        logging.warning('Err in HTML_G:'+str(e))
    return html_g_tv,html_g_movie
html_g_tv,html_g_movie=cache.get(get_html_g,72, table='posters')

class Thread(threading.Thread):
    def __init__(self, target, *args):
       
        self._target = target
        self._args = args
        
        
        threading.Thread.__init__(self)
        
    def run(self):
        
        self._target(*self._args)
def get_tmdb_data(new_name_array,html_g,fav_search_f,fav_servers_en,fav_servers,google_server,rapid_server,direct_server,heb_server,url,isr,xxx):
          
          try:
           global all_d
           if Addon.getSetting("trakt_access_token")!='':
               all_movie_w=[]
               try:
                   i = (call_trakt('/users/me/watched/movies'))
                   
                   for ids in i:
                      all_movie_w.append(str(ids['movie']['ids']['tmdb']))
               except:
                pass
           
           html=requests.get(url).json()
           max_page=html['total_pages']
     
           all_res=html['total_results']
          
           count=0
           
           for data in html['results']:
           
             count+=1
             if 'vote_average' in data:
               rating=data['vote_average']
             else:
              rating=0
             if 'first_air_date' in data:
               year=str(data['first_air_date'].split("-")[0])
             elif 'release_date' in data:
                year=str(data['release_date'].split("-")[0])
             else:
                year='0'
             if data['overview']==None:
               plot=' '
             else:
               plot=data['overview']
             if 'title' not in data:
               tv_movie='tv'
               new_name=data['name']
             else:
               tv_movie='movie'
               new_name=data['title']
              
             
             f_subs=[]
             if 'original_title' in data:
               original_name=data['original_title']
               mode=15
               
               id=str(data['id'])
               if Addon.getSetting("check_subs")=='true' or Addon.getSetting("disapear")=='true':
                 import cache
                 from subs import get_links
                 f_subs=cache.get(get_links,9999,'movie',original_name,original_name,'0','0','0','0',year,id,True, table='pages')
               if data['original_language']!='en':
                
                html2=requests.get('http://api.themoviedb.org/3/movie/%s?api_key=34142515d9d23817496eeb4ff1d223d0'%id).json()
                original_name=html2['title']
                
               
             else:
               original_name=data['original_name']
               id=str(data['id'])
               
               mode=16
               
               if data['original_language']!='en':
                
                    html2=requests.get('http://api.themoviedb.org/3/tv/%s?api_key=34142515d9d23817496eeb4ff1d223d0'%id).json()
                    if 'name' in html2:
                        original_name=html2['name']
                    #if 'name' in data:
                    #    original_name=data['name']
                   
             if data['poster_path']==None:
              icon=' '
             else:
               icon=data['poster_path']
             if 'backdrop_path' in data:
                 if data['backdrop_path']==None:
                  fan=' '
                 else:
                  fan=data['backdrop_path']
             else:
                fan=html['backdrop_path']
             if plot==None:
               plot=' '
             if 'http' not in fan:
               fan='https://image.tmdb.org/t/p/original/'+fan
             if 'http' not in icon:
               icon='https://image.tmdb.org/t/p/original/'+icon
             genres_list= dict([(i['id'], i['name']) for i in html_g['genres'] \
                    if i['name'] is not None])
             try:genere = u' / '.join([genres_list[x] for x in data['genre_ids']])
             except:genere=''
             
             trailer = "plugin://plugin.video.shadow?mode=25&id=%s&url=%s" % (id,tv_movie)
             if new_name not in new_name_array:
              new_name_array.append(new_name)
              if Addon.getSetting("check_subs")=='true' or Addon.getSetting("disapear")=='true':
                  if len(f_subs)>0:
                    color='white'
                  else:
                    color='red'
                    
              else:
                 color='white'
              start_time = time.time()
              
              
              
              
              
              
              elapsed_time = time.time() - start_time
              
              
              if  Addon.getSetting("disapear")=='true' and color=='red' and mode!=7:
                a=1
              else:
                color='white'
                
                watched='no'
                if Addon.getSetting("trakt_access_token")!='':
                    if id in all_movie_w:
                        watched='yes'
                if  mode==4 and fav_search_f=='true' and fav_servers_en=='true' and (len(fav_servers)>0 or heb_server=='true' or google_server=='true' or rapid_server=='true' or direct_server=='true'):
                
                    fav_status='true'
                else:
                    fav_status='false'
            
                
                all_d.append(('[COLOR '+color+']'+new_name+'[/COLOR]',url,mode,icon,fan,plot,year,original_name,id,rating,new_name,year,isr,genere,trailer,watched,fav_status,xxx,max_page,all_res))
              
           
           
          except Exception as e:
            import linecache,sys
            exc_type, exc_obj, tb = sys.exc_info()
            f = tb.tb_frame
            lineno = tb.tb_lineno
            filename = f.f_code.co_filename
            linecache.checkcache(filename)
            line = linecache.getline(filename, lineno, f.f_globals)
            xbmc.executebuiltin((u'Notification(%s,%s)' % ('Shadow', 'No Trailer...Line:'+str(lineno)+' E:'+str(e))).encode('utf-8'))
            logging.warning('ERROR IN GET TMDB:'+str(lineno))
            logging.warning('inline:'+line)
            logging.warning(e)
            
            logging.warning('BAD Trailer play')
def get_all_data(first,last,url,link,new_name_array,isr):
    try:
        
        global all_d
        
        all_d=[]
        xxx=0
 
        if '/tv/' in url:
            fav_search_f=Addon.getSetting("fav_search_f")
            fav_servers_en=Addon.getSetting("fav_servers_en")
            fav_servers=Addon.getSetting("fav_servers")
           
            google_server= Addon.getSetting("google_server")
            rapid_server=Addon.getSetting("rapid_server")
            direct_server=Addon.getSetting("direct_server")
            heb_server=Addon.getSetting("heb_server")
        else:
            fav_search_f=Addon.getSetting("fav_search_f_tv")
            fav_servers_en=Addon.getSetting("fav_servers_en_tv")
            fav_servers=Addon.getSetting("fav_servers_tv")
            google_server= Addon.getSetting("google_server_tv")
            rapid_server=Addon.getSetting("rapid_server_tv")
            direct_server=Addon.getSetting("direct_server_tv")
            heb_server=Addon.getSetting("heb_server_tv")

   
              
              
        if '/tv/' in url:
             url_g='https://api.themoviedb.org/3/genre/tv/list?api_key=34142515d9d23817496eeb4ff1d223d0&language='+lang
             
             html_g=html_g_tv
        else:
             url_g='https://api.themoviedb.org/3/genre/movie/list?api_key=34142515d9d23817496eeb4ff1d223d0&language='+lang
             html_g=html_g_movie
        #html_g=requests.get(url_g).json()
 
        if Addon.getSetting("dp")=='true' and (last-first)>1:
                dp = xbmcgui.DialogProgress()
                dp.create("Loading", "Please Wait", '')
                dp.update(0)
        thread=[]
       
        for i in range(first,last):
          
           url=link+'page='+str(i)
          
           
           thread.append(Thread(get_tmdb_data,new_name_array,html_g,fav_search_f,fav_servers_en,fav_servers,google_server,rapid_server,direct_server,heb_server,url,isr,xxx))
           thread[len(thread)-1].setName('Page '+str(i))
           xxx+=1
       

           
          
           
           

                #addDir3('[COLOR '+color+']'+new_name+'[/COLOR]',url,mode,icon,fan,plot,data=year,original_title=original_name,id=id,rating=rating,heb_name=new_name,show_original_year=year,generes=genere,trailer=trailer,watched=watched,fav_status=fav_status)
    except Exception ,err:
            import traceback
            from os.path import basename
            exc_info=sys.exc_info()
            e=(traceback.format_exc())
            et=e.split(',')
          
            e=','.join(et).replace('UnboundLocalError: ','')
            home1=xbmc.translatePath("special://home/")
            e_al=e.split(home1)
            logging.warning(e_al)
            e=e_al[len(e_al)-1].replace(home1,'')
            logging.warning('Error TMDB:'+str(e))
    start_time=time.time()
    for td in thread:
        td.start()
        if 1:
            
            while td.is_alive():
                xbmc.sleep(100)
            
        if Addon.getSetting("dp")=='true' and (last-first)>1:
                elapsed_time = time.time() - start_time
                dp.update(0, ' Activating '+ time.strftime("%H:%M:%S", time.gmtime(elapsed_time)),td.name," ")
        
        #xbmc.sleep(255)
        
    while 1:

          still_alive=0
          all_alive=[]
          for yy in range(0,len(thread)):
            
            if  thread[yy].is_alive():
              
              still_alive=1
              all_alive.append(thread[yy].name)
          if still_alive==0:
            break
          if Addon.getSetting("dp")=='true' and (last-first)>1:
                elapsed_time = time.time() - start_time
                dp.update(0, ' Please Wait '+ time.strftime("%H:%M:%S", time.gmtime(elapsed_time)),','.join(all_alive)," ")
          xbmc.sleep(100)
    if Addon.getSetting("dp")=='true' and (last-first)>1:
        dp.close()
    return all_d
    
def get_movies(url,local=False,reco=0,global_s=False):
   
   new_name_array=[]
   isr=0
   import cache
   all_years=[]
   import datetime
   all_d=[]
   now = datetime.datetime.now()
   for year in range(now.year,1970,-1):
         all_years.append(str(year))
   if 'advance' in url:
        all_g,start_y,end_y=adv_gen_window(url.split('_')[1])
        
        url='http://api.themoviedb.org/3/discover/%s?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&sort_by=popularity.desc&primary_release_date.gte=%s-01-01&primary_release_date.lte=%s-12-31&with_genres=%s&page=1'%(url.split('_')[1],lang,start_y,end_y,','.join(all_g))
   if url=='movie_years&page=1':
     
      
      if Addon.getSetting("dip_dialog")=='0':
          ret=ret = xbmcgui.Dialog().select("Choose", all_years)
          if ret!=-1:
            
              url='https://api.themoviedb.org/3/discover/movie?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&sort_by=popularity.desc&include_adult=false&include_video=false&primary_release_year=%s&with_original_language=en&page=1'%(lang,all_years[ret])
            
          else:
            return 0
      else:
        for items in all_years:
            
            url='https://api.themoviedb.org/3/discover/movie?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&sort_by=popularity.desc&include_adult=false&include_video=false&primary_release_year=%s&with_original_language=en&page=1'%(lang,items)
            if 0:
               if  name not in all_n:
                all_n.append(name)
                
                aa=addDir3(items,url,14,'https://www.techniquetuesday.com/mm5/graphics/00000001/Technique-Tuesday-Calendar-Years-Clear-Stamps-Large_329x400.jpg','https://images.livemint.com/rf/Image-621x414/LiveMint/Period2/2018/08/16/Photos/Processed/investment-knrG--621x414@LiveMint.jpg',items,collect_all=True)
                all_d.append(aa)
            else:
                aa=addDir3(items,url,14,'https://www.techniquetuesday.com/mm5/graphics/00000001/Technique-Tuesday-Calendar-Years-Clear-Stamps-Large_329x400.jpg','https://images.livemint.com/rf/Image-621x414/LiveMint/Period2/2018/08/16/Photos/Processed/investment-knrG--621x414@LiveMint.jpg',items,collect_all=True)
                all_d.append(aa)
        
   if url=='tv_years&page=1' and 'page=1' in url:
      if Addon.getSetting("dip_dialog")=='0':
          ret=ret = xbmcgui.Dialog().select("Choose", all_years)
          if ret!=-1:
            url='https://api.themoviedb.org/3/discover/tv?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&sort_by=popularity.desc&first_air_date_year=%s&include_null_first_air_dates=false&with_original_language=en&page=1'%(lang,all_years[ret])
           
          else:
            sys.exit()
      else:
        for items in all_years:
            url='https://api.themoviedb.org/3/discover/tv?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s&sort_by=popularity.desc&first_air_date_year=%s&include_null_first_air_dates=false&with_original_language=en&page=1'%(lang,items)
            
            aa=addDir3(items,url,14,'https://www.techniquetuesday.com/mm5/graphics/00000001/Technique-Tuesday-Calendar-Years-Clear-Stamps-Large_329x400.jpg','https://images.livemint.com/rf/Image-621x414/LiveMint/Period2/2018/08/16/Photos/Processed/investment-knrG--621x414@LiveMint.jpg',items,collect_all=True)
            all_d.append(aa)
   try:
        from sqlite3 import dbapi2 as database
   except:
        from pysqlite2 import dbapi2 as database
   cacheFile=os.path.join(user_dataDir,'database.db')
   dbcon = database.connect(cacheFile)
   dbcur = dbcon.cursor()
   dbcur.execute("CREATE TABLE IF NOT EXISTS %s ( ""name TEXT, ""tmdb TEXT, ""season TEXT, ""episode TEXT,""playtime TEXT,""total TEXT, ""free TEXT);" % 'playback')
   dbcur.execute("CREATE TABLE IF NOT EXISTS %s ( ""name TEXT ,""tv_movie TEXT);" % ('search_string2'))
   
   dbcon.commit()
   dbcur.execute("SELECT * FROM search_string2")
   match = dbcur.fetchall()
   all_s_strings=[]
   for qu,tt in match:
    all_s_strings.append((qu+'$$$'+tt))

   
                 
   if '/search' in url and 'page=1' in url and '%s' in url:
        
        
        
        search_entered =''
        keyboard = xbmc.Keyboard(search_entered, 'Enter Search')
        keyboard.doModal()
        if keyboard.isConfirmed() :
               search_entered = keyboard.getText()
               if search_entered=='':
                sys.exit()
          
               
               url=url%urllib.quote_plus(search_entered)
               if '/tv?' in url:
                type_in='tv'
               else:
                type_in='movie'
               
              
          
        else:
          
          
          sys.exit()
   if '/search' in url and 'page=1' in url:
       search_entered=url.split('query=')[1]
       search_entered=search_entered.split('&')[0]
       if 'tv' in url:
            tv_movie='tv'
       else:
            tv_movie='movie'
       if search_entered+'$$$'+tv_movie not in all_s_strings:
         
         dbcur.execute("INSERT INTO search_string2 Values ('%s','%s')"%(urllib.unquote_plus(search_entered.replace("'","%27")),tv_movie))
         dbcon.commit()
   html={}
   html['results']=[]
   regex='page=(.+?)$'
   match=re.compile(regex).findall(url)
   # first=int(match[0])
   # last=int(match[0])+1
   # link=url.split('page=')[0]
   if len(match)==0 or reco==1:
    first=1
    last=2
    link=url.split('page=')[0]
   else:
       link=url.split('page=')[0]
       first=int(match[0])
       s_last=int(Addon.getSetting("num_p"))
       if s_last>10:
         s_last=10
       last=first+int(s_last)


   

   

   #all_in_data=get_all_data(first,last,url,link,new_name_array,isr)
  
  
   all_in_data=cache.get(get_all_data,24,first,last,url,link,new_name_array,isr, table='pages')

   if '/search' in url:
    all_in_data=sorted(all_in_data, key=lambda x: x[6], reverse=True)
   else:
   
    all_in_data=sorted(all_in_data, key=lambda x: x[17], reverse=False)
   max_page=-1
   
    
   dbcur.execute("SELECT * FROM playback")
   match = dbcur.fetchall()
   all_w={}
      
   for n,tm,s,e,p,t,f in match:
            ee=str(tm)
            all_w[ee]={}
            all_w[ee]['resume']=str(p)
            all_w[ee]['totaltime']=str(t)
   for  name,url,mode,icon,fan,plot,year,original_name,id,rating,new_name,year,isr,genere,trailer,watched,fav_status,xxx,max_page,all_res in all_in_data:
            if local:
                addNolink( new_name, id,27,False,fan=fan, iconimage=icon,plot=plot,year=year,generes=genere,rating=rating,trailer=trailer)
            else:
                
                aa=addDir3(name,url,mode,icon,fan,'[B][I]'+year+'[/I][/B]\n'+plot,data=year,original_title=original_name,id=id,rating=rating,heb_name=new_name,show_original_year=year,generes=genere,trailer=trailer,watched=watched,fav_status=fav_status,collect_all=True,all_w=all_w)
                all_d.append(aa)

   regex='page=(.+?)$'
   match=re.compile(regex).findall(url)
   link=url.split('page=')[0]
   if   max_page==-1:
        if not global_s:
            xbmcgui.Dialog().ok('Shadow','[COLOR aqua][I] No results[/I][/COLOR]')
            
            sys.exit()
   
   if max_page>int(match[0]):
        if local:
            mode=26
        else:
            mode=14
        aa=addDir3(('[COLOR aqua][I]Page %s Out of %s (%s Results)[/I][/COLOR]'%(str(int(match[0])+1),str(max_page),str(all_res))).decode('utf8'),link+'page='+str(int(match[0])+1),mode,BASE_LOGO+'next.png','https://cdn4.iconfinder.com/data/icons/arrows-1-6/48/1-512.png','Results'.decode('utf8'),show_original_year='999',data='999',collect_all=True)
        all_d.append(aa)
     
   if 0:
       xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_YEAR)
       xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_SORT_TITLE)
       

       xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_RATING)
   xbmcplugin .addDirectoryItems(int(sys.argv[1]),all_d,len(all_d))
   dbcur.close()
   dbcon.close()
   return new_name_array
   
def get_seasons(name,url,iconimage,fanart,description,data,original_title,id):
   all_d=[]
   payload= {
                    "apikey": "0629B785CE550C8D",
                    "userkey": "",
                    "username": ""
   }
   tmdbKey = '653bb8af90162bd98fc7ee32bcbbfb3d'
   #headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'Accept-Language': 'he'}
   #r = requests.post(domain_s+'api.thetvdb.com/login', json=payload, headers=headers)
   #r_json = r.json()

   url=domain_s+'api.themoviedb.org/3/tv/%s?api_key=34142515d9d23817496eeb4ff1d223d0&language=en&append_to_response=external_ids'%id

   html=requests.get(url).json()
   if 'first_air_date' in html:
    show_original_year=html['first_air_date'].split("-")[0]
   else:
    show_original_year=0
   #tmdb data
   #headers['Authorization'] = "Bearer %s" %  str(r_json.get('token'))
   tmdbid=html['external_ids']['tvdb_id']
   if tmdbid==None:
     response2 = requests.get(domain_s+'www.thetvdb.com/?string=%s&searchseriesid=&tab=listseries&function=Search'%name).content
     
     SearchSeriesRegexPattern = 'a href=".+?tab=series.+?id=(.+?)mp'
     match=re.compile(SearchSeriesRegexPattern).findall(response2)
   
     for tmnum in match:
       tmnum=tmnum.replace("&a","")
       if len(tmnum)>0:
         tmdbid=tmnum

   #import tvdb
   #tvdb=tvdb.TheTvDb()
   #response=tvdb.get_series(html['external_ids']['tvdb_id'])
  
   #response = requests.get('http://thetvdb.com/api/0629B785CE550C8D/series/%s/all/he.xml'%html['external_ids']['tvdb_id'],headers=base_header,timeout=5).content
   #logging.warning(json.dumps(response))
   #attr=['Combined_season','FirstAired']
   #regex='<Episode>.+?<EpisodeName>(.+?)</EpisodeName>.+?<EpisodeNumber>(.+?)</EpisodeNumber>.+?<FirstAired>(.+?)</FirstAired>.+?<SeasonNumber>(.+?)</SeasonNumber>'
   #match=re.compile(regex,re.DOTALL).findall(response)
   match=[]
   #seasons_tvdb=parseDOM(response,'Episode', attr)
   all_season=[]
   all_season_tvdb_data=[]
    
   all_season_imdb=[]
   all_season_imdb_data=[]
   for ep_name,ep_num,aired,s_number in match:
     if s_number not in all_season:

       all_season.append(str(s_number))
       all_season_tvdb_data.append({"name":ep_name,"episode_number":ep_num,"air_date":aired,"season_number":s_number,"poster_path":iconimage})
   try:
       url2='http://api.themoviedb.org/3/tv/%s?api_key=%s&language=en&append_to_response=external_ids'%(id,tmdbKey)
      
       
       imdb_id=requests.get(url2).json()['external_ids']['imdb_id']
       xx=requests.get('https://www.imdb.com/title/%s/episodes'%imdb_id).content
       regex='<label for="bySeason">(.+?)</div'
       match_imdb_s_pre=re.compile(regex,re.DOTALL).findall(xx)[0]
       regex='<option.+?value="(.+?)"'
       match_imdb_s=re.compile(regex).findall(match_imdb_s_pre)
       regex_img='<img itemprop="image".+?src="(.+?)"'
       img_imdb_pre=re.compile(regex_img,re.DOTALL).findall(xx)
       if len (img_imdb_pre)>0:
            img_imdb=img_imdb_pre[0]
       else:
            img_imdb=' '
       for s_number in match_imdb_s:
            all_season_imdb.append(str(s_number))
            all_season_imdb_data.append({"name":'0',"episode_number":'0',"air_date":' ',"season_number":s_number,"poster_path":img_imdb,'backdrop_path':img_imdb})
   except:
    pass
   all_season_tmdb=[]
   for data in html['seasons']:
      all_season_tmdb.append(str(data['season_number']))
   for items_a in all_season:
     if items_a not in all_season_tmdb:
       html['seasons'].append(all_season_tvdb_data[all_season.index(items_a)])
       
   for items_a in all_season_imdb:
     if items_a not in all_season_tmdb:
       html['seasons'].append(all_season_imdb_data[all_season_imdb.index(items_a)])
   plot=html['overview']
   original_name=html['original_name']
   if html['original_language']!='en':
    original_name=html['name']
   for data in html['seasons']:
   
     new_name=' Season '.decode('utf8')+str(data['season_number'])
     if data['air_date']!=None:
         year=str(data['air_date'].split("-")[0])
         premired=data['air_date']
     else:
       year=0
       premired=0
     season=str(data['season_number'])
     if data['poster_path']==None:
      icon=iconimage
     else:
       icon=data['poster_path']
     if 'backdrop_path' in data:
         if data['backdrop_path']==None:
          fan=fanart
         else:
          fan=data['backdrop_path']
     else:
        fan=html['backdrop_path']
     ep_number=''
     if 'episode_count' in data:
        ep_number=data['episode_count']
        
     if plot==None:
       plot=' '
     if fan==None:
       fan=fanart
     if 'http' not in fan:
       fan=domain_s+'image.tmdb.org/t/p/original/'+fan
     if 'http' not in icon:
       icon=domain_s+'image.tmdb.org/t/p/original/'+icon
     
     watched=''
     remain=''
      
     
     color='white'
     try:
        if 'air_date' in data:
           
               datea='[COLOR aqua]'+str(time.strptime(data['air_date'], '%Y-%m-%d'))+'[/COLOR]\n'
               
               a=(time.strptime(data['air_date'], '%Y-%m-%d'))
               b=time.strptime(str(time.strftime('%Y-%m-%d')), '%Y-%m-%d')
               
           
               if a>b:
                 color='red'
                 txt_1=' Wait until ... '
               else:
                 txt_1=' Aired At '
                 color='white'
        datea='[COLOR aqua]'+txt_1+time.strftime( "%d-%m-%Y",a) + '[/COLOR]\n'
     except:
             
             datea=''
             color='red'
     if 'season 0' in new_name:
        continue
     aa=addDir3( '[COLOR %s]'%color+new_name+'[/COLOR]',url,19,icon,fan,datea+plot,data=year,original_title=original_name,id=id,season=season,tmdbid=tmdbid,show_original_year=show_original_year,heb_name=original_name,ep_number=ep_number,watched_ep=watched,remain=remain,premired=premired)
     all_d.append(aa)
   xbmcplugin .addDirectoryItems(int(sys.argv[1]),all_d,len(all_d))
def get_episode_data(id,season,episode,yjump=True,o_name=' '):
    o_season=season
    o_episode=episode
    url='http://api.themoviedb.org/3/tv/%s/season/%s/episode/%s?api_key=653bb8af90162bd98fc7ee32bcbbfb3d&language=%s&append_to_response=external_ids'%(id,season,episode,lang)

    html=requests.get(url).json()
   
    if yjump:
      if 'status_code' in html:
        url='http://api.themoviedb.org/3/tv/%s/season/%s/episode/%s?api_key=653bb8af90162bd98fc7ee32bcbbfb3d&language=%s&append_to_response=external_ids'%(id,str(int(season)+1),'1',lang)
        html=requests.get(url).json()
        episode='1'
        season=str(int(season)+1)
    if 'name' in html:
        name=html['name']
        if 'air_date' in html:
            try:
                from datetime import datetime
                from datetime import date
                from datetime import time
                dateTime1 =datetime.strptime(html['air_date'], "%Y-%m-%d")
                if dateTime1.date() > date.today():
                    color='red'
                else:
                    color='lightblue'
            except:
                color='lightblue'
            name=name + ' [COLOR %s](%s)[/COLOR]'%(color,html['air_date'])
        plot=html['overview']
        if html['still_path']!=None:
          image=domain_s+'image.tmdb.org/t/p/original/'+html['still_path']
        else:
          image=' '
        return name,plot,image,season,episode
    else:
       return o_name,' ',' ',o_season,o_episode
def get_episode(name,url,iconimage,fanart,description,data,original_title,id,season,tmdbid,show_original_year):
   import _strptime
   all_d=[]
   url=domain_s+'api.themoviedb.org/3/tv/%s/season/%s?api_key=34142515d9d23817496eeb4ff1d223d0&language=%s'%(id,season,lang)
   tmdbKey = '653bb8af90162bd98fc7ee32bcbbfb3d'
   html=requests.get(url).json()
   #tmdb data
   if 'episodes'  in html:
       if html['episodes'][0]['name']=='':
         url=domain_s+'api.themoviedb.org/3/tv/%s/season/%s?api_key=34142515d9d23817496eeb4ff1d223d0&language=eng'%(id,season)
         html=requests.get(url).json()
   #response = requests.get('http://thetvdb.com/api/0629B785CE550C8D/series/%s/all/he.xml'%tmdbid).content
   
   attr=['Combined_season','FirstAired']
   regex='<Episode>.+?<EpisodeName>(.+?)</EpisodeName>.+?<EpisodeNumber>(.+?)</EpisodeNumber>.+?<FirstAired>(.+?)</FirstAired>.+?<Overview>(.+?)</Overview>.+?<SeasonNumber>(.+?)</SeasonNumber>'
   #match=re.compile(regex,re.DOTALL).findall(response)
   match=[]
   regex_eng='<slug>(.+?)</slug>'
   #match_eng=re.compile(regex_eng).findall(response)
   match_eng=[]
   eng_name=name
   if len (match_eng)>0:
     eng_name=match_eng[0]

   #seasons_tvdb=parseDOM(response,'Episode', attr)

   all_episodes=[]
   all_season_tvdb_data=[]
   
   all_episodes_imdb=[]
   all_episodes_imdb_data=[]
   image2=' '
   for ep_name,ep_num,aired,overview,s_number in match:
     
     image2=fanart
     if s_number==season:
         if ep_num not in all_episodes:
           
           all_episodes.append(str(ep_num))
           all_season_tvdb_data.append({"name":ep_name,"episode_number":ep_num,"air_date":aired,"overview":overview,"season_number":s_number,"still_path":iconimage,"poster_path":image2})
   
   url2='http://api.themoviedb.org/3/tv/%s?api_key=%s&language=en&append_to_response=external_ids'%(id,tmdbKey)
      
       
    
      
       
   imdb_id=requests.get(url2).json()['external_ids']['imdb_id']
   xx=requests.get('https://www.imdb.com/title/%s/episodes?season=%s'%(imdb_id,season)).content

   regex='div class="image">.+?title="(.+?)"(.+?)meta itemprop="episodeNumber" content="(.+?)".+?itemprop="description">(.+?)<'
   match_imdb_s_pre=re.compile(regex,re.DOTALL).findall(xx)
  
   for ep_name,poster,ep_num,plot in match_imdb_s_pre:
        if 'src="' in poster:
            regex='src="(.+?)"'
            poster=re.compile(regex).findall(poster)[0]
        else:
            poster=' '
        all_episodes_imdb.append(str(ep_num))
        all_episodes_imdb_data.append({"name":ep_name,"episode_number":ep_num,"air_date":' ',"season_number":season,"poster_path":poster,'still_path':poster,"overview":plot})
   
  
   all_episodes_tmdb=[]

   if 'episodes' not in html:
     html['episodes']=[]
     html['poster_path']=fanart
   else:
       for data in html['episodes']:
          all_episodes_tmdb.append(str(data['episode_number']))
   for items_a in all_episodes:
     if items_a not in all_episodes_tmdb:
       html['episodes'].append(all_season_tvdb_data[all_episodes.index(items_a)])
   for items_a in all_episodes_imdb:
     if items_a not in all_episodes_tmdb:
       html['episodes'].append(all_episodes_imdb_data[all_episodes_imdb.index(items_a)])
       
   original_name=original_title
   if Addon.getSetting("dp")=='true' and (Addon.getSetting("disapear")=='true' or Addon.getSetting("check_subs")=='true'):
            dp = xbmcgui.DialogProgress()
            dp.create("Loading", "Please wait", '')
            dp.update(0)
   xxx=0
   start_time = time.time()

   if Addon.getSetting("trakt_access_token")!='':
       all_tv_w={}
       if 1:
           i = (call_trakt('/users/me/watched/shows?extended=full'))
           
           for ids in i:
             
             all_tv_w[str(ids['show']['ids']['tmdb'])]=[]
             for seasons in ids['seasons']:
             
              for ep in seasons['episodes']:
               
                all_tv_w[str(ids['show']['ids']['tmdb'])].append(str(seasons['number'])+'x'+str(ep['number']))
          
       #except:
       # pass
   fav_search_f=Addon.getSetting("fav_search_f_tv")
   fav_servers_en=Addon.getSetting("fav_servers_en_tv")
   fav_servers=Addon.getSetting("fav_servers_tv")
   google_server= Addon.getSetting("google_server_tv")
   rapid_server=Addon.getSetting("rapid_server_tv")
   direct_server=Addon.getSetting("direct_server_tv")
   heb_server=Addon.getSetting("heb_server_tv")
   if  fav_search_f=='true' and fav_servers_en=='true' and (len(fav_servers)>0 or heb_server=='true' or google_server=='true' or rapid_server=='true' or direct_server=='true'):

        fav_status='true'
   else:
       fav_status='false'
   from datetime import datetime
   '''
   try:
        from sqlite3 import dbapi2 as database
   except:
        from pysqlite2 import dbapi2 as database
   cacheFile=os.path.join(user_dataDir,'database.db')
   dbcon = database.connect(cacheFile)
   dbcur = dbcon.cursor()
   dbcur.execute("CREATE TABLE IF NOT EXISTS %s ( ""name TEXT, ""tmdb TEXT, ""season TEXT, ""episode TEXT,""playtime TEXT,""total TEXT, ""free TEXT);" % 'playback')
   dbcon.commit()
    
   dbcur.execute("SELECT * FROM playback where tmdb='%s' and season='%s' "%(id,str(season).replace('%20','0').replace(' ','0')))
   match = dbcur.fetchall()
   '''
   all_w={}

   for n,t,s,e,p,t,f in match:
        ee=str(e)
        all_w[ee]={}
        all_w[ee]['resume']=str(p)
        all_w[ee]['totaltime']=str(t)
    
            
   for data in html['episodes']:
     plot=data['overview']
     new_name=str(data['episode_number'])+" . "+data['name']
     air_date=''
     if 'air_date' in data:
       if data['air_date']!=None:
         
         year=str(data['air_date'].split("-")[0])
       else:
         year=0
     else:
       year=0
     
     if data['still_path']!=None:
       if 'https' not in data['still_path']:
         image=domain_s+'image.tmdb.org/t/p/original/'+data['still_path']
       else:
         image=data['still_path']
       
     elif html['poster_path']!=None:
      if 'https' not in html['poster_path']:
       image=domain_s+'image.tmdb.org/t/p/original/'+html['poster_path']
      else:
         image=html['poster_path']
     else:
       image=fanart
     if html['poster_path']!=None:
      if 'https' not in html['poster_path']:
       icon=domain_s+'image.tmdb.org/t/p/original/'+html['poster_path']
      else:
        icon=html['poster_path']
     else:
       icon=iconimage
     #if image2==fanart:
     #  icon=iconimage
      
     #  image=fanart
     color2='white'
     try:
        premired=' '
        if 'air_date' in data:
               premired=data['air_date']
               datea='[COLOR aqua]'+str(time.strptime(data['air_date'], '%Y-%m-%d'))+'[/COLOR]\n'
               
               a=(time.strptime(data['air_date'], '%Y-%m-%d'))
               b=time.strptime(str(time.strftime('%Y-%m-%d')), '%Y-%m-%d')
               
           
               if a>b:
                 color2='red'
               else:
                 
                 color2='white'
        datea='[COLOR aqua]'+' Aired at '+time.strftime( "%d-%m-%Y",a) + '[/COLOR]\n'
     except:
             
             datea=''
             color2='red'
     f_subs=[]
     
     
     if Addon.getSetting("check_subs")=='true' or Addon.getSetting("disapear")=='true':
         import cache
         from subs import get_links
         f_subs=cache.get(get_links,9999,'tv',original_name,original_name,season,str(data['episode_number']),season,str(data['episode_number']),year,id,True, table='pages')
         
         
     
     

     
     color=color2
     if season!=None and season!="%20":
        tv_movie='tv'
     else:
       tv_movie='movie'
     
     elapsed_time = time.time() - start_time
     if (Addon.getSetting("check_subs")=='true'  or Addon.getSetting("disapear")=='true') and Addon.getSetting("dp")=='true':
        dp.update(int(((xxx* 100.0)/(len(html['episodes']))) ), ' Please wait '+ time.strftime("%H:%M:%S", time.gmtime(elapsed_time)),'[COLOR'+color+']'+new_name+'[/COLOR]')
     xxx=xxx+1
     if  Addon.getSetting("disapear")=='true' and color=='red':
        a=1
     else:
     
       watched='no'
       if Addon.getSetting("trakt_access_token")!='':
           if id in all_tv_w:
             if season+'x'+str(data['episode_number']) in all_tv_w[id]:
              watched='yes'
       
       
       aa=addDir3( '[COLOR %s]'%color+new_name+'[/COLOR]', url,15, icon,image,datea+plot,data=year,original_title=original_name,id=id,season=season,episode=data['episode_number'],eng_name=eng_name,show_original_year=show_original_year,heb_name=original_name,watched=watched,fav_status=fav_status,all_w=all_w,premired=premired)
       all_d.append(aa)
   #dbcur.close()
   #dbcon.close()
   xbmcplugin .addDirectoryItems(int(sys.argv[1]),all_d,len(all_d))
     #xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_EPISODE)
     #xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_SORT_TITLE)
     
