# -*- coding: UTF-8 -*-
import sys,re,os
import six, xbmc
from six.moves import urllib_request, urllib_parse
if six.PY3:
#if six.PY3:
    LOGNOTICE = xbmc.LOGINFO
    from resources.lib.cmf3 import parseDOM
else:
    LOGNOTICE = xbmc.LOGNOTICE
    from resources.lib.cmf2 import parseDOM

if six.PY3:
    unicode = str
    basestring = str

#import urlparse
import hashlib
import base64
#import urllib
import requests
#from CommonFunctions import parseDOM
import resources.lib.pyaes as pyaes

UA='Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0'
headersok = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'pl,en-US;q=0.7,en;q=0.3',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',}
    
main_url='https://www.hulkstream.com/'
s = requests.Session()

def __bytes_to_key(password, salt, output=48):
    try:
        seed = hashlib.md5(password + salt).digest()
        key = seed
    
        while len(key) < output:
            seed = hashlib.md5(seed + (password + salt)).digest()
            key += seed
    
        return key[:output]
    
    except Exception:
        return

def decryptaes(encrypt):
    encrypted=re.findall(r'(.+?)\,',encrypt)[0]
    password=re.findall(r'\,(.*$)',encrypt)[0]
    encrypted=urllib_parse.unquote(encrypted).replace('"','')
    password=urllib_parse.unquote(password).replace('"','')    
    password=str(password)
    try:
        encrypted = base64.b64decode(encrypted)
        salt = encrypted[8:16]
        key_iv = __bytes_to_key(password, salt)
        key = key_iv[:32]
        iv = key_iv[32:]
        decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCBC(key, iv = iv))
        plaintext = decrypter.feed(encrypted[16:])
        plaintext += decrypter.feed()    
        return plaintext    
    except Exception:
        return
        
#def getIcons(nazwa):
#    return 'https://raw.githubusercontent.com/Proximus2000/mb-support/master/flags/%s.png'
        
