/*
@header({
  searchable: 1,
  filterable: 1,
  quickSearch: 1,
  title: '聚合短剧',
  lang: 'cat'
})
*/

import 'assets://js/lib/crypto-js.js';
const UA = 'Mozilla/5.0 (Linux; Android 9; V2196A Build/PQ3A.190705.08211809; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36;webank/h5face;webank/1.0;netType:NETWORK_WIFI;appVersion:416;packageName:com.jp3.xg3';
const aggConfig = {
  keys: 'd3dGiJc651gSQ8w1',
  charMap: {
    '+': 'P', '/': 'X', '0': 'M', '1': 'U', '2': 'l', '3': 'E', '4': 'r', '5': 'Y', '6': 'W', '7': 'b', '8': 'd', '9': 'J',
    'A': '9', 'B': 's', 'C': 'a', 'D': 'I', 'E': '0', 'F': 'o', 'G': 'y', 'H': '_', 'I': 'H', 'J': 'G', 'K': 'i', 'L': 't',
    'M': 'g', 'N': 'N', 'O': 'A', 'P': '8', 'Q': 'F', 'R': 'k', 'S': '3', 'T': 'h', 'U': 'f', 'V': 'R', 'W': 'q', 'X': 'C',
    'Y': '4', 'Z': 'p', 'a': 'm', 'b': 'B', 'c': 'O', 'd': 'u', 'e': 'c', 'f': '6', 'g': 'K', 'h': 'x', 'i': '5', 'j': 'T',
    'k': '-', 'l': '2', 'm': 'z', 'n': 'S', 'o': 'Z', 'p': '1', 'q': 'V', 'r': 'v', 's': 'j', 't': 'Q', 'u': '7', 'v': 'D',
    'w': 'w', 'x': 'n', 'y': 'L', 'z': 'e'
  },
  headers: {
    default: { 'User-Agent': 'okhttp/3.12.11', 'content-type': 'application/json; charset=utf-8' }
  },
  platform: {
    百度: { host: 'https://api.jkyai.top', url1: '/API/bddjss.php?name=fyclass&page=fypage', url2: '/API/bddjss.php?id=fyid', search: '/API/bddjss.php?name=**&page=fypage' },
    锦鲤: { host: 'https://api.jinlidj.com', search: '/api/search', url2: '/api/detail' },
    番茄: { host: 'https://reading.snssdk.com', url1: '/reading/bookapi/bookmall/cell/change/v', url2: 'https://fqgo.52dns.cc/catalog', search: 'https://fqgo.52dns.cc/search' },
    星芽: { host: 'https://app.whjzjx.cn', url1: '/cloud/v2/theater/home_page?theater_class_id', url2: '/v2/theater_parent/detail', search: '/v3/search', loginUrl: 'https://u.shytkjgs.com/user/v1/account/login' },
    西饭: { host: 'https://xifan-api-cn.youlishipin.com', url1: '/xifan/drama/portalPage', url2: '/xifan/drama/getDuanjuInfo', search: '/xifan/search/getSearchList' },
    软鸭: { host: 'https://api.xingzhige.com', url1: '/API/playlet', search: '/API/playlet' },
    七猫: { host: 'https://api-store.qmplaylet.com', url1: '/api/v1/playlet/index', url2: 'https://api-read.qmplaylet.com/player/api/v1/playlet/info', search: '/api/v1/playlet/search' },
    围观: { host: 'https://api.drama.9ddm.com', url1: '/drama/home/shortVideoTags', url2: '/drama/home/shortVideoDetail', search: '/drama/home/search' },
    碎片: { host: 'https://free-api.bighotwind.cc', url1: '/papaya/papaya-api/theater/tags', url2: '/papaya/papaya-api/videos/info', search: '/papaya/papaya-api/videos/page' },
    甜圈: { host: 'https://mov.cenguigui.cn', url1: '/duanju/api.php?classname', url2: '/duanju/api.php?book_id', search: '/duanju/api.php?name' }
  },
  platformList: [
    { name: '甜圈短剧', id: '甜圈' }, { name: '七猫短剧', id: '七猫' }, { name: '锦鲤短剧', id: '锦鲤' },
    { name: '番茄短剧', id: '番茄' }, { name: '星芽短剧', id: '星芽' }, { name: '西饭短剧', id: '西饭' },
    { name: '软鸭短剧', id: '软鸭' }, /*{ name: '百度短剧', id: '百度' }, */{ name: '围观短剧', id: '围观' }
   // { name: '碎片剧场', id: '碎片' }
  ],
  search: { limit: 30, timeout: 6000 }
};

