{
    "$schema": "file:///usr/local/lib/node_modules/@apica-io/url-xi/config/url-xi-schema-v1-0.json",
    "name": "SvtPlay Play Video",
    "description": "Play a video from the documentary category",
    "baseURL": "https://api.svt.se",
    "variables": [
        {
            "key": "format",
            "type": "string",
            "usage": "input",
            "value": "dash-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": "codecs",
            "type": "string",
            "usage": "inResponse",
            "value": ""
        },
        {
            "key": "bandwidth",
            "type": "number",
            "usage": "inResponse",
            "value": ""
        }
    ],
    "steps": [
        {
            "name": "Get Random documentary",
            "requests": [
                {
                    "name": "Get Graphql query",
                    
                    "config": {
                        "url": "/contento/graphql",
                        "params": {
                            "ua": "svtplaywebb-play-render-prod-client",
                            "operationName": "GenrePag",
                            "variables": {
                                "includes": "recommended",
                                "cluster": [
                                    "dokumentar"
                                ]
                            },
                            "extensions": {
                                "persistedQuery": {
                                    "version": 1,
                                    "sha256Hash": "868ba595956599c802d5bf45af0af6fec9765d8cead96728d6eff8ed64cb5238"
                                }
                            }
                        }
                    },
                    "extractors": [
                        {
                            "type": "jsonpath",
                            "expression": "$.data.genres[0].selectionsForWeb[0].items[*].item.urls.svtplay",
                            "variable": "urlSvtPlay"
                        }
                    ],
                    "transformers": [
                        {
                            "type":"extract",
                            "source": "{{urlSvtPlay}}",
                            "target": "legacyId, videoName",
                            "from": "\\/video\/(\\d+)\/(.+)"
                        }
                    ]
                },
                {
                    "name": "Get Video page",
                    "config": {
                        "url": "/contento/graphql",
                        "params": {
                            "ua": "svtplaywebb-play-render-prod-client",
                            "operationName": "VideoPage",
                            "variables": {
                                "legacyIds": [
                                    "{{legacyId}}"
                                ]
                            },
                            "extensions": {
                                "persistedQuery": {
                                    "version": 1,
                                    "sha256Hash": "6ae6ce26d0925cfbeae7d521ab8f512d14609c6188d81d7cfaa00c52847fcc49"
                                }
                            }
                        }
                    },
                    "extractors": [
                        {
                            "type": "jsonpath",
                            "expression": "$.data.listablesByEscenicId[0].svtId",
                            "variable": "svtId"
                        },
                        {
                            "type": "jsonpath",
                            "expression": "$.data.listablesByEscenicId[0].videoSvtId",
                            "variable": "videoSvtId"
                        }
                    ]
                }
            ]
        },
        {
            "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": "$.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"
                        },
                        {
                            "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}}"

                        }
                    ]
                }
            ]
        },
        {
            "name": "Play Video",
            "iterator": {
                "value": "{{videoChunks}}"
            },
            "requests": [
                {
                    "name":"Video chunk {{$lapIdx1}}",
                    "config": {
                        "url": "{{videoChunk}}"
                    }
               
                }
            ]
        }
    ]
}