# -*- coding: utf-8 -*-
import xml.dom.minidom
import base64
import six
if six.PY3:
    from urllib.parse import urlparse, parse_qs, quote_plus, quote, unquote_plus, unquote
else:
    from urlparse import urlparse, parse_qs
    from urllib import quote_plus, quote, unquote_plus, unquote
import re
from datetime import datetime
try:
    from lib.dns import CloudflareHTTP
except ImportError:
    from dns import CloudflareHTTP
import six
try:
    from lib import acesso
except ImportError:
    import acesso


def extract_info(url):
    # Parseia a URL
    parsed_url = urlparse(url)
    protocol = parsed_url.scheme
    # Extrai o host e a porta
    host = parsed_url.hostname
    # Define a porta padrão se não estiver especificada
    if parsed_url.port:
        port = parsed_url.port
    else:
        port = 80 if parsed_url.scheme == 'http' else 443
    
    # Extrai o username e o password dos parâmetros da query
    query_params = parse_qs(parsed_url.query)
    username = query_params.get('username', [None])[0]
    password = query_params.get('password', [None])[0]
    dns = '{0}://{1}:{2}'.format(protocol,host,port)
    
    return dns, username, password


def parselist(url):
    iptv = []
    try:
        r = CloudflareHTTP.get(url, timeout=4)
        if r and 'response' in r:
            res = r['response']
            url = res.json()['url']
    except:
        pass
    try:
        if 'paste.kodi.tv' in url and not 'documents' in url and not 'raw' in url:
            try:
                key = url.split('/')[-1]
                url = 'https://paste.kodi.tv/documents/' + key
                r = CloudflareHTTP.get(url, timeout=4)
                if r and 'response' in r:
                    res = r['response']
                    src = res.json()['data']
                    lines = src.split('\n')
                    if lines:
                        for i in lines:
                            i = i.replace(' ', '')
                            if 'http' in i:
                                dns, username, password = extract_info(i)
                                iptv.append((dns,username,password))
            except:
                pass
        else:
            r = CloudflareHTTP.get(url, timeout=4)
            if r and 'response' in r:
                res = r['response']
                src = res.text
                lines = src.split('\n')
                if lines:
                    for i in lines:
                        i = i.replace(' ', '')
                        if 'http' in i:
                            dns, username, password = extract_info(i)
                            iptv.append((dns,username,password))
    except:
        pass
    return iptv




def ordenar_resolucao(item):
    #name = item[0]
    name = item.get('name', '')
    if 'FHD' in name:
        return 1
    elif 'HD' in name:
        return 2
    elif '4K' in name:
        return 3    
    elif 'SD' in name:
        return 4
    return 5 

def remove_emojis(text):    
    try:
        # Verificar se está rodando no Python 2
        if six.PY2:
            # Verificar se 'text' é uma string Unicode no Python 2
            if not isinstance(text, unicode):  # Python 2: converter para unicode
                text = text.decode("utf-8")
            # Remover emojis e símbolos especiais (inclui várias faixas de símbolos)
            highpoints = re.compile(u'[\u2600-\u26FF\u2700-\u27BF\u2B50-\u2B59\uD800-\uDBFF][\uDC00-\uDFFF]|[\U00010000-\U0010ffff]')
        else:
            # No Python 3, todas as strings são Unicode
            highpoints = re.compile(r'[\u2600-\u26FF\u2700-\u27BF\u2B50-\u2B59\U00010000-\U0010ffff]', flags=re.UNICODE)

        # Substituir todos os caracteres de emojis e símbolos especiais por uma string vazia
        text = highpoints.sub(r'', text)
    except:
        pass
    try:
        text = text.replace('  ', ' ')
    except:
        pass
    return text


    

