# Embedded file name: /usr/lib/enigma2/python/Plugins/Extensions/HighLive/unwise.py
import re

def unwise1(w):
    int1 = 0
    result = ''
    while int1 < len(w):
        result = result + chr(int(w[int1:int1 + 2], 36))
        int1 += 2

    return result


def unwise(w, i, s, e):
    int1 = 0
    int2 = 0
    int3 = 0
    int4 = 0
    string1 = ''
    string2 = ''
    while True:
        if w != '':
            if int1 < 5:
                string2 = string2 + w[int1:int1 + 1]
            elif int1 < len(w):
                string1 = string1 + w[int1:int1 + 1]
            int1 += 1
        if i != '':
            if int2 < 5:
                string2 = string2 + i[int2:int2 + 1]
            elif int2 < len(i):
                string1 = string1 + i[int2:int2 + 1]
            int2 += 1
        if s != '':
            if int3 < 5:
                string2 = string2 + s[int3:int3 + 1]
            elif int3 < len(s):
                string1 = string1 + s[int3:int3 + 1]
            int3 = int3 + 1
        if e != '':
            if int4 < 5:
                string2 = string2 + e[int4:int4 + 1]
            elif int4 < len(e):
                string1 = string1 + e[int4:int4 + 1]
            int4 = int4 + 1
        if len(w) + len(i) + len(s) + len(e) == len(string1) + len(string2):
            break

    int1 = 0
    int2 = 0
    result = ''
    while int1 < len(string1):
        flag = -1
        if ord(string2[int2:int2 + 1]) % 2:
            flag = 1
        result = result + chr(int(string1[int1:int1 + 2], 36) - flag)
        int2 += 1
        if int2 >= len(string2):
            int2 = 0
        int1 += 2

    return result


def unwise_process(result):
    while True:
        a = re.compile('eval\\s*\\(\\s*function\\s*\\(\\s*w\\s*,\\s*i\\s*,\\s*s\\s*,\\s*e\\s*\\).+?[\\"\\\']\\s*\\)\\s*\\)').search(result)
        if not a:
            break
        a = a.group()
        tmp = re.compile('\\}\\s*\\(\\s*[\\"\\\'](\\w*)[\\"\\\']\\s*,\\s*[\\"\\\'](\\w*)[\\"\\\']\\s*,\\s*[\\"\\\'](\\w*)[\\"\\\']\\s*,\\s*[\\"\\\'](\\w*)[\\"\\\']').search(a)
        if not tmp:
            print 'UNWISE ERROR --- ' + a
            result = result.replace(a, '')
        else:
            wise = ['',
             '',
             '',
             '']
            wise = tmp.groups()
            if a.find('while') == -1:
                result = result.replace(a, unwise1(wise[0]))
            else:
                c = 0
                wisetmp = ['',
                 '',
                 '',
                 '']
                b = re.compile('eval\\s*\\(\\s*function\\s*\\(\\s*w\\s*,\\s*i\\s*,\\s*s\\s*,\\s*e\\s*\\)(.*?)while').search(a).group(1)
                for d in re.compile('var\\s+\\w+\\s*=\\s*0').findall(b):
                    wisetmp[c] = wise[c]
                    c += 1

                result = result.replace(a, unwise(wisetmp[0], wisetmp[1], wisetmp[2], wisetmp[3]))

    return result


def resolve_var(HTML, key):
    key = re.escape(key)
    tmp1 = HTML.replace('\r', '')
    tmp1 = tmp1.replace('\n', ';')
    tmp2 = re.compile('[^\\w\\.]' + key + '\\s*=\\s*["\'](.*?)["\']').search(tmp1)
    if tmp2:
        tmp2 = tmp2.group(1)
    else:
        tmp2 = re.compile('[^\\w\\.]' + key + '\\s*=\\s*(.*?)[;,]').search(tmp1)
        if not tmp2:
            tmp2 = ''
        else:
            tmp2 = resolve_var(HTML, tmp2.group(1))
    return tmp2


test = ";eval(function(w,i,s,e){var lIll=0;var ll1I=0;var Il1l=0;var ll1l=[];var l1lI=[];while(true){if(lIll<5)l1lI.push(w.charAt(lIll));else if(lIll<w.length)ll1l.push(w.charAt(lIll));lIll++;if(ll1I<5)l1lI.push(i.charAt(ll1I));else if(ll1I<i.length)ll1l.push(i.charAt(ll1I));ll1I++;if(Il1l<5)l1lI.push(s.charAt(Il1l));else if(Il1l<s.length)ll1l.push(s.charAt(Il1l));Il1l++;if(w.length+i.length+s.length+e.length==ll1l.length+l1lI.length+e.length)break;}var lI1l=ll1l.join('');var I1lI=l1lI.join('');ll1I=0;var l1ll=[];for(lIll=0;lIll<ll1l.length;lIll+=2){var ll11=-1;if(I1lI.charCodeAt(ll1I)%2)ll11=1;l1ll.push(String.fromCharCode(parseInt(lI1l.substr(lIll,2),36)-ll11));ll1I++;if(ll1I>=l1lI.length)ll1I=0;}return l1ll.join('');}('[encoded data, 5781 characters]','[encoded data, 5781 characters]','[encoded data, 5781 characters]','bc1cc22d6e847830ade4904add3ddca9'));"
test = unwise_process(test)
print resolve_var(test, 'flashvars.filekey')