{
  "apiVersion": 1,
  "identityFields": [],
  "id": "mango",
  "name": "Mango TV",
  "locales": {
    "zh-CN": {
      "name": "芒果TV"
    }
  },
  "version": "0.7.1",
  "hosts": [
    "mobileso.bz.mgtv.com",
    "pcweb.api.mgtv.com",
    "galaxy.bz.mgtv.com",
    "*.mgtv.com",
    "*.hitv.com"
  ],
  "search": {
    "inputs": ["q"],
    "steps": [
      {
        "type": "assign",
        "id": "device",
        "values": {
          "deviceId": "$md5($string($millis()))"
        }
      },
      {
        "type": "http",
        "id": "search",
        "request": {
          "method": "GET",
          "url": "'https://mobileso.bz.mgtv.com/aphone/search/rebirth/v2'",
          "query": "{ 'q': q, '_support': '10100001', 'device': '23127PN0CC', 'osVersion': '16', 'appVersion': '9.3.3', 'did': device.deviceId, 'mac': device.deviceId, 'seqId': $md5(device.deviceId & '.' & $string($millis())), 'ticket': '', 'userId': '0', 'osType': 'android', 'type': '10', 'abroad': '0' }",
          "rewriteHeaders": {
            "User-Agent": "'Dalvik/2.1.0 (Linux; U; Android 16; 23127PN0CC Build/AP1A.240505.003)'"
          }
        }
      }
    ],
    "output": "($media := search.data.contents[type='mediaRebirthV2' or type='mediaRebirthV3'].data[($not($exists(source)) or source = '' or source = 'imgo') and clipId != '']; $seen := [$map($media, function($m) { $string($m.clipId) })]; $series := search.data.contents[type='clipMerge'].data.dataList[clipId != '' and $not($string(clipId) in $seen)]; $append([$media.($cid := $string(clipId); $descText := $type(desc) = 'array' ? desc[0] : desc; { 'providerIds': { 'collectionId': $cid }, 'indexedId': $cid, 'title': $trim($replace(title, /<[^>]+>/, '')), 'type': $trim($replace($substringBefore($descText, '/'), '类型:', '')), 'year': $number($regexExtract($descText, '([12][890][0-9][0-9])')), 'imageUrl': img, 'episodeCount': videoCount })], [$series.($cid := $string(clipId); { 'providerIds': { 'collectionId': $cid }, 'indexedId': $cid, 'title': $trim($replace(title, /<[^>]+>/, '')), 'imageUrl': img })]))"
  },
  "episodes": {
    "inputs": ["collectionId"],
    "steps": [
      {
        "type": "http",
        "id": "bootstrap",
        "request": {
          "method": "GET",
          "url": "'https://pcweb.api.mgtv.com/variety/showlist'",
          "query": "{ 'allowedRC': '1', 'collection_id': collectionId, 'month': '', 'page': '1', '_support': '10000000' }",
          "rewriteHeaders": { "Referer": "'https://www.mgtv.com/'" }
        },
        "extract": {
          "months": "[data.tab_m.m]"
        }
      },
      {
        "type": "forEach",
        "id": "pages",
        "in": "bootstrap.months",
        "as": "month",
        "request": {
          "method": "GET",
          "url": "'https://pcweb.api.mgtv.com/variety/showlist'",
          "query": "{ 'allowedRC': '1', 'collection_id': collectionId, 'month': month, 'page': '1', '_support': '10000000' }",
          "rewriteHeaders": { "Referer": "'https://www.mgtv.com/'" }
        },
        "extract": {
          "items": "data.list"
        },
        "collect": "items",
        "throttleMs": 100
      }
    ],
    "output": "[($filter(pages, function($p) { $p.src_clip_id = collectionId })^(ts)).($n := $regexExtract(t1 & t2, '第(\\\\d+)集'); { 'providerIds': { 'vid': video_id, 'cid': $string($$.collectionId) }, 'indexedId': $string(video_id), 'title': $trim(t1 & ' ' & t2), 'episodeNumber': $n ? $number($n) : undefined })]"
  },
  "danmaku": {
    "inputs": ["cid", "vid"],
    "steps": [
      {
        "type": "http",
        "id": "info",
        "request": {
          "method": "GET",
          "url": "'https://pcweb.api.mgtv.com/video/info'",
          "query": "{ 'cid': cid, 'vid': vid }",
          "rewriteHeaders": { "Referer": "'https://www.mgtv.com/'" }
        },
        "extract": {
          "durationSec": "$timeToSeconds(data.info.time)"
        }
      },
      {
        "type": "http",
        "id": "ctl",
        "request": {
          "method": "GET",
          "url": "'https://galaxy.bz.mgtv.com/getctlbarrage'",
          "query": "{ 'version': '8.1.39', 'abroad': '0', 'uuid': '', 'os': '10.15.7', 'platform': '0', 'mac': '', 'vid': vid, 'pid': '', 'cid': cid, 'ticket': '' }",
          "rewriteHeaders": { "Referer": "'https://www.mgtv.com/'" }
        },
        "extract": {
          "cdn": "$substringBefore(data.cdn_list, ',')",
          "version": "data.cdn_version"
        }
      },
      {
        "type": "forEach",
        "id": "segments",
        "in": "(ctl.cdn and ctl.version) ? $range(0, $ceil(info.durationSec / 60)) : []",
        "as": "i",
        "request": {
          "method": "GET",
          "url": "'https://' & ctl.cdn & '/' & ctl.version & '/' & $string(i) & '.json'"
        },
        "extract": {
          "items": "data.items"
        },
        "collect": "items",
        "throttleMs": 50,
        "concurrency": 4
      }
    ],
    "output": "[segments.{ 'p': $string($number(time) / 1000) & ',1,16777215,' & $string(uid), 'm': content }]"
  }
}
