# -*- 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, 'os.path.join', 'os.path.join', (['control.dataPath', '"""finecastcookie.lwp"""'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (20, 'liveresolver.modules.client.request', 'client.request', (['url'], {'referer': 'referer', 'cj': 'cj'}), False, 'from liveresolver.modules import client, recaptcha_v2, control, constants, decryptionUtils\n'), (25, 'liveresolver.modules.log_utils.log', 'log', (['result'], {}), False, 'from liveresolver.modules.log_utils import log\n'), (21, 're.findall', 're.findall', (['"""data-sitekey="([^"]+)"""', 'rs'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (27, 're.findall', 're.findall', (['"""[\'"](.+?.stream)[\'"]"""', 'result'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (28, 're.findall', 're.findall', (['"""[\'"](\\\\?wmsAuthSign.+?)[\'"]"""', 'result'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (41, 'cookielib.LWPCookieJar', 'cookielib.LWPCookieJar', ([], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (47, 'cookielib.LWPCookieJar', 'cookielib.LWPCookieJar', ([], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (22, 'liveresolver.modules.recaptcha_v2.UnCaptchaReCaptcha', 'recaptcha_v2.UnCaptchaReCaptcha', ([], {}), False, 'from liveresolver.modules import client, recaptcha_v2, control, constants, decryptionUtils\n'), (24, 'urllib.urlencode', 'urllib.urlencode', (['token'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (17, 'urlparse.urlparse', 'urlparse.urlparse', (['url'], {}), False, 'import re, urlparse, cookielib, os, urllib\n'), (12, 'urlparse.urlparse', 'urlparse.urlparse', (['url'], {}), False, 'import re, urlparse, cookielib, os, urllib\n')]