#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ykdl.extractor import VideoExtractor
from ykdl.videoinfo import VideoInfo
from ykdl.util.match import match1
from ykdl.util.html import get_content
from ykdl.util.jsengine import JSEngine

assert JSEngine, "No JS Interpreter found, can't parse douyu live/video!"

class OpenC(VideoExtractor):
    name = "网易公开课 (163 openCourse)"

    sopported_stream_types = [
        ['TD', 'Shd', '超清'], 
        ['HD',  'Hd', '高清'], 
        ['SD',  'Sd', '标清']
    ]
    name2lang = {
        '中文': 'zh',
        '英文': 'en'
    }

    def list_only(self):
        self.vid = match1(self.url, 'mid=(\w+)')
        return self.vid is None

    def prepare_data(self):
        html = get_content(self.url)
        js = match1(html, 'window\.__NUXT__=(.+);</script>')
        js_ctx = JSEngine()
        data = js_ctx.eval(js)
        self.logger.debug('video_data: \n%s', data)
        try:
            self.url = data['data'][0]['playUrl']
        except KeyError:
            self.data = data
        else:
            self.prepare_data()

    def prepare(self):
        info = VideoInfo(self.name)

        if self.data is None:
            self.prepare_data()
        data = self.data
        moiveList = data['state']['movie']['moiveList']
        if self.vid is None:
            movie = moiveList[0]
        else:
            for movie in moiveList:
                mid = movie['mid']
                if mid == self.vid:
                    break
            assert mid == self.vid, 'can not found mid %r' % mid

        title = data['data'][0]['title']
        mtitle = movie['title'].rpartition(title)[-1]
        if mtitle:
            for sp in ['：', '】']:
                t1, _, t2 = mtitle.partition(sp)
                if title.startswith(t1):
                    mtitle = t2
                    break
        if mtitle:
            p = movie['pNumber']
            pc = len(moiveList)
            if pc > 1 and not mtitle[0].isdecimal() and str(p) not in mtitle:
                pl = 0
                while pc:
                    pl += 1
                    pc //= 10
                mtitle = ('{:0>%dd} {}' % pl).format(p, mtitle)
            title = '{} - {}'.format(title, mtitle)
        school_info = data['data'][0]
        school = school_info['school']
        director = school_info['director']
        if director and director != 'null':
            if director != school :
                director = '[{}] {}'.format(school, director)
        else:
            director = school
        if school not in title:
            title = '[{}] {}'.format(school, title)
        info.title = title
        info.artist = director

        for stream, tp, profile in self.sopported_stream_types:
            for ext in ['mp4', 'm3u8']:
                for orig in ['', 'Orign']:
                    if stream in info.streams:
                        continue
                    url = movie['{}{}Url{}'.format(ext, tp, orig)]
                    if not url:
                        continue
                    size = movie['{}{}Size{}'.format(ext, tp, orig)]
                    info.stream_types.append(stream)
                    info.streams[stream] = {
                        'container': ext,
                        'video_profile': profile,
                        'src': [url],
                        'size': size
                    }

        nlang = 0
        for sub in movie['subList']:
            name = sub['subName']
            if not name:
                if nlang:
                    name = movie['subtitle']
                else:
                    name = '中文'
                nlang += 1
            lang = self.name2lang[name]
            info.subtitles.append({
                'lang': lang,
                'name': name,
                'format': 'srt',
                'src': sub['subUrl'],
                'size': sub['subSize']
            })

        return info

    def prepare_list(self):
        self.prepare_data()
        return [movie['mid'] for movie in self.data['state']['movie']['moiveList']]

site = OpenC()
