#!/usr/bin/env python

__all__ = ['zhanqi_download']

from ..common import *
import re

def zhanqi_download(url, output_dir = '.', merge = True, info_only = False, **kwargs):
    html = get_content(url)
    video_type_patt = r'VideoType":"([^"]+)"'
    video_type = match1(html, video_type_patt)

    #rtmp_base_patt = r'VideoUrl":"([^"]+)"'
    rtmp_id_patt = r'videoId":"([^"]+)"'
    vod_m3u8_id_patt = r'VideoID":"([^"]+)"'
    title_patt = r'<p class="title-name" title="[^"]+">([^<]+)</p>'
    title_patt_backup = r'<title>([^<]{1,9999})</title>'
    title = match1(html, title_patt) or match1(html, title_patt_backup)
    title = unescape_html(title)
    rtmp_base = "http://wshdl.load.cdn.zhanqi.tv/zqlive"
    vod_base = "http://dlvod.cdn.zhanqi.tv"
    rtmp_real_base = "rtmp://dlrtmp.cdn.zhanqi.tv/zqlive/"
    
    if video_type == "LIVE":
        rtmp_id = match1(html, rtmp_id_patt).replace('\\/','/')
        #request_url = rtmp_base+'/'+rtmp_id+'.flv?get_url=1'
        #real_url = get_html(request_url)
        real_url = rtmp_real_base + '/' + rtmp_id
        print_info(site_info, title, 'flv', float('inf'))
        if not info_only:
            download_rtmp_url(real_url, title, 'flv', {}, output_dir, merge = merge)
            #download_urls([real_url], title, 'flv', None, output_dir, merge = merge)
    elif video_type == "VOD":
        vod_m3u8_request = vod_base + match1(html, vod_m3u8_id_patt).replace('\\/','/')
        vod_m3u8 = get_html(vod_m3u8_request)
        part_url = re.findall(r'(/[^#]+)\.ts',vod_m3u8)
        real_url = []
        for i in part_url:
            i = vod_base + i + ".ts"
            real_url.append(i)
        type_ = ''
        size = 0
        for url in real_url:
            _, type_, temp = url_info(url)
            size += temp or 0

        print_info(site_info, title, type_ or 'ts', size)
        if not info_only:
            download_urls(real_url, title, type_ or 'ts', size, output_dir, merge = merge)
    else:
        NotImplementedError('Unknown_video_type')

site_info = "zhanqi.tv"
download = zhanqi_download
download_playlist = playlist_not_supported('zhanqi')

@@ -79,16 +79,69 @@
 mport re
+%0Aimport base64%0Aimport json%0Aimport time%0Aimport hashlib
 %0A%0Adef zh
@@ -864,16 +864,143 @@
 qlive/%22%0A
+    room_info = %22http://www.zhanqi.tv/api/static/live.roomid/%22%0A    KEY_MASK = %22#%7B&..?!(%22%0A    ak2_pattern = r'ak2%22:%22%5Cd-(%5B%5E%7C%5D+)'%0A
     %0A   
@@ -1189,24 +1189,619 @@
 equest_url)%0A
+        html2 = get_content(room_info + rtmp_id.split(%22_%22)%5B0%5D + %22.json%22)%0A        json_data = json.loads(html2)%0A        cdns = json_data%5B%22data%22%5D%5B%22flashvars%22%5D%5B%22cdns%22%5D%0A        cdns = base64.b64decode(cdns).decode(%22utf-8%22)%0A        cdn = match1(cdns, ak2_pattern)%0A        cdn = base64.b64decode(cdn).decode(%22utf-8%22)%0A        key = ''%0A        i = 0%0A        while(i %3C len(cdn)):%0A            key = key + chr(ord(cdn%5Bi%5D) %5E ord(KEY_MASK%5Bi %25 8%5D))%0A            i = i + 1%0A        time_hex = hex(int(time.time()))%5B2:%5D%0A        key = hashlib.md5(bytes(key + %22/zqlive/%22 + rtmp_id + time_hex, %22utf-8%22)).hexdigest()%0A
         real
@@ -1837,16 +1837,49 @@
  rtmp_id
+ + %22?k=%22 + key + %22&t=%22 + time_hex
 %0A       

fix zhanqitv