def getCompetition(nazwa):
    out=[]
    if 'europe' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/uefa/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'UEFA'}, {'href': 'https://www.javafooty.com/league/england/', 'img': 'https://www.javafooty.com/flags/en.gif', 'title': 'England'}, {'href': 'https://www.javafooty.com/league/spain/', 'img': 'https://www.javafooty.com/flags/es.gif', 'title': 'Spain'}, {'href': 'https://www.javafooty.com/league/italy/', 'img': 'https://www.javafooty.com/flags/it.gif', 'title': 'Italy'}, {'href': 'https://www.javafooty.com/league/germany/', 'img': 'https://www.javafooty.com/flags/de.gif', 'title': 'Germany'}, {'href': 'https://www.javafooty.com/league/france/', 'img': 'https://www.javafooty.com/flags/fr.gif', 'title': 'France'}, {'href': 'https://www.javafooty.com/league/netherlands/', 'img': 'https://www.javafooty.com/flags/nl.gif', 'title': 'Netherlands'}, {'href': 'https://www.javafooty.com/league/portugal/', 'img': 'https://www.javafooty.com/flags/pt.gif', 'title': 'Portugal'}, {'href': 'https://www.javafooty.com/league/turkey/', 'img': 'https://www.javafooty.com/flags/tr.gif', 'title': 'Turkey'}, {'href': 'https://www.javafooty.com/league/greece/', 'img': 'https://www.javafooty.com/flags/gr.gif', 'title': 'Greece'}, {'href': 'https://www.javafooty.com/league/belgium/', 'img': 'https://www.javafooty.com/flags/be.gif', 'title': 'Belgium'}, {'href': 'https://www.javafooty.com/league/scotland/', 'img': 'https://www.javafooty.com/flags/sl.gif', 'title': 'Scotland'}, {'href': 'https://www.javafooty.com/league/albania/', 'img': 'https://www.javafooty.com/flags/al.gif', 'title': 'Albania'}, {'href': 'https://www.javafooty.com/league/armenia/', 'img': 'https://www.javafooty.com/flags/am.gif', 'title': 'Armenia'}, {'href': 'https://www.javafooty.com/league/austria/', 'img': 'https://www.javafooty.com/flags/at.gif', 'title': 'Austria'}, {'href': 'https://www.javafooty.com/league/azerbaijan/', 'img': 'https://www.javafooty.com/flags/az.gif', 'title': 'Azerbaijan'}, {'href': 'https://www.javafooty.com/league/belarus/', 'img': 'https://www.javafooty.com/flags/by.gif', 'title': 'Belarus'}, {'href': 'https://www.javafooty.com/league/bulgaria/', 'img': 'https://www.javafooty.com/flags/bg.gif', 'title': 'Bulgaria'}, {'href': 'https://www.javafooty.com/league/crimea/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'Crimea'}, {'href': 'https://www.javafooty.com/league/croatia/', 'img': 'https://www.javafooty.com/flags/hr.gif', 'title': 'Croatia'}, {'href': 'https://www.javafooty.com/league/cyprus/', 'img': 'https://www.javafooty.com/flags/cy.gif', 'title': 'Cyprus'}, {'href': 'https://www.javafooty.com/league/czech-republic/', 'img': 'https://www.javafooty.com/flags/cz.gif', 'title': 'Czech Republic'}, {'href': 'https://www.javafooty.com/league/denmark/', 'img': 'https://www.javafooty.com/flags/dk.gif', 'title': 'Denmark'}, {'href': 'https://www.javafooty.com/league/estonia/', 'img': 'https://www.javafooty.com/flags/ee.gif', 'title': 'Estonia'}, {'href': 'https://www.javafooty.com/league/faroe-islands/', 'img': 'https://www.javafooty.com/flags/fo.gif', 'title': 'Faroe Islands'}, {'href': 'https://www.javafooty.com/league/finland/', 'img': 'https://www.javafooty.com/flags/fi.gif', 'title': 'Finland'}, {'href': 'https://www.javafooty.com/league/georgia/', 'img': 'https://www.javafooty.com/flags/ge.gif', 'title': 'Georgia'}, {'href': 'https://www.javafooty.com/league/hungary/', 'img': 'https://www.javafooty.com/flags/hu.gif', 'title': 'Hungary'}, {'href': 'https://www.javafooty.com/league/iceland/', 'img': 'https://www.javafooty.com/flags/is.gif', 'title': 'Iceland'}, {'href': 'https://www.javafooty.com/league/ireland/', 'img': 'https://www.javafooty.com/flags/ie.gif', 'title': 'Ireland'}, {'href': 'https://www.javafooty.com/league/kazakhstan/', 'img': 'https://www.javafooty.com/flags/kz.gif', 'title': 'Kazakhstan'}, {'href': 'https://www.javafooty.com/league/lithuania/', 'img': 'https://www.javafooty.com/flags/lt.gif', 'title': 'Lithuania'}, {'href': 'https://www.javafooty.com/league/luxembourg/', 'img': 'https://www.javafooty.com/flags/lu.gif', 'title': 'Luxembourg'}, {'href': 'https://www.javafooty.com/league/macedonia/', 'img': 'https://www.javafooty.com/flags/mk.gif', 'title': 'Macedonia'}, {'href': 'https://www.javafooty.com/league/moldova/', 'img': 'https://www.javafooty.com/flags/md.gif', 'title': 'Moldova'}, {'href': 'https://www.javafooty.com/league/northern-ireland/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'Northern Ireland'}, {'href': 'https://www.javafooty.com/league/norway/', 'img': 'https://www.javafooty.com/flags/no.gif', 'title': 'Norway'}, {'href': 'https://www.javafooty.com/league/poland/', 'img': 'https://www.javafooty.com/flags/pl.gif', 'title': 'Poland'}, {'href': 'https://www.javafooty.com/league/romania/', 'img': 'https://www.javafooty.com/flags/ro.gif', 'title': 'Romania'}, {'href': 'https://www.javafooty.com/league/russia/', 'img': 'https://www.javafooty.com/flags/ru.gif', 'title': 'Russia'}, {'href': 'https://www.javafooty.com/league/serbia/', 'img': 'https://www.javafooty.com/flags/rs.gif', 'title': 'Serbia'}, {'href': 'https://www.javafooty.com/league/slovakia/', 'img': 'https://www.javafooty.com/flags/sk.gif', 'title': 'Slovakia'}, {'href': 'https://www.javafooty.com/league/slovenia/', 'img': 'https://www.javafooty.com/flags/si.gif', 'title': 'Slovenia'}, {'href': 'https://www.javafooty.com/league/sweden/', 'img': 'https://www.javafooty.com/flags/se.gif', 'title': 'Sweden'}, {'href': 'https://www.javafooty.com/league/switzerland/', 'img': 'https://www.javafooty.com/flags/ch.gif', 'title': 'Switzerland'}, {'href': 'https://www.javafooty.com/league/ukraine/', 'img': 'https://www.javafooty.com/flags/ua.gif', 'title': 'Ukraine'}]
    elif 'oceania' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/ofc/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'OFC'}, {'href': 'https://www.javafooty.com/league/new-zealand/', 'img': 'https://www.javafooty.com/flags/nz.gif', 'title': 'New Zealand'}]
    elif 'south america' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/conmebol/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'CONMEBOL'}, {'href': 'https://www.javafooty.com/league/argentina/', 'img': 'https://www.javafooty.com/flags/ar.gif', 'title': 'Argentina'}, {'href': 'https://www.javafooty.com/league/brazil/', 'img': 'https://www.javafooty.com/flags/br.gif', 'title': 'Brazil'}, {'href': 'https://www.javafooty.com/league/uruguay/', 'img': 'https://www.javafooty.com/flags/uy.gif', 'title': 'Uruguay'}, {'href': 'https://www.javafooty.com/league/chile/', 'img': 'https://www.javafooty.com/flags/cl.gif', 'title': 'Chile'}, {'href': 'https://www.javafooty.com/league/bolivia/', 'img': 'https://www.javafooty.com/flags/bo.gif', 'title': 'Bolivia'}, {'href': 'https://www.javafooty.com/league/colombia/', 'img': 'https://www.javafooty.com/flags/co.gif', 'title': 'Colombia'}, {'href': 'https://www.javafooty.com/league/ecuador/', 'img': 'https://www.javafooty.com/flags/ec.gif', 'title': 'Ecuador'}, {'href': 'https://www.javafooty.com/league/guatemala/', 'img': 'https://www.javafooty.com/flags/gt.gif', 'title': 'Guatemala'}, {'href': 'https://www.javafooty.com/league/paraguay/', 'img': 'https://www.javafooty.com/flags/py.gif', 'title': 'Paraguay'}, {'href': 'https://www.javafooty.com/league/peru/', 'img': 'https://www.javafooty.com/flags/pe.gif', 'title': 'Peru'}]
    elif 'north & central america' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/concacaf/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'CONCACAF'}, {'href': 'https://www.javafooty.com/league/usa/', 'img': 'https://www.javafooty.com/flags/us.gif', 'title': 'USA'}, {'href': 'https://www.javafooty.com/league/mexico/', 'img': 'https://www.javafooty.com/flags/mx.gif', 'title': 'Mexico'}]

    elif 'asia' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/afc/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'AFC'}, {'href': 'https://www.javafooty.com/league/australia/', 'img': 'https://www.javafooty.com/flags/au.gif', 'title': 'Australia'}, {'href': 'https://www.javafooty.com/league/bangladesh/', 'img': 'https://www.javafooty.com/flags/bd.gif', 'title': 'Bangladesh'}, {'href': 'https://www.javafooty.com/league/china/', 'img': 'https://www.javafooty.com/flags/cn.gif', 'title': 'China'}, {'href': 'https://www.javafooty.com/league/india/', 'img': 'https://www.javafooty.com/flags/in.gif', 'title': 'India'}, {'href': 'https://www.javafooty.com/league/indonesia/', 'img': 'https://www.javafooty.com/flags/id.gif', 'title': 'Indonesia'}, {'href': 'https://www.javafooty.com/league/japan/', 'img': 'https://www.javafooty.com/flags/jp.gif', 'title': 'Japan'}, {'href': 'https://www.javafooty.com/league/jordan/', 'img': 'https://www.javafooty.com/flags/jo.gif', 'title': 'Jordan'}, {'href': 'https://www.javafooty.com/league/malaysia/', 'img': 'https://www.javafooty.com/flags/my.gif', 'title': 'Malaysia'}, {'href': 'https://www.javafooty.com/league/qatar/', 'img': 'https://www.javafooty.com/flags/qa.gif', 'title': 'Qatar'}, {'href': 'https://www.javafooty.com/league/saudi-arabia/', 'img': 'https://www.javafooty.com/flags/sa.gif', 'title': 'Saudi Arabia'}, {'href': 'https://www.javafooty.com/league/singapore/', 'img': 'https://www.javafooty.com/flags/sg.gif', 'title': 'Singapore'}, {'href': 'https://www.javafooty.com/league/south-korea/', 'img': 'https://www.javafooty.com/flags/kr.gif', 'title': 'South Korea'}, {'href': 'https://www.javafooty.com/league/uae/', 'img': 'https://www.javafooty.com/flags/ae.gif', 'title': 'UAE'}, {'href': 'https://www.javafooty.com/league/uzbekistan/', 'img': 'https://www.javafooty.com/flags/uz.gif', 'title': 'Uzbekistan'}]

    elif 'africa' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/caf/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'CAF'}, {'href': 'https://www.javafooty.com/league/algeria/', 'img': 'https://www.javafooty.com/flags/dz.gif', 'title': 'Algeria'}, {'href': 'https://www.javafooty.com/league/egypt/', 'img': 'https://www.javafooty.com/flags/eg.gif', 'title': 'Egypt'}, {'href': 'https://www.javafooty.com/league/morocco/', 'img': 'https://www.javafooty.com/flags/ma.gif', 'title': 'Morocco'}, {'href': 'https://www.javafooty.com/league/south-africa/', 'img': 'https://www.javafooty.com/flags/za.gif', 'title': 'South Africa'}, {'href': 'https://www.javafooty.com/league/tunisia/', 'img': 'https://www.javafooty.com/flags/tn.gif', 'title': 'Tunisia'}]

    elif 'world' in nazwa.lower():
        out=[{'href': 'https://www.javafooty.com/league/fifa/', 'img': 'https://www.javafooty.com/flags/nof.gif', 'title': 'FIFA'}]

    return out
    
