# -*- coding: utf-8 -*-


import re,urlparse,cookielib,os,urllib
from liveresolver.modules import client,recaptcha_v2,control,constants, decryptionUtils
from liveresolver.modules.log_utils import log
cookieFile = os.path.join(control.dataPath, 'finecastcookie.lwp')

def resolve(url):
    #try:
        try:
            referer = urlparse.parse_qs(urlparse.urlparse(url).query)['referer'][0]
        except:
            referer=url


        id = urlparse.parse_qs(urlparse.urlparse(url).query)['u'][0]
        cj = get_cj()
        url = 'http://www.finecast.tv/embed4.php?u=%s&vw=640&vh=450'%id
        rs = client.request(url,referer=referer,cj=cj)
        sitekey = re.findall('data-sitekey="([^"]+)', rs)[0]
        token = recaptcha_v2.UnCaptchaReCaptcha().processCaptcha(sitekey, lang='de')
        #1:04
        result =client.request (url, post=urllib.urlencode(token),referer=referer)
        log(result)

        file = re.findall('[\'\"](.+?.stream)[\'\"]',result)[0]
        auth = re.findall('[\'\"](\?wmsAuthSign.+?)[\'\"]',result)[0]
        rtmp = 'http://play.finecast.tv:1935/live/%s/playlist.m3u8%s'%(file,auth)

        return rtmp

        
    #except:
    #    return


def get_cj():
    cookieJar=None
    try:
        cookieJar = cookielib.LWPCookieJar()
        cookieJar.load(cookieFile,ignore_discard=True)
    except: 
        cookieJar=None

    if not cookieJar:
        cookieJar = cookielib.LWPCookieJar()
    return cookieJar
[((7, 13, 7, 65), 'os.path.join', 'os.path.join', ({(7, 26, 7, 42): 'control.dataPath', (7, 44, 7, 64): '"""finecastcookie.lwp"""'}, {}), "(control.dataPath, 'finecastcookie.lwp')", False, 'import re, urlparse, cookielib, os, urllib\n'), ((20, 13, 20, 54), 'liveresolver.modules.client.request', 'client.request', (), '', False, 'from liveresolver.modules import client, recaptcha_v2, control, constants, decryptionUtils\n'), ((25, 8, 25, 19), 'liveresolver.modules.log_utils.log', 'log', ({(25, 12, 25, 18): 'result'}, {}), '(result)', False, 'from liveresolver.modules.log_utils import log\n'), ((21, 18, 21, 57), 're.findall', 're.findall', ({(21, 29, 21, 52): '"""data-sitekey="([^"]+)"""', (21, 54, 21, 56): 'rs'}, {}), '(\'data-sitekey="([^"]+)\', rs)', False, 'import re, urlparse, cookielib, os, urllib\n'), ((27, 15, 27, 60), 're.findall', 're.findall', ({(27, 26, 27, 52): '"""[\'"](.+?.stream)[\'"]"""', (27, 53, 27, 59): 'result'}, {}), '(\'[\\\'"](.+?.stream)[\\\'"]\', result)', False, 'import re, urlparse, cookielib, os, urllib\n'), ((28, 15, 28, 66), 're.findall', 're.findall', ({(28, 26, 28, 58): '"""[\'"](\\\\?wmsAuthSign.+?)[\'"]"""', (28, 59, 28, 65): 'result'}, {}), '(\'[\\\'"](\\\\?wmsAuthSign.+?)[\\\'"]\', result)', False, 'import re, urlparse, cookielib, os, urllib\n'), ((41, 20, 41, 44), 'cookielib.LWPCookieJar', 'cookielib.LWPCookieJar', ({}, {}), '()', False, 'import re, urlparse, cookielib, os, urllib\n'), ((47, 20, 47, 44), 'cookielib.LWPCookieJar', 'cookielib.LWPCookieJar', ({}, {}), '()', False, 'import re, urlparse, cookielib, os, urllib\n'), ((22, 16, 22, 49), 'liveresolver.modules.recaptcha_v2.UnCaptchaReCaptcha', 'recaptcha_v2.UnCaptchaReCaptcha', ({}, {}), '()', False, 'from liveresolver.modules import client, recaptcha_v2, control, constants, decryptionUtils\n'), ((24, 42, 24, 65), 'urllib.urlencode', 'urllib.urlencode', ({(24, 59, 24, 64): 'token'}, {}), '(token)', False, 'import re, urlparse, cookielib, os, urllib\n'), ((17, 31, 17, 53), 'urlparse.urlparse', 'urlparse.urlparse', ({(17, 49, 17, 52): 'url'}, {}), '(url)', False, 'import re, urlparse, cookielib, os, urllib\n'), ((12, 40, 12, 62), 'urlparse.urlparse', 'urlparse.urlparse', ({(12, 58, 12, 61): 'url'}, {}), '(url)', False, 'import re, urlparse, cookielib, os, urllib\n')]