<?xml version="1.0" encoding="utf-8"?>
<parser delay="500" crpj="true" referer="true" date="2021.03.19">
    <url><![CDATA[https?://(?:www\.)?icourse163\.org/learn/.*?tid=.*?id=(?<id>[^&]+)]]></url>
    <title><![CDATA[title="([^"]+)"]]></title>
    <thumbnail><![CDATA[thumbnail=([^,]+)]]></thumbnail>
    <duration><![CDATA[duration=([^,]+)]]></duration>
    <mediaUrl><![CDATA[cr_video_url=([^\$]+)]]></mediaUrl>
    <ckSign><![CDATA[NTESSTUDYSI]]></ckSign>
    <jsCode><![CDATA[
        try {
            //https://www.icourse163.org/learn/NJU-1002190001?tid=1002759016#/learn/content?type=detail&id=1003790221
            var url = window.external.url;
            var title = document.title;
            var thumbnail = '';
            var duration = 0;
            var video_url = '';
            var cookie = window.external.getCookie(url) || document.cookie;
            if (cookie.indexOf('') == -1) {
                title = '请在浏览器中登录您的MOOC账号然后再试';
                video_url = window.external.url;
            }
            else {
                var hs_id = cookie.match(/NTESSTUDYSI=([^;]+)/)[1];
                var biz_id = (url.indexOf('cid=') != -1) ? url.match(/cid=([^&]+)/)[1] : '';
                //get biz_id
                if (!biz_id) {
                    var tid = url.match(/tid=([^#&]+)/)[1];
                    var api_url = 'https://www.icourse163.org/dwr/call/plaincall/CourseBean.getLastLearnedMocTermDto.dwr';
                    var args = 'callCount=1\r\nscriptSessionId=${scriptSessionId}190\r\nhttpSessionId=' + hs_id + '\r\nc0-scriptName=CourseBean\r\nc0-methodName=getLastLearnedMocTermDto\r\nc0-id=0\r\nc0-param0=number:' + tid + '\r\nbatchId=' + Date.now();
                    var headers = 'User-Agent: ' + navigator.userAgent + '\r\nReferer: ' + url + '\r\nCookie: ' + cookie;
                    var data = window.external.getPostResultEx(api_url, args, headers);
                    if (data.indexOf('lastLearnUnitId') != -1)
                        biz_id = data.match(/lastLearnUnitId:(\d+)/)[1];
                }
                if (biz_id) {
                    api_url = 'https://www.icourse163.org/web/j/resourceRpcBean.getResourceToken.rpc?csrfKey=' + hs_id;
                    args = 'bizId=' + biz_id + '&bizType=1&contentType=1';
                    headers = 'Content-Type: application/x-www-form-urlencoded\r\nCookie: ' + cookie;
                    data = window.external.getPostResultEx(api_url, args, headers);
                    var jo = JSON.parse(data);
                    if (jo.result && jo.result.videoSignDto) {
                        var vi = jo.result.videoSignDto;
                        api_url = 'https://vod.study.163.com/eds/api/v1/vod/video?videoId=' + vi.videoId + '&signature=' + vi.signature + '&clientType=1';
                        data = window.external.getHtmlCode(api_url, url);
                        jo = JSON.parse(data);
                        if (jo.result && jo.result.videos && jo.result.videos.length != 0) {
                            vi = jo.result;
                            title = vi.name.replace('.mp4', '').replace('.flv', '');
                            thumbnail = vi.videoImgUrl || '';
                            duration = vi.duration;
                            var videos = jo.result.videos;

                            //sort...
                            function sbs(a, b) {
                                //mp4 first
                                var sa = (a.format == 'mp4') ? a.size * 1.2 : a.size;
                                var sb = (b.format == 'mp4') ? b.size * 1.2 : b.size;
                                return sa - sb;
                            }
                            videos.sort(sbs);
                            video_url = videos[videos.length - 1].videoUrl;
                        }
                    }
                }
            }
            if (!video_url)
                video_url = window.external.url;

            document.body.innerHTML = 'title=\"' + title + '\",thumbnail=' + thumbnail + ',duration=' + duration + ',cr_video_url=' + video_url + '$';
        }
        catch (ex) {
        }
    ]]></jsCode>
</parser>