def getFoothighlights(url):

    html=getUrlReqok(url)    
    out=[]    
    zdek=''
    result=parseDOM(html,'table', attrs={'class': "table table-striped"})[0]    
    links = parseDOM(result, 'tr')#[0] 

    if links:
    
        out=[]
        for link in links:
            link= link.replace('&nbsp;','')
            href = parseDOM(link, 'a', ret='href')[0] 

            tyt = re.findall('"50\%">([^>]+)</td><td width="35\%">([^>]+)<',link)[0]
            title = '%s %s'%(tyt[0],tyt[1])
            out.append({'title':title,'href':href})    
    else:
        alstrs=re.findall("""var\s+wwwjavafootycom.+?['"](.+?)['"]""",result)#[0]
        for alstr in alstrs:
            decoded=decodeAlan(alstr,False)
            decoded= urllib_parse.unquote(decoded)
            zdek+=decoded.replace("\'",'"')
        links = parseDOM(zdek, 'li', attrs={'class': "sidenav-item"})#[0] 
        out=[]
        for link in links:
            link= link.replace('&nbsp;','')
            href = parseDOM(link, 'a', ret='href')[0] 
            title= parseDOM(link, 'div')[0] 
            out.append({'title':title,'href':href})    
    return out        

def checkNxtPg(url,page):
    html=getUrlReqok(url)
    result=parseDOM(html,'table', attrs={'class': "table table-striped"})[0]  #<table class="table table-striped">
    links = parseDOM(result, 'tr') 
    if links:
        nextpage=page
    else:
        nextpage=False
    return nextpage
    
