#!/usr/bin/env python

__all__ = ['letv_download']

import json
import random
import xml.etree.ElementTree as ET
from ..common import *

def get_timestamp():
    tn = random.random()
    url = 'http://api.letv.com/time?tn={}'.format(tn)
    result = get_content(url)
    return json.loads(result)['stime']

def get_key(t):
    for s in range(0, 8):
        e = 1 & t
        t >>= 1
        e <<= 31
        t += e
    return t ^ 185025305

def video_info(vid):
    tn = get_timestamp()
    key = get_key(tn)
    url = 'http://api.letv.com/mms/out/video/play?id={}&platid=1&splatid=101&format=1&tkey={}&domain=http%3A%2F%2Fwww.letv.com'.format(vid, key)
    r = get_content(url, decoded=False)
    xml_obj = ET.fromstring(r)
    info = json.loads(xml_obj.find("playurl").text)
    title = info.get('title')
    urls = info.get('dispatch')
    for k in urls.keys():
        url = urls[k][0]
        break
    url += '&termid=1&format=0&hwtype=un&ostype=Windows7&tag=letv&sign=letv&expect=1&pay=0&rateid={}'.format(k)
    return url, title

def letv_download_by_vid(vid, output_dir='.', merge=True, info_only=False):
    url, title = video_info(vid)
    _, _, size = url_info(url)
    ext = 'flv'
    print_info(site_info, title, ext, size)
    if not info_only:
        download_urls([url], title, ext, size, output_dir=output_dir, merge=merge)

def letv_download(url, output_dir='.', merge=True, info_only=False):
    if re.match(r'http://www.letv.com/ptv/vplay/(\d+).html', url):
        vid = match1(url, r'http://www.letv.com/ptv/vplay/(\d+).html')
    else:
        html = get_content(url)
        vid = match1(html, r'vid="(\d+)"')
    letv_download_by_vid(vid, output_dir=output_dir, merge=merge, info_only=info_only)


site_info = "letv.com"
download = letv_download
download_playlist = playlist_not_supported('letv')

@@ -434,16 +434,17 @@
 025305%0A%0A
+%0A
 def vide
@@ -502,36 +502,34 @@
 _key(tn)%0A    url
- = '
+=%22
 http://api.letv.
@@ -544,83 +544,102 @@
 out/
-video/play?id=%7B%7D&
+common/geturl?platid=3&s
 platid=
+30
 1&
-s
 pla
-tid=101&format=1&tkey=%7B%7D&domain=http%253A%252F%252F
+yid=0&vtype=9,13,21,28&version=2.0&tss=no&vid=%7B%7D&domain=
 www.
@@ -646,17 +646,25 @@
 letv.com
-'
+&tkey=%7B%7D%22
 .format(
@@ -667,17 +667,16 @@
 mat(vid,
- 
 key)%0A   
@@ -716,47 +716,16 @@
 se)%0A
-    xml_obj = ET.fromstring(r)%0A
     info
  = j
@@ -720,19 +720,17 @@
     info
- = 
+=
 json.loa
@@ -736,164 +736,223 @@
 ads(
-xml_obj.find(%22playurl%22).text)%0A    title = info.get('title')%0A    urls = info.get('dispatch')%0A    for k in urls.keys():%0A        url = urls%5Bk%5D%5B0%5D%0A        break
+str(r,%22utf-8%22))%0A    size=0%0A    for i in info%5B%22data%22%5D%5B0%5D%5B%22infos%22%5D: #0 means only one file not truncated.need to upgrade %0A        if int(i%5B%22gsize%22%5D)%3Esize:%0A            size=int(i%5B%22gsize%22%5D)%0A            url=i%5B%22mainUrl%22%5D%0A
 %0A   
@@ -1046,16 +1046,25 @@
 &rateid=
+1300'   #
 %7B%7D'.form
@@ -1083,23 +1083,16 @@
 turn url
-, title
 %0A%0Adef le
@@ -1106,32 +1106,38 @@
 load_by_vid(vid,
+title,
  output_dir='.',
@@ -1178,16 +1178,8 @@
  url
-, title 
 = vi
@@ -1454,24 +1454,70 @@
 nly=False):%0A
+    html = get_content(url)%0A    #to get title%0A
     if re.ma
@@ -1656,40 +1656,8 @@
 se:%0A
-        html = get_content(url)%0A
     
@@ -1695,16 +1695,73 @@
 %5Cd+)%22')%0A
+    title=match1(html,r'name=%22irTitle%22 content=%22(.*?)%22')%0A
     letv
@@ -1781,16 +1781,22 @@
 vid(vid,
+title,
  output_

fix letv by new api