class API:
    def __init__(self, dns, username, password):
        self.live_url = '{0}/enigma2.php?username={1}&password={2}&type=get_live_categories'.format(dns, username, password)
        self.live_url2 = '{0}/player_api.php?username={1}&password={2}&action=get_live_categories'.format(dns, username, password)
        self.vod_url = '{0}/enigma2.php?username={1}&password={2}&type=get_vod_categories'.format(dns, username, password)
        self.vod_url2 = '{0}/player_api.php?username={1}&password={2}&action=get_vod_categories'.format(dns, username, password)
        self.series_url = '{0}/enigma2.php?username={1}&password={2}&type=get_series_categories'.format(dns, username, password)
        self.player_api = '{0}/player_api.php?username={1}&password={2}'.format(dns, username, password)
        self.play_url = '{0}/live/{1}/{2}/'.format(dns,username,password)
        self.play_movies = '{0}/movie/{1}/{2}/'.format(dns,username,password)
        self.play_series = '{0}/series/{1}/{2}/'.format(dns,username,password)
        self.adult_tags = ['xxx','xXx','XXX','adult','Adult','ADULT','adults','Adults','ADULTS','porn','Porn','PORN']
        try:
            self.hide_adult = acesso.getsetting('hidexxx')
        except:
            self.hide_adult = 'false'

    def http(self, url='', mode=None):
        if not mode:
            try:
                r = CloudflareHTTP.get(url, timeout=4)
                if r and 'response' in r:
                    res = r['response']
                    src = res.content                
                    return src
                return   
            except:
                return
        elif mode == 'channels_category':
            try:
                r = CloudflareHTTP.get(self.live_url, timeout=4)
                if r and 'response' in r:
                    res = r['response']
                    src = res.content                
                    return src
                return
            except:
                return
        elif mode == 'json_url':
            try:
                r = CloudflareHTTP.get(url,timeout=4)
                if r and 'response' in r:
                    res = r['response']
                    src = res.json()               
                    return src
                return
            except:
                return
            return            
        elif mode == 'vod':
            try:
                r = CloudflareHTTP.get(url, timeout=4)
                if r and 'response' in r:
                    res = r['response']
                    src = res.text
                    return src
                return
            except:
                return
        return ''

    
    def check_login(self):
        ok = False
        try:
            parse = self.http(self.player_api, 'json_url')
            login_data = parse['user_info']['auth']
            if login_data == 0:
                ok = False
            else:
                ok = True
        except:
            pass
        return ok
    
    def MonthNumToName(self,num):
        if '01' in num:
            month = 'Janeiro'
        elif '02' in num:
            month = 'Fevereiro'
        elif '03' in num:
            month = 'Março'
        elif '04' in num:
            month = 'Abril'
        elif '05' in num:
            month = 'Maio'
        elif '06' in num:
            month = 'Junho'
        elif '07' in num:
            month = 'Julho'
        elif '08' in num:
            month = 'Agosto'
        elif '09' in num:
            month = 'Setembro'
        elif '10' in num:
            month = 'Outubro'
        elif '11' in num:
            month = 'Novembro'
        elif '12' in num:
            month = 'Dezembro'
        return month
    
    def account_info(self):
        itens = []
        try:
            parse = self.http(self.player_api, 'json_url')
            expiry = parse['user_info']['exp_date']
            if not expiry=="" and not expiry == None:
                expiry = datetime.fromtimestamp(int(expiry)).strftime('%d/%m/%Y - %H:%M')
                expreg = re.compile('^(.*?)/(.*?)/(.*?)$',re.DOTALL).findall(expiry)
                for day,month,year in expreg:  
                    month = self.MonthNumToName(month)
                    year = re.sub(' -.*?$','',year)
                    expiry = day+' de '+month+' de '+year
            else:
                expiry = 'Ilimitado'
            max_connection = str(parse['user_info']['max_connections'])
            if max_connection == 'None':
                max_connection = 'Ilimitado'
            status = parse['user_info']['status']
            if status == 'Active':
                status = 'Ativo'
            elif status == 'Trial':
                status = 'Teste'
            expira_ = '[B][COLOR white]Expira em: [/COLOR][/B] '+expiry
            status_ = '[B][COLOR white]Status: [/COLOR][/B] %s'%status
            conexoes_atuais_ = '[B][COLOR white]Conexões Atual: [/COLOR][/B] '+ str(parse['user_info']['active_cons'])
            conexoes_permitidas_ = '[B][COLOR white]Conexões Permitidas: [/COLOR][/B] '+ max_connection
            itens.append(expira_)
            itens.append(status_)
            itens.append(conexoes_atuais_)
            itens.append(conexoes_permitidas_)
        except:
            pass  
        return itens
        
        
    def b64(self, obj):
        return base64.b64decode(obj).decode('utf-8')
    
    def check_protocol(self,url):
        parsed = urlparse(self.live_url)
        protocol = parsed.scheme
        if protocol=='https':
            return url.replace('http','https')
        return url
    
    def regex_from_to(self, text, from_string, to_string, excluding=True):
        if excluding:
            try: r = re.search("(?i)" + from_string + "([\S\s]+?)" + to_string, text).group(1)
            except: r = ''
        else:
            try: r = re.search("(?i)(" + from_string + "[\S\s]+?" + to_string + ")", text).group(1)
            except: r = ''
        return r 

    def regex_get_all(self, text, start_with, end_with):
        r = re.findall("(?i)(" + start_with + "[\S\s]+?" + end_with + ")", text)
        return r       

    
    def channels_category(self):
        itens = []
        try:
            vod_cat = self.http(self.live_url2, 'json_url')
            if vod_cat:
                for cat in vod_cat:
                    name = cat['category_name']
                    url = self.player_api + '&action=get_live_streams&category_id=' + str(cat['category_id'])
                    if not 'All' in name:
                        i = {'name': name, 'url': url}
                        if self.hide_adult == 'true':                      
                            itens.append(i)
                        else:
                            if not any(s in name for s in self.adult_tags):
                                itens.append(i)
        except:
            pass                           
        return itens
    
    def channel_id(self,json_data,n):
        if json_data:
            for i, d in enumerate(json_data):
                if n == i:
                    return d.get('stream_id', '')
        return ''

    
    def channels_open(self,url):
        itens = []
        vod_cat = self.http(url, 'json_url')
        if vod_cat:
            for cat in vod_cat:
                name = cat['name']
                stream_id = str(cat['stream_id'])
                url_ = '{0}{1}.m3u8'.format(self.play_url,stream_id)
                try:
                    thumb = cat['stream_icon']
                except:
                    thumb = ''
                i = {'id': str(stream_id), 'name': name, 'url': url_, 'iconimage': thumb}
                itens.append(i)
        
        if itens:
            try:
                itens = sorted(itens, key=ordenar_resolucao)
            except:
                pass
        return itens

    def series_cat(self):
        itens = []
        url_series = '{0}&action=get_series_categories'.format(self.player_api)
        vod_cat = self.http(url_series,'json_url')
        if vod_cat:
            for cat in vod_cat:
                name = cat['category_name']
                try:
                    name = remove_emojis(name)
                except:
                    pass
                url = self.player_api + '&action=get_series&category_id='+cat['category_id']                
                if self.hide_adult == 'true':
                    itens.append((name,url))
                else:
                    if not any(s in name for s in self.adult_tags):
                        itens.append((name,url))
        return itens
    
    def series_list(self,url):
        itens = []
        ser_cat = self.http(url,'json_url')
        if ser_cat:
            for ser in ser_cat:	
                name = ser['name']
                url = self.player_api+'&action=get_series_info&series_id='+str(ser['series_id'])
                try:
                    thumb = ser['cover']
                    background = ser['backdrop_path'][0]
                    plot = ser['plot']
                    releaseDate = ser['releaseDate']
                    cast = str(ser['cast']).split()
                    rating_5based = ser['rating_5based']
                    episode_run_time = str(ser['episode_run_time'])
                    genre = ser['genre']
                except:
                    thumb = ''
                    plot = ''
                    releasedate = ''
                    cast = ('', '')
                    rating_5based = ''
                    episode_run_time = ''
                    genre = ''
                itens.append((name,url,thumb,background,plot,releaseDate,cast,rating_5based,episode_run_time,genre)) # series seasons
        return itens
    
    def series_seasons(self,url):
        itens = []
        ser_cat = self.http(url,'json_url')
        if ser_cat:
            for ser in ser_cat['episodes']:
                info = ser_cat['info']
                try:
                    thumb = info['cover']
                except:
                    thumb = ''
                try:
                    background = info['backdrop_path'][0]
                except:
                    background = ''
                name = 'Temporada - '+ser
                url_ = url+'&season_number='+str(ser)
                itens.append((name,url_,thumb,background)) # season list
        return itens
    
    def season_list(self,url):
        itens = []
        ser_cat = self.http(url,'json_url')
        info = ser_cat['info']
        ser_cat = ser_cat['episodes']
        parsed_url = urlparse(url)
        season_number = str(parse_qs(parsed_url.query)['season_number'][0])
        for ser in ser_cat[season_number]:
            url = self.play_series+str(ser['id'])+'.'+ser['container_extension']
            name = ser['title']
            try:
                thumb = ser['info']['movie_image']
            except:
                thumb = ''
            try:
                background = ser['info']['movie_image']
            except:
                background = ''
            try:
                plot = ser['info']['plot']
            except:
                plot = ''
            try:
                releasedate = ser['info']['releasedate']
            except:
                releasedate = ''
            try:
                cast = str(info['cast']).split()
            except:
                cast = ('', '')
            try:
                rating_5based = info['rating_5based']
            except:
                rating_5based = ''
            try:
                duration = str(ser['info']['duration'])
            except:
                duration = ''
            try:
                genre = info['genre']
            except:
                genre = ''
            itens.append((name,url,thumb,background,plot,releasedate,cast,rating_5based,duration,genre))
        return itens 

    def vod(self,url=''):
        # exemplo no addon
        # tipo = t[0]
        # if tipo == 'dir':
        #     d, name1, name2 = t        
        itens = []
        if not url:
            open = self.http(self.vod_url, mode='vod')
        else:
            open = self.http(url, 'vod')
        if open:
            all_cats = self.regex_get_all(open,'<channel>','</channel>')
            if all_cats:
                for a in all_cats:
                    if '<playlist_url>' in open:
                        name = str(self.b64(self.regex_from_to(a,'<title>','</title>'))).replace('?','')
                        url = self.check_protocol(self.regex_from_to(a,'<playlist_url>','</playlist_url>').replace('<![CDATA[','').replace(']]>',''))
                        if not 'All' in name: 
                            if self.hide_adult == 'false':
                                itens.append(('dir', name,url))
                            else:
                                if not any(s in name for s in self.adult_tags):
                                    itens.append(('dir', name,url))
                    else:
                        try:
                            name = self.b64(self.regex_from_to(a,'<title>','</title>'))
                            thumb= self.regex_from_to(a,'<desc_image>','</desc_image>').replace('<![CDATA[','').replace(']]>','')
                            url = self.check_protocol(self.regex_from_to(a,'<stream_url>','</stream_url>').replace('<![CDATA[','').replace(']]>',''))
                            desc = self.b64(self.regex_from_to(a,'<description>','</description>'))
                            plot = self.regex_from_to(desc,'PLOT:','\n')
                            try:
                                cast = self.regex_from_to(desc,'CAST:','\n')
                            except:
                                cast = ('', '')
                            ratin= self.regex_from_to(desc,'RATING:','\n')
                            year = self.regex_from_to(desc,'RELEASEDATE:','\n').replace(' ','-')
                            year = re.compile('-.*?-.*?-(.*?)-',re.DOTALL).findall(year)
                            runt = self.regex_from_to(desc,'DURATION_SECS:','\n')
                            genre= self.regex_from_to(desc,'GENRE:','\n')
                            background = ''
                            itens.append(('play', name, url, thumb, background,plot,str(year).replace("['","").replace("']",""),str(cast).split(),ratin,genre))
                        except:pass
        return itens


    def vod2(self):
        itens = []
        vod_cat = self.http(self.vod_url2, 'json_url')
        if vod_cat:
            for cat in vod_cat:
                name = cat['category_name']
                try:
                    name = remove_emojis(name)
                except:
                    pass                
                link = self.player_api + '&action=get_vod_streams&category_id=' + str(cat['category_id'])
                if self.hide_adult == 'true':
                    itens.append((name,link))
                else:
                    if not any(s in name for s in self.adult_tags):
                        itens.append((name,link))
        return itens
    
    def Vodlist(self,url):
        itens = []
        vod_cat = self.http(url, 'json_url')
        if vod_cat:
            for cat in vod_cat:
                try:
                    thumb = cat['stream_icon']
                except:
                    thumb = ''
                link = self.play_movies + str(cat['stream_id'])+'.' + cat['container_extension']
                name = str(cat['name'])
                if self.hide_adult == 'true':
                    itens.append((name,link,thumb))
                else:
                    if not any(s in name for s in self.adult_tags):
                        itens.append((name,link,thumb))
        return itens
    
    def get_epg(self, channel):
        text_epg = ''
        url = self.player_api + '&action=get_short_epg&stream_id=' + str(channel)
        src = self.http(url,'json_url')
        if src:
            epgs = src.get('epg_listings', [])
            if epgs:
                for i in epgs:
                    hora_inicio = i.get('start', '')
                    if hora_inicio:
                        hora_inicio = hora_inicio.split(' ')[1]
                        try:
                            if hora_inicio.count(':') > 1:
                                hora_inicio = hora_inicio[:-3]
                        except:
                            pass
                    else:
                        hora_inicio = ''
                    hora_fim = i.get('stop', '')
                    if hora_fim:
                        hora_fim = hora_fim.split(' ')[1]
                        try:
                            if hora_fim.count(':') > 1:
                                hora_fim = hora_fim[:-3]
                        except:
                            pass                        
                    else:
                        hora_fim = ''
                    if hora_inicio and hora_fim: 
                        tempo = str(hora_inicio) + ' - ' + str(hora_fim)
                    else:
                        tempo = ''
                    title = i.get('title', '')
                    if title:
                        title = self.b64(title)
                    else:
                        title = ''
                    #desc = i.get('description', '')
                    desc = False
                    if title and tempo:
                        #text_epg += str(title) + ': ' + str(tempo) + '\n'
                        text_epg += str(tempo) + ' => ' + str(title) + '\n'
                        if desc:
                            desc = self.b64(desc)
                            text_epg += 'Desc: '+ str(desc) + '\n'

        return text_epg
    

