{
  "$schema": "https://files-apicasystem-com.s3-eu-west-1.amazonaws.com/schemas/url-xi-schema-v1-0.json",
  "name": "SvtPlay Play Video",
  "description": "Play a video from the documentary category",
  "baseURL": "https://api.svt.se",
  "message": "Download speed {{totDownloadMBps}} MB/s, Video/Audio Content Length = {{totContentLength}}",
  "variables": [
    {
      "key": "format",
      "type": "string",
      "usage": "input",
      "value": "dash-hbbtv-avc",
      "validation": "value.startsWith('dash')"
    },
    {
      "key": "videoChunks",
      "type": "number",
      "usage": "input",
      "value": 10,
      "validation": "value > 0 && value < 21",
      "unit": "chunks"
    },
    {
      "key": "representationId",
      "type": "number",
      "usage": "input",
      "value": 1,
      "validation": "value > 0 && value < 5"
    },
    {
      "key": "programTitle",
      "type": "string",
      "usage": "inResponse",
      "value": ""
    },
    {
      "key": "episodeTitle",
      "type": "string",
      "usage": "inResponse",
      "value": ""
    },
    {
      "key": "geoBlockedSweden",
      "type": "boolean",
      "usage": "inResponse",
      "value": false
    },
    {
      "key": "codecs",
      "type": "string",
      "usage": "inResponse",
      "value": ""
    },
    {
      "key": "bandwidth",
      "type": "number",
      "usage": "inResponse",
      "value": ""
    },
    {
      "key": "totDownloadMBps",
      "type": "number",
      "usage": "returnValue",
      "value": 0,
      "unit": "MB/s"
    }
  ],
  "steps": [
    {
      "name": "Get Random documentary",
      "requests": [
        {
          "name": "Get Graphql query",
          "config": {
            "url": "/contento/graphql",
            "params": {
              "ua": "svtplaywebb-play-render-prod-client",
              "operationName": "CategoryPageQuery",
              "variables": {
                "abTestVariants": [],
                "history": null,
                "id": "dokumentar",
                "includeFullOppetArkiv": true,
                "tab": null
              },
              "extensions": {
                "persistedQuery": {
                  "version": 1,
                  "sha256Hash": "8310718ae92359ab2d84968ccbe4a92824dd7684f612119007b1159a4c358ec0"
                }
              }
            }
          },
          "extractors": [
            {
              "type": "jsonpath",
              "expression": "$.data.categoryPage.lazyLoadedTabs[0].selections[0].items[*].item.videoSvtId",
              "variable": "videoSvtId",
              "notFoundError": true
            }
          ]
        }
      ]
    },
    {
      "name": "Get Video Meta data",
      "requests": [
        {
          "name": "Get Program title",
          "config": {
            "url": "/video/{{videoSvtId}}"
          },
          "extractors": [
            {
              "type": "jsonpath",
              "expression": "$.programTitle",
              "variable": "programTitle"
            },
            {
              "type": "jsonpath",
              "expression": "$.episodeTitle",
              "variable": "episodeTitle"
            },
            {
              "type": "jsonpath",
              "expression": "$.rights.geoBlockedSweden",
              "variable": "geoBlockedSweden"
            },
            {
              "type": "jsonpath",
              "expression": "$.videoReferences[?(@.format =='{{format}}')].resolve",
              "variable": "resolver"
            }
          ]
        }
      ]
    },
    {
      "name": "Get Manifest",
      "requests": [
        {
          "name": "Get Main manifest resolver",
          "config": {
            "url": "{{resolver}}"
          },
          "extractors": [
            {
              "type": "jsonpath",
              "expression": "$.location",
              "variable": "manifest"
            }
          ],
          "transformers": [
            {
              "type": "extract",
              "source": "{{manifest}}",
              "target": "videoHostPath",
              "from": "^((?:\/\/|[^\/]+)*(.*))\/"
            }
          ]
        },
        {
          "name": "Get xml manifest",
          "config": {
            "url": "{{manifest}}"
          },
          "extractors": [
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][1][@contentType='video']/*[local-name() = 'Representation'][@id={{representationId}}]/*[local-name() = 'SegmentTemplate']/@media",
              "variable": "videoTemplate",
              "notFoundError": true
            },
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][1][@contentType='video']/*[local-name() = 'Representation'][@id={{representationId}}]/*[local-name() = 'SegmentTemplate']/@initialization",
              "variable": "videoInit",
              "notFoundError": true
            },
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][@contentType='audio']/*[local-name() = 'Representation'][@mimeType='audio/mp4']/*[local-name() = 'SegmentTemplate']/@media",
              "variable": "audioTemplate",
              "notFoundError": true
            },
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][@contentType='audio']/*[local-name() = 'Representation'][@mimeType='audio/mp4']/*[local-name() = 'SegmentTemplate']/@initialization",
              "variable": "audioInit",
              "notFoundError": true
            },
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][1][@contentType='video']/*[local-name() = 'Representation'][@id={{representationId}}]/@bandwidth",
              "variable": "bandwidth"
            },
            {
              "type": "xpath",
              "expression": "//*[local-name() = 'AdaptationSet'][1][@contentType='video']/*[local-name() = 'Representation'][@id={{representationId}}]/@codecs",
              "variable": "codecs"
            }
          ],
          "transformers": [
            {
              "type": "replace",
              "source": "{{videoHostPath}}/{{videoTemplate}}",
              "target": "videoChunk",
              "from": "$Number$",
              "to": "{{$lapIdx1}}"
            },
            {
              "type": "replace",
              "source": "{{videoHostPath}}/{{audioTemplate}}",
              "target": "audioChunk",
              "from": "$Number$",
              "to": "{{$lapIdx1}}"
            }
          ]
        }
      ]
    },
    {
      "name": "Video Init",
      "requests": [
        {
          "name": "Audio initialization segment",
          "config": {
            "url": "{{videoHostPath}}/{{audioInit}}"
          },
          "notSaveData": true
        },
        {
          "name": "Video initialization segment",
          "config": {
            "url": "{{videoHostPath}}/{{videoInit}}"
          },
          "notSaveData": true
        }
      ]
    },
    {
      "name": "Play Video Segments",
  
      "iterator": {
        "value": "{{videoChunks}}"
      },
      "scripts": [
        {
          "name": "CalcTotalMBps",
          "scope": "after",
          "script": [
            "const totContentLength =uxs.getVar('totContentLength')",
            "const totDownloadDuration=uxs.getVar('totDownloadDuration')",
            "if (totContentLength) {",
            "uxs.setVar('totDownloadMBps',  (totContentLength * 1000/totDownloadDuration/(1024*1024)))",
            

            "}",
            "else {",
            "uxs.setVar('totDownloadMBps', 0)",
            "}"
          ]
        },
        {
          "name": "CalcMBps",
          "scope": "afterEach",
          "script": [
            "const contentLength=uxs.getVar('$contentLength')",
            "const durationMs=uxs.getVar('$durationMs')",
            "if (contentLength) {",
            "uxs.setVar('MBps',  (contentLength * 1000/durationMs/(1024*1024)).toFixed(2))",
            "let totContentLength = uxs.getVar('totContentLength')",
            "uxs.setVar('totContentLength',totContentLength +contentLength)",
            "let totDownloadDuration = uxs.getVar('totDownloadDuration')",
            "uxs.setVar('totDownloadDuration',totDownloadDuration +durationMs)",

            "}",
            "else {",
            "uxs.setVar('MBps', 0)",
            "}"
          ]
        }
      ],
      "requests": [
        {
          "name": "Audio segment {{$lapIdx1}}",
          "config": {
            "url": "{{audioChunk}}"
          },
          "message": "Audio download Speed {{MBps}} MegaBytes/s",
          "notSaveData": true
        },
        {
          "name": "Video segment {{$lapIdx1}}",
          "config": {
            "url": "{{videoChunk}}"
          },
          "message": "Video download Speed {{MBps}} MegaBytes/s",
          "notSaveData": true
        }
      ]
    }
  ]
}