const ruleFilterDef = {
  百度: { area: '逆袭' },
  锦鲤: { area: '' },
  番茄: { area: 'videoseries_hot' },
  星芽: { area: '1' },
  西饭: { area: '' },
  软鸭: { area: '战神' },
  七猫: { area: '0' },
  围观: { area: '' },
  碎片: { area: '' },
  甜圈: { area: '推荐榜' }
};

const filterOptionsB64 = " ";

let xingya_headers = {};
function base64Encode(text) {
  return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
}
function base64Decode(text) {
  return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
}
async function request(url, options = {}) {
  const { method = 'GET', headers = {}, body, timeout = 5000 } = options;
  const isPost = method.toUpperCase() === 'POST';
  let requestBody = body && typeof body === 'object' ? JSON.stringify(body) : body;
  if (typeof req !== 'undefined') {
    return (await req(url, {
      method,
      headers: { ...aggConfig.headers.default, ...headers },
      body: isPost ? requestBody : null,
      timeout
    })).content;
  }
}
async function md5(str) {
  return CryptoJS.MD5(str).toString(CryptoJS.enc.Hex).toLowerCase();
}
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    const r = Math.random() * 16 | 0;
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
}
function getFilterOptions() {
  try {
    return JSON.parse(base64Decode(filterOptionsB64));
  } catch (e) {
    console.error('filterOptions 解码失败', e);
    return {};
  }
}
async function getQmParamsAndSign() {
  const sessionId = Date.now().toString();
  const data = {
    "static_score": "0.8",
    "uuid": "00000000-7fc7-08dc-0000-000000000000",
    "device-id": "20250220125449b9b8cac84c2dd3d035c9052a2572f7dd0122edde3cc42a70",
    "sourceuid": "aa7de295aad621a6",
    "refresh-type": "0",
    "model": "22021211RC",
    "client-id": "aa7de295aad621a6",
    "brand": "Redmi",
    "sys-ver": "12",
    "phone-level": "H",
    "wlb-uid": "aa7de295aad621a6",
    "session-id": sessionId
  };
  const jsonStr = JSON.stringify(data);
  const base64Str = base64Encode(unescape(encodeURIComponent(jsonStr)));
  let qmParams = '';
  for (const c of base64Str) {
    qmParams += aggConfig.charMap[c] || c;
  }
  const paramsStr = `AUTHORIZATION=app-version=10001application-id=com.duoduo.readchannel=unknownis-white=net-env=5platform=androidqm-params=${qmParams}reg=${aggConfig.keys}`;
  const sign = await md5(paramsStr);
  return { qmParams, sign };
}
async function getHeaderX() {
  const { qmParams, sign } = await getQmParamsAndSign();
  return {
    'net-env': '5',
    'reg': '',
    'channel': 'unknown',
    'is-white': '',
    'platform': 'android',
    'application-id': 'com.duoduo.read',
    'authorization': '',
    'app-version': '10001',
    'user-agent': 'webviewversion/0',
    'qm-params': qmParams,
    'sign': sign
  };
}
async function init(cfg) {
  try {
    const res = JSON.parse(await request(aggConfig.platform.星芽.loginUrl, {
      method: 'POST',
      headers: { 'User-Agent': 'okhttp/4.10.0', platform: '1', 'Content-Type': 'application/json' },
      body: JSON.stringify({ device: '24250683a3bdb3f118dff25ba4b1cba1a' })
    }));
    const token = res?.data?.token || res?.token;
    xingya_headers = token ? { ...aggConfig.headers.default, authorization: token } : aggConfig.headers.default;
  } catch (e) {
    xingya_headers = aggConfig.headers.default;
  }
  return true;
}
async function getXingyaHeaders() {
  return xingya_headers.authorization ? xingya_headers : aggConfig.headers.default;
}
async function home(filter) {
  const classes = aggConfig.platformList.map(p => ({ type_name: p.name, type_id: p.id }));
  const filters = {};
  const filterOptions = getFilterOptions();
  aggConfig.platformList.forEach(item => {
    const platformId = item.id;
    if (filterOptions[platformId]) {
      filters[platformId] = filterOptions[platformId];
    }
  });
  return JSON.stringify({ class: classes, filters });
}
async function homeVod() { return await recommend(); }
async function recommend() {
  const randomPlat = aggConfig.platformList[Math.floor(Math.random() * aggConfig.platformList.length)];
  const platId = randomPlat.id;
  const area = ruleFilterDef[platId]?.area || '';
  const videos = await category(platId, 1, false, { area });
  const list = JSON.parse(videos).list.slice(0, 10).map(v => ({ ...v, vod_content: `${randomPlat.name} | ${v.vod_remarks}` }));
  return JSON.stringify({ list });
}
async function category(tid, pg, filter, extend) {
  const plat = aggConfig.platform[tid];
  const area = (extend?.area !== undefined ? extend.area : ruleFilterDef[tid]?.area) || '';
  const page = parseInt(pg) || 1;
  const videos = [];
  if (!plat) return JSON.stringify({ list: videos, page, pagecount: 1, limit: 0, total: 0 });
  try {
    if (tid === '七猫') {
      const sign = await md5(`operation=1playlet_privacy=1tag_id=${area}${aggConfig.keys}`);
      const url = `${plat.host}${plat.url1}?tag_id=${area}&playlet_privacy=1&operation=1&sign=${sign}`;
      const res = JSON.parse(await request(url, { headers: { ...await getHeaderX(), ...aggConfig.headers.default } }));
      (res?.data?.list || []).forEach(i => videos.push({
        vod_id: `七猫@${encodeURIComponent(i.playlet_id)}`,
        vod_name: i.title,
        vod_pic: i.image_link,
        vod_remarks: `${i.total_episode_num}集`
      }));
    } else if (tid === '百度') {
      const url = `${plat.host}${plat.url1.replace('fyclass', area).replace('fypage', page)}`;
      const res = JSON.parse(await request(url));
      if (res?.data) videos.push(...res.data.map(i => ({
        vod_id: `百度@${i.id}`, vod_name: i.title, vod_pic: i.cover, vod_remarks: `更新至${i.totalChapterNum}集`
      })));
    } else if (tid === '锦鲤') {
      const res = JSON.parse(await request(plat.host + plat.search, { method: 'POST', body: JSON.stringify({ page, limit: 24, type_id: area, keyword: '' }) }));
      if (res?.data?.list) videos.push(...res.data.list.map(i => ({
        vod_id: `锦鲤@${i.vod_id}`, vod_name: i.vod_name, vod_pic: i.vod_pic, vod_remarks: `${i.vod_total}集`
      })));
    } else if (tid === '番茄') {
      const sessionId = new Date().toISOString().slice(0, 16).replace(/-|T:/g, '');
      let url = `${plat.host}${plat.url1}?change_type=0&selected_items=${area}&tab_type=8&cell_id=6952850996422770718&version_tag=video_feed_refactor&device_id=1423244030195267&aid=1967&app_name=novelapp&ssmix=a&session_id=${sessionId}`;
      if (page > 1) url += `&offset=${(page - 1) * 12}`;
      const res = JSON.parse(await request(url));
      const items = res?.data?.cell_view?.cell_data || (Array.isArray(res?.data) ? res.data : []);
      items.forEach(i => {
        const v = i.video_data?.[0] || i;
        videos.push({ vod_id: `番茄@${v.series_id || v.book_id || ''}`, vod_name: v.title, vod_pic: v.cover || v.horiz_cover, vod_remarks: v.sub_title || '' });
      });
    } else if (tid === '星芽') {
      const headers = await getXingyaHeaders();
      const url = `${plat.host}${plat.url1}=${area}&type=1&class2_ids=0&page_num=${page}&page_size=24`;
      const res = JSON.parse(await request(url, { headers }));
      if (res?.data?.list) videos.push(...res.data.list.map(i => ({
        vod_id: `星芽@${plat.host}${plat.url2}?theater_parent_id=${i.theater.id}`,
        vod_name: i.theater.title, vod_pic: i.theater.cover_url, vod_remarks: `${i.theater.total}集`
      })));
    } else if (tid === '西饭') {
      const [typeId, typeName] = area.split('@');
      const ts = Math.floor(Date.now() / 1000);
      const url = `${plat.host}${plat.url1}?reqType=aggregationPage&offset=${(page - 1) * 30}&categoryId=${typeId}&quickEngineVersion=-1&scene=&categoryNames=${encodeURIComponent(typeName || '')}&categoryVersion=1&density=1.5&pageID=page_theater&version=2001001&androidVersionCode=28&requestId=${ts}aa498144140ef297&appId=drama&teenMode=false&userBaseMode=false&session=eyJpbmZvIjp7InVpZCI6IiIsInJ0IjoiMTc0MDY1ODI5NCIsInVuIjoiT1BHXzFlZGQ5OTZhNjQ3ZTQ1MjU4Nzc1MTE2YzFkNzViN2QwIiwiZnQiOiIxNzQwNjU4Mjk0In19&feedssession=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dHlwIjowLCJidWlkIjoxNjMzOTY4MTI2MTQ4NjQxNTM2LCJhdWQiOiJkcmFtYSIsInZlciI6MiwicmF0IjoxNzQwNjU4Mjk0LCJ1bm0iOiJPUEdfMWVkZDk5NmE2NDdlNDUyNTg3NzUxMTZjMWQ3NWI3ZDAiLCJpZCI6IjNiMzViZmYzYWE0OTgxNDQxNDBlZjI5N2JkMDY5NGNhIiwiZXhwIjoxNzQxMjYzMDk0LCJkYyI6Imd6cXkifQ.JS3QY6ER0P2cQSxAE_OGKSMIWNAMsYUZ3mJTnEpf-Rc`;
      const res = JSON.parse(await request(url, { headers: aggConfig.headers.default }));
      const elements = res?.result?.elements || [];
      elements.forEach(s => (s.contents || []).forEach(v => {
        const d = v.duanjuVo;
        if (d) {
          videos.push({ vod_id: `西饭@${d.duanjuId}#${d.source}`, vod_name: d.title, vod_pic: d.coverImageUrl, vod_remarks: `${d.total}集` });
        }
      }));
    } else if (tid === '软鸭') {
      const url = `${plat.host}${plat.url1}/?keyword=${encodeURIComponent(area)}&page=${page}`;
      const res = JSON.parse(await request(url));
      if (res?.data) videos.push(...res.data.map(i => {
        const purl = `${i.title}@${i.cover}@${i.author}@${i.type}@${i.desc}@${i.book_id}`;
        return { vod_id: `软鸭@${encodeURIComponent(purl)}`, vod_name: i.title, vod_pic: i.cover, vod_remarks: i.type };
      }));
    } else if (tid === '围观') {
      const res = JSON.parse(await request(`${plat.host}${plat.search}`, {
        method: 'POST', body: JSON.stringify({ audience: "全部受众", page, pageSize: 30, searchWord: "", subject: "全部主题" })
      }));
      if (res?.data) videos.push(...res.data.map(i => ({
        vod_id: `围观@${i.oneId}`, vod_name: i.title, vod_pic: i.vertPoster, vod_remarks: `集数:${i.episodeCount} 播放:${i.viewCount}`
      })));
    } else if (tid === '碎片') {
      let openId = (await md5(guid())).substring(0, 16);
      let body = JSON.stringify({ openId });
      let key = CryptoJS.enc.Utf8.parse("p0sfjw@k&qmewu#w");
      const encrypted = CryptoJS.AES.encrypt(body, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).ciphertext.toString(CryptoJS.enc.Hex);
      const tokenRes = JSON.parse(await request("https://free-api.bighotwind.cc/papaya/papaya-api/oauth2/uuid", {
        method: 'POST', headers: { 'Content-Type': 'application/json', 'key': encrypted }, body
      }));
      if (tokenRes?.data?.token) {
        const headers = { ...aggConfig.headers.default, 'Authorization': tokenRes.data.token };
        const url = `${plat.host}${plat.search}?type=5&tagId=&pageNum=${page}&pageSize=24`;
        const res = JSON.parse(await request(url, { headers }));
        if (res?.list) videos.push(...res.list.map(i => ({
          vod_id: `碎片@${i.itemId}@${i.videoCode}`,
          vod_name: i.title,
          vod_pic: `https://speed.hiknz.com/papaya/papaya-file/files/download/${i.imageKey}/${i.imageName}`,
          vod_remarks: `集数:${i.episodesMax} 播放:${i.hitShowNum}`
        })));
      }
    } else if (tid === '甜圈') {
      const url = `${plat.host}${plat.url1}=${encodeURIComponent(area)}&offset=${page}`;
      const res = JSON.parse(await request(url));
      if (res?.data) {
        videos.push(...res.data.map(i => ({
          vod_id: `甜圈@${i.book_id}`,
          vod_name: i.title,
          vod_pic: i.cover,
          vod_remarks: `${i.sub_title || ''}`
        })));
      }
    }
  } catch (e) { }
  return JSON.stringify({ list: videos, page, pagecount: page + 1, limit: videos.length, total: videos.length * (page + 1) });
}
async function detail(id) {
  const [platId, ...rest] = id.split('@');
  const did = rest.join('@');
  const plat = aggConfig.platform[platId];
  if (!plat) return JSON.stringify({ list: [{ vod_id: id, vod_name: '平台不支持', vod_play_url: '' }] });
  let vod = { vod_id: id, vod_name: '未知', vod_pic: '', vod_remarks: '', vod_content: '', vod_play_from: '', vod_play_url: '' };
  try {
    if (platId === '七猫') {
      const didDecoded = decodeURIComponent(did);
      const sign = await md5(`playlet_id=${didDecoded}${aggConfig.keys}`);
      const url = `${plat.url2}?playlet_id=${didDecoded}&sign=${sign}`;
      const res = JSON.parse(await request(url, { headers: { ...await getHeaderX(), ...aggConfig.headers.default } }));
      if (res?.data) {
        const d = res.data;
        vod = {
          ...vod,
          vod_name: d.title,
          vod_pic: d.image_link,
          vod_remarks: `${d.total_episode_num}集`,
          vod_content: d.intro,
          vod_play_from: '七猫短剧',
          vod_play_url: (d.play_list || []).map(i => `${i.sort}$${i.video_url}`).join('#')
        };
      }
    } else if (platId === '百度') {
      const res = JSON.parse(await request(`${plat.host}${plat.url2.replace('fyid', did)}`));
      if (res) vod = { ...vod, vod_name: res.title, vod_pic: res.data?.[0]?.cover, vod_remarks: `更新至:${res.total || 0}集`, vod_play_from: '百度短剧', vod_play_url: (res.data || []).map(i => `${i.title}$${i.video_id}`).join('#') };
    } else if (platId === '锦鲤') {
      const res = JSON.parse(await request(`${plat.host}${plat.url2}/${did}`));
      if (res?.data) {
        const list = res.data;
        const urls = list.player ? Object.keys(list.player).map(k => `${k}$${list.player[k]}`) : [];
        vod = { ...vod, vod_name: list.vod_name, vod_pic: list.vod_pic, vod_remarks: list.vod_remarks, vod_play_from: '锦鲤短剧', vod_play_url: urls.join('#') };
      }
    } else if (platId === '番茄') {
      const res = JSON.parse(await request(`${plat.url2}?book_id=${did}`));
      if (res?.data) {
        const b = res.data.book_info;
        const u = (res.data.item_data_list || []).map(i => `${i.title}$${i.item_id}`).join('#');
        vod = { ...vod, vod_name: b.book_name, vod_pic: b.thumb_url, vod_remarks: `更新至${res.data.item_data_list?.length}集`, vod_play_from: '番茄短剧', vod_play_url: u };
      }
    } else if (platId === '星芽') {
      const headers = await getXingyaHeaders();
      const res = JSON.parse(await request(did, { headers }));
      if (res?.data) {
        const d = res.data;
        const u = (d.theaters || []).map(i => `${i.num}$${i.son_video_url}`).join('#');
        vod = { ...vod, vod_name: d.title, vod_pic: d.cover_url, vod_remarks: d.desc_tags + '', vod_play_from: '星芽短剧', vod_play_url: u };
      }
    } else if (platId === '西饭') {
      const [duanjuId, source] = did.split('#');
      const url = `${plat.host}${plat.url2}?duanjuId=${duanjuId}&source=${source}`;
      const res = JSON.parse(await request(url));
      if (res?.result) {
        const d = res.result;
        const u = (d.episodeList || []).map(e => `${e.index}$${e.playUrl}`).join('#');
        vod = { ...vod, vod_name: d.title, vod_pic: d.coverImageUrl, vod_remarks: d.updateStatus === 'over' ? `${d.total}集 已完结` : `更新${d.total}集`, vod_play_from: '西饭短剧', vod_play_url: u };
      }
    } else if (platId === '甜圈') {
      const url = `${plat.host}${plat.url2}=${did}`;
      const res = JSON.parse(await request(url));
      if (res) {
        const u = (res.data || []).map(item => `${item.title}$${item.video_id}`).join('#');
        vod = { ...vod, vod_name: res.book_name, vod_pic: res.book_pic, vod_remarks: `${res.duration || ''}`, vod_content: res.desc || '', vod_play_from: '甜圈短剧', vod_play_url: u };
      }
    } else if (platId === '软鸭') {
      const didDecoded = decodeURIComponent(did);
      const [title, img, author, type, desc, book_id] = didDecoded.split('@');
      const detailUrl = `${plat.host}${plat.url1}/?book_id=${book_id || did.split('@')[5]}`;
      const res = JSON.parse(await request(detailUrl, { headers: aggConfig.headers.default }));
      const u = (res.data?.video_list || []).map(e => `${e.title}$${e.video_id}`).join('#');
      vod = { ...vod, vod_name: title, vod_pic: img, vod_remarks: type, vod_content: desc, vod_play_from: '软鸭短剧', vod_play_url: u };
    } else if (platId === '围观') {
      const res = JSON.parse(await request(`${plat.host}${plat.url2}?oneId=${did}&page=1&pageSize=1000`));
      if (res?.data?.length) {
        const d = res.data;
        vod = { ...vod, vod_name: d[0].title, vod_pic: d[0].vertPoster, vod_remarks: `共${d.length}集`, vod_play_from: '围观短剧', vod_play_url: d.map(e => `${e.title}第${e.playOrder}集$${e.playSetting}`).join('#') };
      }
    } else if (platId === '碎片') {
      const [itemId, videoCode] = did.split('@');
      let openId = (await md5(guid())).substring(0, 16);
      let body = JSON.stringify({ openId });
      let key = CryptoJS.enc.Utf8.parse("p0sfjw@k&qmewu#w");
      const encrypted = CryptoJS.AES.encrypt(body, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).ciphertext.toString(CryptoJS.enc.Hex);
      const tokenRes = JSON.parse(await request("https://free-api.bighotwind.cc/papaya/papaya-api/oauth2/uuid", {
        method: 'POST', headers: { 'Content-Type': 'application/json', 'key': encrypted }, body
      }));
      if (tokenRes?.data?.token) {
        const headers = { ...aggConfig.headers.default, 'Authorization': tokenRes.data.token };
        const res = JSON.parse(await request(`${plat.host}${plat.url2}?videoCode=${videoCode}&itemId=${itemId}`, { headers }));
        if (res) {
          const d = res.data || res;
          const pic = `https://speed.hiknz.com/papaya/papaya-file/files/download/${d.imageKey}/${d.imageName}`;
          const u = (d.episodesList || []).map(e => {
            const best = e.resolutionList?.sort((a, b) => b.resolution - a.resolution)[0];
            return best ? `第${e.episodes}集$https://speed.hiknz.com/papaya/papaya-file/files/download/${best.fileKey}/${best.fileName}` : null;
          }).filter(Boolean).join('#');
          vod = { ...vod, vod_name: d.title, vod_pic: pic, vod_remarks: `共${d.episodesMax || 0}集`, vod_play_from: '碎片剧场', vod_play_url: u };
        }
      }
    }
  } catch (e) { vod.vod_name = '加载失败'; }
  return JSON.stringify({ list: [vod] });
}
async function play(flag, id) {
  if (/七猫/.test(flag)) {
    return JSON.stringify({ parse: 0, url: id });
  }
  if (/百度/.test(flag)) {
    const res = JSON.parse(await request(`https://api.jkyai.top/API/bddjss.php?video_id=${id}`));
    if (res?.data?.qualities) {
      const order = ["1080p", "sc", "sd"];
      const qMap = { "1080p": "蓝光", "sc": "超清", "sd": "标清" };
      const urls = [];
      order.forEach(k => {
        const q = res.data.qualities.find(x => x.quality === k);
        if (q) urls.push(qMap[k], q.download_url);
      });
      return JSON.stringify({ parse: 0, url: urls });
    }
  }
  if (/甜圈/.test(flag)) {
    return JSON.stringify({ parse: 0, url: `https://mov.cenguigui.cn/duanju/api.php?video_id=${id}&type=mp4` });
  }
  if (/锦鲤/.test(flag)) {
    try {
      const html = await request(`${id}&auto=1`, { headers: { referer: 'https://www.jinlidj.com/' } });
      const match = html.match(/let data\s*=\s*({[^;]*});/);
      if (match) {
        const data = JSON.parse(match[1]);
        if (data.url) return JSON.stringify({ parse: 0, url: data.url });
      }
    } catch {}
  }
  if (/番茄/.test(flag)) {
    try {
      const res = JSON.parse(await request(`https://fqgo.52dns.cc/video?item_ids=${id}`));
      if (res?.data?.[id]?.video_model) {
        const model = JSON.parse(res.data[id].video_model);
        const mainUrl = model?.video_list?.video_1?.main_url;
        if (mainUrl) return JSON.stringify({ parse: 0, url: base64Decode(mainUrl) });
      }
    } catch {}
  }
  if (/软鸭/.test(flag)) {
    const response = await request(`${aggConfig.platform.软鸭.host}/API/playlet/?video_id=${id}&quality=1080p`, { headers: aggConfig.headers.default });
    const res = JSON.parse(response);
    return JSON.stringify({ parse: 0, url: res.data?.video?.url || '' });
  }
  if (/围观/.test(flag)) {
    try {
      const ps = typeof id === 'string' ? JSON.parse(id) : id;
      const urls = [];
      if (ps?.super) urls.push("超清", ps.super);
      if (ps?.high) urls.push("高清", ps.high);
      if (ps?.normal) urls.push("流畅", ps.normal);
      return JSON.stringify({ parse: 0, url: urls });
    } catch { return JSON.stringify({ parse: 0, url: id }); }
  }
  return JSON.stringify({ parse: 0, url: id });
}
async function search(wd, quick, pg) {
  const page = parseInt(pg) || 1;
  const videos = [];
  try {
    const sign = await md5(`operation=2playlet_privacy=1search_word=${wd}${aggConfig.keys}`);
    const url = `${aggConfig.platform.七猫.host}${aggConfig.platform.七猫.search}?search_word=${encodeURIComponent(wd)}&playlet_privacy=1&operation=2&sign=${sign}`;
    const res = JSON.parse(await request(url, {
      headers: { ...await getHeaderX(), ...aggConfig.headers.default },
      timeout: aggConfig.search.timeout
    }));
    (res?.data?.list || []).forEach(i => {
      if (i.title.toLowerCase().includes(wd.toLowerCase())) {
        videos.push({
          vod_id: `七猫@${encodeURIComponent(i.playlet_id)}`,
          vod_name: i.title,
          vod_pic: i.image_link,
          vod_remarks: `七猫短剧｜${i.total_episode_num}集`
        });
      }
    });
  } catch {}
  const platforms = ['百度', '锦鲤', '番茄', '星芽', '西饭', '软鸭', '围观', '碎片', '甜圈'];
  for (const tid of platforms) {
    const plat = aggConfig.platform[tid];
    try {
      let res, data;
      if (tid === '百度') {
        res = await request(`${plat.host}${plat.search.replace('**', encodeURIComponent(wd)).replace('fypage', page)}`);
        data = (JSON.parse(res)?.data || []).map(i => ({ vod_id: `百度@${i.id}`, vod_name: i.title, vod_pic: i.cover, vod_remarks: `百度短剧｜更新至${i.totalChapterNum}集` }));
      } else if (tid === '锦鲤') {
        res = await request(plat.host + plat.search, { method: 'POST', body: JSON.stringify({ page, limit: 30, keyword: wd }) });
        data = (JSON.parse(res)?.data?.list || []).map(i => ({ vod_id: `锦鲤@${i.vod_id}`, vod_name: i.vod_name, vod_pic: i.vod_pic, vod_remarks: `锦鲤短剧｜${i.vod_total}集` }));
      } else if (tid === '番茄') {
        res = await request(`${plat.search}?keyword=${encodeURIComponent(wd)}&page=${page}`);
        data = (JSON.parse(res)?.data || []).map(i => ({ vod_id: `番茄@${i.series_id}`, vod_name: i.title, vod_pic: i.cover, vod_remarks: `番茄短剧｜${i.sub_title}` }));
      } else if (tid === '星芽') {
        const headers = await getXingyaHeaders();
        res = await request(plat.host + plat.search, { method: 'POST', headers, body: JSON.stringify({ text: wd }) });
        data = (JSON.parse(res)?.data?.theater?.search_data || []).map(i => ({ vod_id: `星芽@${plat.host}${plat.url2}?theater_parent_id=${i.id}`, vod_name: i.title, vod_pic: i.cover_url, vod_remarks: `星芽短剧｜${i.total}集 播放:${i.play_amount_str}` }));
      } else if (tid === '西饭') {
        const ts = Math.floor(Date.now() / 1000);
        const url = `${plat.host}${plat.search}?reqType=search&offset=${(page - 1) * 30}&keyword=${encodeURIComponent(wd)}&quickEngineVersion=-1&scene=`;
        res = await request(url);
        data = (JSON.parse(res)?.result?.elements || []).map(vod => {
          const dj = vod.duanjuVo || {};
          return { vod_id: `西饭@${dj.duanjuId}#${dj.source}`, vod_name: dj.title, vod_pic: dj.coverImageUrl, vod_remarks: `西饭短剧｜${dj.total}集` };
        });
      } else if (tid === '软鸭') {
        res = await request(`${plat.host}${plat.search}/?keyword=${encodeURIComponent(wd)}&page=${page}`);
        data = (JSON.parse(res)?.data || []).map(i => {
          const purl = `${i.title}@${i.cover}@${i.author}@${i.type}@${i.desc}@${i.book_id}`;
          return { vod_id: `软鸭@${encodeURIComponent(purl)}`, vod_name: i.title, vod_pic: i.cover, vod_remarks: `软鸭短剧｜${i.type}` };
        });
      } else if (tid === '围观') {
        res = await request(`${plat.host}${plat.search}`, {
          method: 'POST',
          body: JSON.stringify({ audience: "", page, pageSize: 30, searchWord: wd, subject: "" })
        });
        data = (JSON.parse(res)?.data || []).map(i => ({ vod_id: `围观@${i.oneId}`, vod_name: i.title, vod_pic: i.vertPoster, vod_remarks: `围观短剧｜集数:${i.episodeCount} 播放:${i.viewCount}` }));
      } else if (tid === '碎片') {
        let openId = (await md5(guid())).substring(0, 16);
        let body = JSON.stringify({ openId });
        let key = CryptoJS.enc.Utf8.parse("p0sfjw@k&qmewu#w");
        const encrypted = CryptoJS.AES.encrypt(body, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).ciphertext.toString(CryptoJS.enc.Hex);
        const tokenRes = JSON.parse(await request("https://free-api.bighotwind.cc/papaya/papaya-api/oauth2/uuid", {
          method: 'POST', headers: { 'Content-Type': 'application/json', 'key': encrypted }, body, timeout: aggConfig.search.timeout
        }));
        if (tokenRes?.data?.token) {
          const headers = { ...aggConfig.headers.default, 'Authorization': tokenRes.data.token };
          const url = `${plat.host}${aggConfig.platform.碎片.search}?type=5&tagId=&pageNum=${page}&pageSize=30&title=${encodeURIComponent(wd)}`;
          res = await request(url, { headers });
          data = (JSON.parse(res)?.list || []).map(i => ({
            vod_id: `碎片@${i.itemId}@${i.videoCode}`,
            vod_name: i.title,
            vod_pic: `https://speed.hiknz.com/papaya/papaya-file/files/download/${i.imageKey}/${i.imageName}`,
            vod_remarks: `碎片剧场｜集数:${i.episodesMax} 播放:${i.hitShowNum}`
          }));
        }
      } else if (tid === '甜圈') {
        const url = `${plat.host}${plat.search}=${encodeURIComponent(wd)}&offset=${page}`;
        res = await request(url);
        data = (JSON.parse(res)?.data || []).map(i => ({
          vod_id: `甜圈@${i.book_id}`,
          vod_name: i.title,
          vod_pic: i.cover,
          vod_remarks: `甜圈短剧｜${i.sub_title || '无简介'}`
        }));
      }
      if (data) videos.push(...data.filter(v => v.vod_name?.toLowerCase().includes(wd.toLowerCase())));
    } catch {}
  }
  return JSON.stringify({
    list: videos,
    page,
    pagecount: page + 1,
    limit: videos.length,
    total: videos.length * (page + 1)
  });
}
export function __jsEvalReturn() {
  return { init, home, homeVod, category, detail, play, proxy: null, search };
}