def getSportpieces(url,page):

    pgn=page+1
    urln=url + '%d/' %pgn
    url = url + '%d/' %page

    html=getUrlReqok(url)    
    out=[]    
    prevpage=False #gr=False
    nextpage=page+1
    if page>1:
        prevpage=page-1
    if html.find(u'href="#"> » NEXT » <')>0:
        nextpage=False
    result=parseDOM(html,'table', attrs={'class': "table.+?table-striped"})[0]  #<table class="table table-striped">
    links = parseDOM(result, 'tr', attrs={'class': "paneldefault.+?"})
    for link in links:    
        link = link.replace('<font color="#ffffff">','').replace('</font>','')
        try:
            tds = parseDOM(link, 'td') 
            title = tds[0]
            categ = tds[1]
            data = tds[2]
            href = parseDOM(link, 'a', ret='href')[0] 
            imag = ''
            plot='[COLOR khaki]'+categ+'[CR][COLOR blue]'+title+'[/COLOR][CR]'+data
            
            tytul = '%s    [COLOR khaki]%s[/COLOR]    %s'%(title,categ,data)
            out.append({'title':tytul,'href':href,'img':imag,'code':'[COLOR khaki]'+categ+'[/COLOR]', 'plot':plot,'genre':data})
        except:
            pass
    return (out,(prevpage,nextpage))

