# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
    parse_age_limit,
    parse_iso8601,
    smuggle_url,
)


class TenPlayIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?10play\.com\.au/(?:[^/]+/)+(?P<id>tpv\d{6}[a-z]{5})'
    _TESTS = [{
        'url': 'https://10play.com.au/masterchef/episodes/season-1/masterchef-s1-ep-1/tpv190718kwzga',
        'info_dict': {
            'id': '6060533435001',
            'ext': 'mp4',
            'title': 'MasterChef - S1 Ep. 1',
            'description': 'md5:4fe7b78e28af8f2d900cd20d900ef95c',
            'age_limit': 10,
            'timestamp': 1240828200,
            'upload_date': '20090427',
            'uploader_id': '2199827728001',
        },
        'params': {
            'format': 'bestvideo',
            'skip_download': True,
        }
    }, {
        'url': 'https://10play.com.au/how-to-stay-married/web-extras/season-1/terrys-talks-ep-1-embracing-change/tpv190915ylupc',
        'only_matching': True,
    }]
    BRIGHTCOVE_URL_TEMPLATE = 'https://players.brightcove.net/2199827728001/cN6vRtRQt_default/index.html?videoId=%s'

    def _real_extract(self, url):
        content_id = self._match_id(url)
        data = self._download_json(
            'https://10play.com.au/api/video/' + content_id, content_id)
        video = data.get('video') or {}
        metadata = data.get('metaData') or {}
        brightcove_id = video.get('videoId') or metadata['showContentVideoId']
        brightcove_url = smuggle_url(
            self.BRIGHTCOVE_URL_TEMPLATE % brightcove_id,
            {'geo_countries': ['AU']})

        return {
            '_type': 'url_transparent',
            'url': brightcove_url,
            'id': content_id,
            'title': video.get('title') or metadata.get('pageContentName') or metadata.get('showContentName'),
            'description': video.get('description'),
            'age_limit': parse_age_limit(video.get('showRatingClassification') or metadata.get('showProgramClassification')),
            'series': metadata.get('showName'),
            'season': metadata.get('showContentSeason'),
            'timestamp': parse_iso8601(metadata.get('contentPublishDate') or metadata.get('pageContentPublishDate')),
            'ie_key': 'BrightcoveNew',
        }

@@ -106,16 +106,33 @@
 mport (%0A
+    HEADRequest,%0A
     pars
@@ -166,16 +166,18 @@
 601,%0A   
+ #
  smuggle
@@ -816,16 +816,18 @@
         
+ #
  'format
@@ -1067,16 +1067,18 @@
   %7D%5D%0A   
+ #
  BRIGHTC
@@ -1182,16 +1182,164 @@
 eoId=%25s'
+%0A    _GEO_BYPASS = False%0A    _FASTLY_URL_TEMPL = 'https://10-selector.global.ssl.fastly.net/s/kYEXFC/media/%25s?mbr=true&manifest=m3u&format=redirect'
 %0A%0A    de
@@ -1680,32 +1680,34 @@
 ideoId'%5D%0A       
+ #
  brightcove_url 
@@ -1724,24 +1724,26 @@
 url(%0A       
+ #
      self.BR
@@ -1784,24 +1784,26 @@
 _id,%0A       
+ #
      %7B'geo_c
@@ -1821,16 +1821,359 @@
 %5B'AU'%5D%7D)
+%0A        m3u8_url = self._request_webpage(HEADRequest(%0A            self._FASTLY_URL_TEMPL %25 brightcove_id), brightcove_id).geturl()%0A        if '10play-not-in-oz' in m3u8_url:%0A            self.raise_geo_restricted(countries=%5B'AU'%5D)%0A        formats = self._extract_m3u8_formats(m3u8_url, brightcove_id, 'mp4')%0A        self._sort_formats(formats)
 %0A%0A      
@@ -2194,16 +2194,18 @@
         
+ #
  '_type'
@@ -2232,24 +2232,26 @@
 %0A           
+ #
  'url': brig
@@ -2279,20 +2279,55 @@
    '
-id': content
+formats': formats,%0A            'id': brightcove
 _id,
@@ -2405,37 +2405,33 @@
 me') or metadata
-.get(
+%5B
 'showContentName
@@ -2427,25 +2427,25 @@
 ContentName'
-)
+%5D
 ,%0A          
@@ -2847,16 +2847,108 @@
         
+ 'thumbnail': video.get('poster'),%0A            'uploader_id': '2199827728001',%0A            #
  'ie_key

fix format extraction(closes #26653)