<?xml version="1.0" encoding="utf-8"?>
<parser delay="4500" crpj="true" speed="6M" date="2021.03.29">
    <url><![CDATA[https?://(?:www\.|\w\.)?(?:ixigua|toutiao)\.com/(?:item/(?<id>\d+)|(?<id>i?\d+)|cinema/album/(?<id>[^/]+)|group/(?<id>[^/]+)|pseries/(?<id>[^/]+)|user_playlist/(?<id>[^?]+)|(?<id>\w{7}))]]></url>
    <title><![CDATA[title="([^"]+)"]]></title>
    <thumbnail><![CDATA[thumbnail=([^,]+)]]></thumbnail>
    <duration><![CDATA[duration=([^,]+)]]></duration>
    <mediaUrl><![CDATA[cr_video_url=([^\$]+)]]></mediaUrl>
    <audioUrl><![CDATA[cr_audio_url=([^\$]+)]]></audioUrl>
    <ckSign><![CDATA[ttwid]]></ckSign>
    <jsCode><![CDATA[
        try {
            //若加referer，有些电脑上下载视频支为空html文件，不加
            //它丫的又改了。开始加载空页面，置4500，以使其真页面加载
            //https://v.ixigua.com/JauYbFb/
            //https://www.ixigua.com/6796539530896736782?logTag=NiIeQhfmMcwvG6CVYqgUI...此链接怪异，无水印源指向不对
            //https://www.ixigua.com/6877138417789075968?logTag=8q7DBzS-sSTCARV46KwH6
            //https://www.ixigua.com/6749846354375213581?logTag=9sd1sbdGtg5vGWHsu3uaB 旧水印
            //https://www.ixigua.com/6884820658107187727?logTag=1u9KsswO3DAxVBYU6kBpX 视频中固有水印
            //https://www.ixigua.com/6681057203291423246?logTag=J49wmJuki3BhKvo24rPEf 亦固有
            //https://www.ixigua.com/6793371865243976204 亦固有
            //https://www.ixigua.com/6493326417961419278?logTag=6Fp-BC1COFKRzPla8Ih0e 这种古老水印
            //https://www.ixigua.com/user_playlist/67211827964?rank=18&offset=120&order=new&filterTS=1615106429
            //https://www.ixigua.com/6516466656263275015?logTag=bd559f96d944407b5113 美拍水印
            var video_url = '', audio_url = '';
            if (typeof(fv) == 'undefined')
                fv = undefined;
            if (fv && (fv == '1.5.7.3' || fv == '1.5.5.3') && (!an || an.indexOf('CRTubeGet') != -1))
                video_url = '_';
            //crash branch...
            if (!video_url && window.location.href.indexOf('blank') != -1 && typeof(gvd) != 'undefined') {
                var data = gvd(window.external.url);
                var jo = JSON.parse(data);
                if (jo.body && jo.body.video_url) {
                    document.body.innerHTML = 'title=\"' + jo.body.title + '\",thumbnail=' + jo.body.cover_url + ',duration=' + 0 + ',cr_video_url=' + jo.body.video_url + '$';
                    return;
                }
            }

            //注：因其1080p dash文件为drm加密，因此不能硬解之，cr_audio_url为非剧集类dash而置
            var vq = window.external.vq || '1080p';
            var sd = window._SSR_HYDRATED_DATA;
            var pd = sd && sd.anyVideo && sd.anyVideo.gidInformation ? sd.anyVideo.gidInformation.packerData : undefined;
            //有此标记，则有dash源
            var dash = pd && pd.video && pd.video.video_logo;
            var url = window.external.url.match(/\/\d+/) ? window.external.url : window.location.href;
            if (url.indexOf('v.ixigua') != -1)
                url = window.location.href;
            var title = document.getElementsByTagName('h1').length != 0 ? document.getElementsByTagName('h1')[0].innerText : document.title;
            var thumbnail = document.head.innerHTML.indexOf('og:image') != -1 ? document.head.innerHTML.match(/og:image\"\s+content=\"([^\"]+)/)[1] : '';
            var duration = document.head.innerHTML.indexOf('op:video:duration') != -1 ? document.head.innerHTML.match(/op:video:duration\"\s+content=\"([^\"]+)/)[1] : 0;
            var gid = sd ? document.body.innerHTML.match(/group_id\":\"([^\"]+)/)[1] : (url.match(/com\/i?(\d+)/) ? url.match(/\/i?(\d+)/)[1] : undefined);
            var vid = sd ? document.body.innerHTML.match(/vid\":\"([^\"]+)/)[1] : undefined;
            var xd = (typeof(window.external.gcv) != 'undefined') ? window.external.gcv('Download', 'XGDash', 'true').toLowerCase() : 'true';
            var cookie = window.external.getCookie(url) || document.cookie;
            if (!vid || !thumbnail) {
                var headers = 'Cookie: ' + cookie + '\r\nReferer: ' + url;
                var html = window.external.getHtmlCodeEx(url, headers);
                vid = html.match(/vid\":\"([^\"]+)/) ? html.match(/vid\":\"([^\"]+)/)[1] : '';
                if (!gid && html.match(/group_id=/))
                    gid = html.match(/group_id=([^\"]+)/)[1];
                title = html.indexOf('og:title') != -1 ? html.match(/og:title\"\s+content=\"([^\"]+)/)[1].replace(' - 西瓜视频', '') : title;
                thumbnail = html.indexOf('og:image') != -1 ? html.match(/og:image\"\s+content=\"([^\"]+)/)[1] : '';
                duration = html.indexOf('op:video:duration') != -1 ? html.match(/op:video:duration\"\s+content=\"([^\"]+)/)[1] : '';
            }
            //url转换：https://www.ixigua.com/user_playlist/7008152666?rank=12&offset=90&order=new&filterTS=1616157018&logTag=KA3Wn5N63IDaTstoQMWhT
            if (url.indexOf('user_playlist') != -1 && gid && fv && fv[0] != '0')
                video_url = 'https://www.ixigua.com/' + gid + '#ra';

            if (!video_url && gid) {
                //https://www.ixigua.com/6620915739064795655/ 此有水印。因此，此支只判断k类的
                //https://www.ixigua.com/6785489916194193928/ 4k 2k...360p
                var api_url = 'http://is.snssdk.com/ugc/video/v1/aweme/detail/info/?group_id=' + gid + '&device_id=1662092118334637&aid=32&version_code=746&device_platform=android';
                var data = window.external.getHtmlCode(api_url, url);
                if (data && data.indexOf('\"data\":{') != -1) {
                    var jo = JSON.parse(data);
                    title = jo.data.title;
                    if (!thumbnail && jo.data.thumb_image_list.length != 0)
                        thumbnail = jo.data.thumb_image_list[0].url;
                    if (!duration)
                        duration = jo.data.video.duration;

                    function sbs(a, b) {
                        return a.size - b.size;
                    }
                    var pl = jo.data.video.play_addr_list;
                    pl.sort(sbs);
                    //有dash即可走此支
                    if (dash && xd == 'true' || fv && fv[0] == '0') {
                        for (var v in pl) {
                            var video = pl[v];
                            video_url = video.play_url_list[0];
                            if (video.definition == vq)
                                break;
                        }
                    }
                }
            }
            //dash source, no watermark...加上版本判断
            //极少数用户机子上，dash支路不通，而UrlExists()却又存在，此问题，建议置720p走上支路...XGDash
            if (!video_url && pd && dash && fv && fv[0] != '0' && xd == 'true') {
                try {
                    //video & episodeInfo & movie  https://www.ixigua.com/6904818605544866315，dash只有video_list
                    var dash_video = pd.video.videoResource.dash_120fps ? pd.video.videoResource.dash_120fps : pd.videoResource.dash;
                    var ja = dash_video && dash_video.dynamic_video ? dash_video.dynamic_video.dynamic_audio_list : undefined;
                    var jv = dash_video && dash_video.dynamic_video ? dash_video.dynamic_video.dynamic_video_list : dash_video.video_list;
                    audio_url = ja ? atob(ja[ja.length - 1].main_url) : audio_url;
                    //音视频须为一体
                    if (jv && ja) {
                        for (var v in jv) {
                            if (jv[v].definition == vq) {
                                video_url = atob(jv[v].main_url);
                                break;
                            }
                        }
                    }
                }
                catch (ex) {
                }
            }
            //https://www.ixigua.com/6813173441525449227?id=6821125198100562444&logTag=NSQbfGIjXOMCg1BaG7XUs其解析出错误
            if (!video_url && (vq == '1080p' || vq.indexOf('k') != -1) && typeof(gvd) != 'undefined' && (window.location.href.match(/\/(?:i)?\d{19,}/) || url.indexOf('user_playlist') != -1)) {
                if (url.indexOf('user_playlist') != -1 && gid)
                    url = 'https://www.ixigua.com/' + gid;
                var data = gvd(url);
                var jo = JSON.parse(data);
                //https://www.toutiao.com/item/6927629030690652675/
                if (data.indexOf('video_url') == -1)
                    data = data = gvd(window.location.href);
                if (jo.body && jo.body.video_url) {
                    title = jo.body.title;
                    if (!thumbnail)
                        thumbnail = jo.body.cover_url;
                    //https://www.ixigua.com/6768030523659911694?logTag=xJpJ8B4-ejC4z1Hz7xNNn此支，无水印源不可下
                    if (window.external.urlExists(jo.body.video_url))
                        video_url = jo.body.video_url;
                }
            }
            //max: 720p...此支，电脑皆可下，而1080等在极少数电脑上，地址不可访问。此支，有些视频亦有水印
            //https://www.ixigua.com/6769931165542384140?logTag=WSGY_0R8nn5YVTnsam-Ge 有水印
            //https://v.ixigua.com/JGxh5x6/->https://www.ixigua.com/group/6913490258722030088/?app=video_article&timestamp=1609929798
            if (!video_url && gid && vid && url.indexOf('id=') == -1) {
                var api_url = 'http://ib-lq.snssdk.com/video/app/article/information/v23/?group_id=' + gid + '&item_id=' + vid + '&aggr_type=2&context=1&hot_push=0&flags=64&from_category=pgc&article_page=1&play_param=codec_type%3A1%2Ccdn_type%3A1%2Cenable_dash%3A1&aid=32';
                var data = window.external.getHtmlCodeEx(api_url, 'Cookie: ' + cookie);
                if (data && data.indexOf('video_play_info') != -1 && data.indexOf('video_list') != -1) {
                    var jo = JSON.parse(data);
                    jo = JSON.parse(jo.data.video_play_info);
                    if (!duration)
                        duration = jo.video_duration;
                    if (!thumbnail)
                        thumbnail = jo.poster_url;
                    for (var v in jo.video_list) {
                        var video = jo.video_list[v];
                        video_url = atob(video.main_url);
                        if (video.definition == vq)
                            break;
                    }
                }
            }
            if (!video_url && pd) {
                //movies...pd.episodeInfo
                var vr = pd.videoResource || pd.video.videoResource;
                var jn = vr.normal.video_list;
                for (var v in jn) {
                    video_url = jn[v].main_url;
                    if (jn[v].definition == vq)
                        break;
                }
                video_url = (video_url.indexOf('ixigua') == -1) ? atob(video_url) : video_url;
            }
            if (pd) {
                title = pd.video ? pd.video.title : pd.episodeInfo ? pd.episodeInfo.name || pd.episodeInfo.title : title;
                if (!thumbnail && pd.episodeInfo && pd.episodeInfo.coverList)
                    thumbnail = pd.episodeInfo.coverList[0].url;
                if (!duration && pd.video)
                    duration = pd.video_duration
            }

            //document.body.innerHTML不行，它会被原页面替换
            document.head.innerHTML = '';
            document.body.innerHTML = 'title=\"' + title + '\",thumbnail=' + thumbnail + ',duration=' + duration + ',cr_video_url=' + video_url + '$cr_audio_url=' + audio_url + '$';
        }
        catch (ex) {
        }
    ]]></jsCode>
</parser>