def getSportpiecesSearchDate(html,url):
    out=[]    
    result=parseDOM(html,'tbody')[0]
    trs=parseDOM(result,'tr')#[0]
    for tr in trs:
        if '"bg-primary"' in tr:
            
            if 'javafoot' in url:
                title=re.findall('>([^<]+)</span>',tr)[0]
                title='[COLOR khaki]'+title+'[/COLOR]'
                continue
            else:
                title=re.findall('>([^<]+)</span>',tr)[0]
                title='[COLOR khaki]'+title+'[/COLOR]'
                continue
        try:
            data = parseDOM(tr, 'td')[1]
            imag = ''
            href = parseDOM(tr, 'a', ret='href')[0]   
            titlem=parseDOM(tr,'td')[2]          
            titlem = '[COLOR blue]%s[/COLOR]'%titlem
            plot='[COLOR khaki]'+title+'[CR][COLOR blue]'+titlem+'[/COLOR][CR]'+data            
        except:
            continue
        out.append({'title':titlem,'url':href,'img':imag,'code':title, 'plot':plot,'genre':data})
    return out
        
def getSportpiecesSearch(html,url):    
    out=[]
    result=parseDOM(html,'div', attrs={'class': "table-responsive"})[0] 
    links = parseDOM(result, 'tr') 
    for link in links:    
        try:
            if 'javafooty' in url:
                categ=re.findall('</span>([^<]+)<span',link)[0].upper()
                href = parseDOM(link, 'a', ret='href')[0]     
                titledata=re.findall('<td>([^<]+)</td><td><span class="\w+"></span>([^<]+)<',link)#[0]
                if titledata:
                    title=titledata[0][1]
                    data=titledata[0][0]    
                    categ2=''                    
            else:
                categtitledata=re.findall('>([^<]\w+)<.+?>([^<]+)</span></a></td><td>([^<]+)</td><td>([^<]+)<',link)
                if categtitledata:
                    categ2=categtitledata[0][0]
                    categ2=', '+categ2
                    categ=categtitledata[0][1]
                    title=categtitledata[0][2]
                    data=categtitledata[0][3]
                else:
                    categ=''
                    categ2=''
                    title=''
                    data= ''    
                    continue                    
                href = parseDOM(link, 'a', ret='href')[2] 
            imag = ''
            title1 = '[COLOR blue]%s[/COLOR]'%title
            plot='[COLOR khaki]'+categ+'[CR][COLOR blue]'+title+'[/COLOR][CR]'+data
            out.append({'title':title1,'url':href,'img':imag,'code':'[COLOR khaki]'+categ+categ2+'[/COLOR]', 'plot':plot,'genre':data})
        except:
            pass
    return out    
    
def Listdate(url,query):
    query=(str(query).replace(' ','0')).split('/')
    data='%s/%s/%s'%(query[1],query[0],query[2])
    typ=False
    html=postUrlReq(url,data,typ)
    links = getSportpiecesSearchDate(html,url)
    return links

def ListsearchhighNew(query):
    query=query.replace(' ','+')
    typ=True
    url=' '
    html=postUrlReq(url,query,typ)
    out=[]
    xbmc.log('@#@CHANNEL-sess.cookiesx12: %s' % str(html), LOGNOTICE)
    result=parseDOM(html,'table', attrs={'class': "table table\-dark.+?"})[0] 
#table table-dark table-striped "
    links = parseDOM(result, 'tr', attrs={'class': "paneldefault.+?"})
   # xbmc.log('@#@CHANNEL-sess.cookiesx12: %s' % str(links), LOGNOTICE)
    for link in links:    
        link = link.replace('<font color="#ffffff">','').replace('</font>','')
        try:
            tds = parseDOM(link, 'td') 
            title = tds[0]
            categ = tds[1]
            data = tds[2]
            href = parseDOM(link, 'a', ret='href')[0] 
            imag = ''
            plot='[COLOR khaki]'+categ+'[CR][COLOR blue]'+title+'[/COLOR][CR]'+data
            
            tytul = '%s    [COLOR khaki]%s[/COLOR]    %s'%(title,categ,data)
            out.append({'title':tytul,'href':href,'img':imag,'code':'[COLOR khaki]'+categ+'[/COLOR]', 'plot':plot,'genre':data})
        except:
            pass

    return out
    
def Listsearchhigh(url,query):
    query=query.replace(' ','+')
    typ=True

    html=postUrlReq(url,query,typ)
    
    links = getSportpiecesSearch(html,url)
    return links

def getUrlReqok(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'pl,en-US;q=0.7,en;q=0.3',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',}
    content=s.get(url,headers=headers, verify=False).text    
   # if six.PY3:
    #   content= content.decode(encoding='utf-8', errors='strict')
    return content    
    
def getUrlReq(url):
    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'en-US,en;q=0.9',
        'Referer': url,
    }
    content=s.get(url,headers=headers, verify=False).text    
    if six.PY3:
       content= content.encode(encoding='utf-8', errors='strict')
    return content    
def postUrlReq(url,data,typ):

    headerspo = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Referer': 'https://www.javafooty.com/',
        'Upgrade-Insecure-Requests': '1',}
    if typ:
        data = {
        'inpsearch': data}
        url='https://www.javafooty.com/tv'
    else:
        data = {
        'datematchsport': data,
        'submit1': 'Submit'}
        url='https://www.javafooty.com/'
    response = s.post(url, data=data,headers=headerspo).text
   # if six.PY3:
   #    response= response.encode(encoding='utf-8', errors='strict')
    return response
    
def decodeAlan(alantvcom_streaminglive,data):
    ex1 = alantvcom_streaminglive.split('_javafooty_football_highlights_goals_video_');
    p = ex1[1].split('_');
    t0 = int(p[0]);
    t1 = int(p[0]) + int(p[1]);
    aaa=p[2]
    abac=t1+int(aaa)
    t2 = int(p[0]) + int(p[1]) + int(p[2]);
    j0 = ex1[0][0: int(t0)];
    j0 = ex1[0][0: int(t0)];
    k0 = int(ex1[0][int(t0) - 1]) + 1;
    j1 = ex1[0][int(t0): int(t0)+int(p[1])];
    k1 = int(ex1[0][int(t1) - 1]) + 1;
    j2 = ex1[0][int(t1):int(t1)+ int(p[2])];
    k2 = int(ex1[0][int(t2) - 1]) + 1;
    a = '';
    s0 = urllib_parse.unquote(j0[0: len(j0) - k0]);
    abacad=int(len(j0)-k0)
    t = '';
    abacada=int(j0[len(j0)-k0:abacad+1])
    for i in range(0, len(s0)):
        t += chr(ord(s0[i])-abacada    )
    z = urllib_parse.unquote(t);
    a0 = '';
    for ii in range(0, len(z)):
        qq=len(z) - ii
        a0 += z[len(z) - ii: qq+1];
    a = a + a0;
    s1 = urllib_parse.unquote(j1[0: len(j1) - k1]);
    t = '';
    for i in range(0, len(s1)):
        abacad=int(len(j1) - k1)
        abacada=int(j1[len(j1) - k1:abacad+1])
        t += chr(ord(s1[i])-abacada    )
    z = urllib_parse.unquote(t);
    a1 = '';
    for ii in range(0, len(z)):
        a1 += z[len(z) - ii:(len(z) - ii)+ 1];
    a = a + urllib_parse.quote(a1);
    s2 = urllib_parse.unquote(j2[0: len(j2) - k2]);
    t = '';
    for i in range(0, len(s2)):
        abacad=int(len(j2) - k2)
        abacada=int(j2[len(j2) - k2:abacad+1])
        t += chr(ord(s2[i])-abacada    )   
    z = urllib_parse.unquote(t);
    a2 = '';
    for ii in range(0, len(z)):
        a2 += z[len(z) - ii:(len(z) - ii)+ 1];
    a = a + a2;
    return a    

def getJSlinkNew(url,nazwa):

    html=getUrlReqok(url)
    out=[]    
    outw=[]    

    resultwynik = parseDOM(html,'div', attrs={'class': "collapse show",'id': "shop-filters-1"})[0] 
    druz = re.findall('Match : ([^<]+)<',resultwynik)[0] 
    resultat = re.findall('Result : ([^<]+)<',resultwynik)[0] 
    results=parseDOM(html,'div', attrs={'class': "row no-gutters"})
    co=0

    for result in results:
        if 'HIGHLIGHTS VIDEOS LINK' in result:
            vidz = re.findall('<script>(.+?)</script>',result)
            for vid in vidz:
                hreftit = re.findall('font color="#ffffff">([^>]+)</font><br/>.*?src="([^"]*)"',vid)
                co+=1

                for title,href in hreftit:
                    #if 'about:blank' in href:
                    #    href = parseDOM(vid, 'iframe', ret='src')[0] 
                    href  = 'https:'+href  if href.startswith('//') else href
                    title = PLchar(title)
                    if co>1:
                    
                        titlex='[COLOR lightblue]%s[/COLOR] - [COLOR khaki]%s[/COLOR]'%(druz,resultat)
                        tit2 = '[COLOR orange]%s[/COLOR]'%title
                        plot = '%s[CR]%s'%(titlex,tit2)
                    else:
                        titlex='[COLOR lightblue]%s[/COLOR] - [COLOR khaki]%s[/COLOR]'%(druz,resultat)
                        tit2 = '[COLOR orange]%s[/COLOR]'%title
                        plot = '%s[CR]%s'%(titlex,tit2)
                    out.append({'title':titlex,'url':href,'plot':plot,'genre':tit2})
                    
            return out,outw
            
def getJSlinkNew2(url):
    html=getUrlReqok(url)
    out=[]    
    
    results=parseDOM(html,'div', attrs={'class': "row no-gutters"})#[0] #<div class="row no-gutters">
    co=0
    for result in results:
        if 'HIGHLIGHTS VIDEOS LINK' in result:
            vidz = parseDOM(result,'script')
            for vid in vidz:
                hreftit = re.findall('font color="#ffffff">([^>]+)</font><br/>.+?src="([^"]+)"',vid)
                co+=1
                for title,href in hreftit:
                    href  = 'https:'+href  if href.startswith('//') else href
                    if co>1:
                        #title='[COLOR orange]%s[/COLOR]'%title
                        code = '(Goal video)'
                    else:
                        #title='[COLOR lightgreen]%s[/COLOR]'%title
                        code = '(Highlight video)'
                    out.append({'title':title,'url':href,'genre':code})
                    
            return out
            
def getJSlink4(url):
    html=getUrlReq(url)
    out=[]    
    try:
        result=parseDOM(html,'div', attrs={'class': "card mb-4"})[0]
        alstr=re.findall("""var wwwjavafootycom.+?['"](.+?)['"]""",result)[0]                
        counter=0
        decoded=decodeAlan(alstr,False)
        decoded= urllib_parse.unquote(decoded)
        aespass=re.findall("decrypt\((.+?)\)",decoded)         
        if aespass:    
            decoded=(decryptaes(aespass[0])).replace("\'",'"')
        results=parseDOM(decoded,'span')
        for result in results:
            title=parseDOM(result,'font')[0]
            href=parseDOM(result,'iframe',ret='src')#[0]
            if href:
                href=href[0]
            else:
                href=parseDOM(result,'a',ret='href')[0]
            href = 'https:'+href if href.startswith('//') else href
            if 'tinyurl' in href:
                r=s.get(href,headers=headersok,allow_redirects=False)
                href= r.headers['Location']    
            out.append({'title':title,'href':href})
        return out
    except:
        return out

def getMatchat(link):
    content = getUrlReq(link)
    content=content.replace("\'",'"')
    m3u8=re.findall('hls\:"(.+?)"',content,re.DOTALL)[0]
    str='https:'+m3u8
    return str    
    
def getStreamlet(url):
    html= getUrlReq(url)
    if ' Blocked Video ' in html:
        return ''
    else:
        src = re.compile('hls:"(//.*?)"').findall(html)[0]    
        src = 'http:'+src if src.startswith('//') else src
        return src    
        
def getStreamja(url):
    html= getUrlReq(url)
    src = re.compile('source src="([^"]+)"').findall(html)[0]
    src = 'http:'+src if src.startswith('//') else src
    return src    

def getLinks2(url):
    out=getJSlinkNew2(url)
    return out
    
def getLinks(url,nazwa):
    out=getJSlinkNew(url,nazwa)
    return out
    
def PLchar(letter):
    if isinstance(letter, unicode):
        letter = letter.encode('utf-8') if six.PY2 else letter
    letter = letter.replace('&lt;br/&gt;',' ')
    letter = letter.replace('&nbsp;','')
   # s='JiNcZCs7'
   # letter = re.sub(s.decode('base64'),'',letter)
    letter = letter.replace('&quot;','"').replace('&amp;quot;','"')
    letter = letter.replace('&oacute;','ó').replace('&Oacute;','Ó')
    letter = letter.replace('&aacute;','a').replace('&Aacute;','A')
    letter = letter.replace('&amp;oacute;','ó').replace('&amp;Oacute;','Ó')
    letter = letter.replace('&amp;','&')
    letter = letter.replace('\u0105','ą').replace('\u0104','Ą')
    letter = letter.replace('\u0107','ć').replace('\u0106','Ć')
    letter = letter.replace('\u0119','ę').replace('\u0118','Ę')
    letter = letter.replace('\u0142','ł').replace('\u0141','Ł')
    letter = letter.replace('\u0144','ń').replace('\u0144','Ń')
    letter = letter.replace('\u00f3','ó').replace('\u00d3','Ó')
    letter = letter.replace('\u015b','ś').replace('\u015a','Ś')
    letter = letter.replace('\u017a','ź').replace('\u0179','Ź')
    letter = letter.replace('\u017c','ż').replace('\u017b','Ż')
    return letter
    