From 35779eda7a2ad52f2bbbe2e1251ad8ddb4bac7d1 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Fri, 19 Feb 2021 14:10:10 +0000
Subject: [PATCH 1/9] Dealing with RTP latest changes

---
 youtube_dl/extractor/rtp.py | 75 +++++++++++++++++++++++++++----------
 1 file changed, 55 insertions(+), 20 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 02986f442..665c86f14 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -6,22 +6,27 @@ from ..utils import (
     determine_ext,
     js_to_json,
 )
+from ..compat import (
+    compat_b64decode,
+    compat_urllib_parse_unquote,
+)
+
+import re
 
 
 class RTPIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
+    _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/(.*\/)?p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
     _TESTS = [{
-        'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
-        'md5': 'e736ce0c665e459ddb818546220b4ef8',
+        'url': 'https://www.rtp.pt/play/p117/e476527/os-contemporaneos',
         'info_dict': {
-            'id': 'e174042',
-            'ext': 'mp3',
-            'title': 'Paixões Cruzadas',
-            'description': 'As paixões musicais de António Cartaxo e António Macedo',
+            'id': 'e476527',
+            'ext': 'mp4',
+            'title': 'Os Contemporâneos Episódio 1 -  RTP Play - RTP',
+            'description': 'Os Contemporâneos, um programa de humor com um olhar na sociedade portuguesa!',
             'thumbnail': r're:^https?://.*\.jpg',
         },
     }, {
-        'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
+        'url': 'https://www.rtp.pt/play/p510/aleixo-fm',
         'only_matching': True,
     }]
 
@@ -29,30 +34,60 @@ class RTPIE(InfoExtractor):
         video_id = self._match_id(url)
 
         webpage = self._download_webpage(url, video_id)
-        title = self._html_search_meta(
-            'twitter:title', webpage, display_name='title', fatal=True)
+        title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
+
+        # Get JS object
+        js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
+
+        json_string_for_config = ''
+
+        # Verify JS object since it isn't pure JSON and maybe it needs some decodings
+        for line in js_object.splitlines():
+            stripped_line = line.strip()
+
+            # If JS object key is 'file'
+            if re.match('file ?:', stripped_line):
+                if 'decodeURIComponent' in stripped_line:
+                    # 1) The file URL is inside object and with HLS encoded...
+                    hls_encoded = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0]
+                    hls_encoded = hls_encoded.replace('"', '').replace('\'', '').replace(',', '')
+                    decoded_file_url = compat_b64decode(
+                        compat_urllib_parse_unquote(
+                            hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
+
+                    # Insert the decoded HLS file URL into pure JSON string
+                    json_string_for_config += '\nfile: "' + decoded_file_url + '",'
+                else:
+                    # 2) ... or it's a direct M3U8 file
+                    json_string_for_config += '\n' + line
+
+            elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line):
+                # Ignore commented lines and 'fileKey' entry since it is no longer supported by RTP
+                json_string_for_config += '\n' + line
+
+        # Finally send pure JSON string for JSON parsing
+        config = self._parse_json(json_string_for_config, video_id, js_to_json)
+
+        # config = self._parse_json(self._search_regex(
+        #       r'(?s)RTPPlayer ?\( ?({.+?})\);', webpage,
+        #       'player config'), video_id, js_to_json)
 
-        config = self._parse_json(self._search_regex(
-            r'(?s)RTPPlayer\(({.+?})\);', webpage,
-            'player config'), video_id, js_to_json)
         file_url = config['file']
         ext = determine_ext(file_url)
+
         if ext == 'm3u8':
-            file_key = config.get('fileKey')
+            # Download via m3u8 file
             formats = self._extract_m3u8_formats(
                 file_url, video_id, 'mp4', 'm3u8_native',
-                m3u8_id='hls', fatal=file_key)
-            if file_key:
-                formats.append({
-                    'url': 'https://cdn-ondemand.rtp.pt' + file_key,
-                    'preference': 1,
-                })
+                m3u8_id='hls')
+
             self._sort_formats(formats)
         else:
             formats = [{
                 'url': file_url,
                 'ext': ext,
             }]
+
         if config.get('mediaType') == 'audio':
             for f in formats:
                 f['vcodec'] = 'none'

From a85625977d39b4ad5bb9b399773700fa20db7587 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Sat, 20 Feb 2021 20:42:33 +0000
Subject: [PATCH 2/9] 'Estudo em Casa' wasn't working since RTP has a lot of
 ways of dealing with their code

---
 youtube_dl/extractor/rtp.py | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 665c86f14..02716c172 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -58,7 +58,7 @@ class RTPIE(InfoExtractor):
                     # Insert the decoded HLS file URL into pure JSON string
                     json_string_for_config += '\nfile: "' + decoded_file_url + '",'
                 else:
-                    # 2) ... or it's a direct M3U8 file
+                    # 2) ... or the file URL is not encoded so keep it that way
                     json_string_for_config += '\n' + line
 
             elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line):
@@ -68,11 +68,12 @@ class RTPIE(InfoExtractor):
         # Finally send pure JSON string for JSON parsing
         config = self._parse_json(json_string_for_config, video_id, js_to_json)
 
-        # config = self._parse_json(self._search_regex(
-        #       r'(?s)RTPPlayer ?\( ?({.+?})\);', webpage,
-        #       'player config'), video_id, js_to_json)
+        # Check if file URL is directly a string or is still inside object
+        if isinstance(config['file'], str):
+            file_url = config['file']
+        else:
+            file_url = config['file']['hls']
 
-        file_url = config['file']
         ext = determine_ext(file_url)
 
         if ext == 'm3u8':

From b955e0a5dc208abaa5f7d64e7228c46bb8a3af63 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Sun, 21 Feb 2021 14:05:42 +0000
Subject: [PATCH 3/9] RTP devs like to try out different approaches

---
 youtube_dl/extractor/rtp.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 02716c172..5ba26268e 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -15,11 +15,11 @@ import re
 
 
 class RTPIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/(.*\/)?p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
+    _VALID_URL = r'https?://(?:(?:(?:www\.)?rtp\.pt/play/(?P<subarea>.*/)?p(?P<program_id>[0-9]+)/(?P<episode_id>e[0-9]+/)?)|(?:arquivos\.rtp\.pt/conteudos/))(?P<id>[^/?#]+)/?'
     _TESTS = [{
         'url': 'https://www.rtp.pt/play/p117/e476527/os-contemporaneos',
         'info_dict': {
-            'id': 'e476527',
+            'id': 'os-contemporaneos',
             'ext': 'mp4',
             'title': 'Os Contemporâneos Episódio 1 -  RTP Play - RTP',
             'description': 'Os Contemporâneos, um programa de humor com um olhar na sociedade portuguesa!',
@@ -51,9 +51,12 @@ class RTPIE(InfoExtractor):
                     # 1) The file URL is inside object and with HLS encoded...
                     hls_encoded = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0]
                     hls_encoded = hls_encoded.replace('"', '').replace('\'', '').replace(',', '')
-                    decoded_file_url = compat_b64decode(
-                        compat_urllib_parse_unquote(
-                            hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
+                    if 'atob' in stripped_line:
+                        decoded_file_url = compat_b64decode(
+                            compat_urllib_parse_unquote(
+                                hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
+                    else:
+                        decoded_file_url = compat_urllib_parse_unquote(hls_encoded)
 
                     # Insert the decoded HLS file URL into pure JSON string
                     json_string_for_config += '\nfile: "' + decoded_file_url + '",'

From 7f40887b29f97d4729e344b04e76456f02c0c33a Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Tue, 13 Apr 2021 21:03:59 +0100
Subject: [PATCH 4/9] Ignore more comments in RTP source code

---
 youtube_dl/extractor/rtp.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 5ba26268e..1c487749b 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -64,8 +64,8 @@ class RTPIE(InfoExtractor):
                     # 2) ... or the file URL is not encoded so keep it that way
                     json_string_for_config += '\n' + line
 
-            elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line):
-                # Ignore commented lines and 'fileKey' entry since it is no longer supported by RTP
+            elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line) and not re.match('.*extraSettings ?:', stripped_line):
+                # Ignore commented lines, 'fileKey' entry since it is no longer supported by RTP and also 'extraSettings'
                 json_string_for_config += '\n' + line
 
         # Finally send pure JSON string for JSON parsing

From ff47d11269d6cffb36c8b86cf8f5e8effa881670 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Wed, 21 Apr 2021 16:24:26 +0100
Subject: [PATCH 5/9] Ignore EVEN more comments in RTP source code

---
 youtube_dl/extractor/rtp.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 1c487749b..2f9360e12 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -34,6 +34,10 @@ class RTPIE(InfoExtractor):
         video_id = self._match_id(url)
 
         webpage = self._download_webpage(url, video_id)
+
+        # Remove JS multi-line comments from webpage source
+        webpage = re.sub(r'(\/\*.*\*\/)', '', webpage, flags=re.DOTALL)
+
         title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
 
         # Get JS object

From d30180d4f3e1c0f1f01f7a3aa3d3fa250ce23b15 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Fri, 23 Apr 2021 23:02:04 +0100
Subject: [PATCH 6/9] Consider multi-part videos in filename output

---
 youtube_dl/extractor/rtp.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index 2f9360e12..da30a7789 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -40,6 +40,14 @@ class RTPIE(InfoExtractor):
 
         title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
 
+        # Replace irrelevant text in title
+        title = title.replace(' - RTP Play - RTP', '')
+
+        # Check if it's a video split in parts, if so add part number to title
+        part = self._html_search_regex(r'section\-parts.*<span.*>(.+?)</span>.*</ul>', webpage, 'part', default=None)
+        if part:
+            title = f'{title} {part}'
+
         # Get JS object
         js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
 

From 5a7243b741f2f9a746b0955de8bae2f558efd074 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Thu, 6 May 2021 23:20:20 +0100
Subject: [PATCH 7/9] Working around new URLs

---
 youtube_dl/extractor/rtp.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index da30a7789..d421f2f1a 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -70,6 +70,9 @@ class RTPIE(InfoExtractor):
                     else:
                         decoded_file_url = compat_urllib_parse_unquote(hls_encoded)
 
+                    # Workaround for new behaviour
+                    decoded_file_url = decoded_file_url.replace('streaming-vod.rtp.pt/hls/', 'streaming-ondemand.rtp.pt/').replace('.mp4/', '/')
+
                     # Insert the decoded HLS file URL into pure JSON string
                     json_string_for_config += '\nfile: "' + decoded_file_url + '",'
                 else:

From 6bde4a13779bd8b7202240cf5036be4a2ca242b2 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Sat, 28 Aug 2021 21:10:18 +0100
Subject: [PATCH 8/9] Fix 'RTP Arquivos' downloads, allow subtitles download,
 more tests, general cleanup

---
 youtube_dl/extractor/rtp.py | 135 +++++++++++++++++++++++++++---------
 1 file changed, 102 insertions(+), 33 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index d421f2f1a..ffa915075 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
 
 from .common import InfoExtractor
 from ..utils import (
+    ExtractorError,
     determine_ext,
     js_to_json,
 )
@@ -17,13 +18,53 @@ import re
 class RTPIE(InfoExtractor):
     _VALID_URL = r'https?://(?:(?:(?:www\.)?rtp\.pt/play/(?P<subarea>.*/)?p(?P<program_id>[0-9]+)/(?P<episode_id>e[0-9]+/)?)|(?:arquivos\.rtp\.pt/conteudos/))(?P<id>[^/?#]+)/?'
     _TESTS = [{
-        'url': 'https://www.rtp.pt/play/p117/e476527/os-contemporaneos',
+        'url': 'https://www.rtp.pt/play/p117/e563265/os-contemporaneos',
         'info_dict': {
             'id': 'os-contemporaneos',
             'ext': 'mp4',
-            'title': 'Os Contemporâneos Episódio 1 -  RTP Play - RTP',
+            'title': 'Os Contemporâneos Episódio 1',
             'description': 'Os Contemporâneos, um programa de humor com um olhar na sociedade portuguesa!',
-            'thumbnail': r're:^https?://.*\.jpg',
+            'thumbnail': r're:^https?://.*\.(jpg|png)'
+        },
+    }, {
+        'url': 'https://www.rtp.pt/play/p8157/e541212/telejornal',
+        'info_dict': {
+            'id': 'telejornal',
+            'ext': 'mp4',
+            'title': 'Telejornal de 01 Mai 2021 PARTE 1',
+            'description': 'A mais rigorosa seleção de notícias, todos os dias às 20h00. De segunda a domingo, João Adelino Faria e José Rodrigues dos Santos mostram-lhe o que de'
+        },
+    }, {
+        'url': 'https://www.rtp.pt/play/p6646/e457262/grande-entrevista',
+        'info_dict': {
+            'id': 'grande-entrevista',
+            'ext': 'mp4',
+            'title': 'Grande Entrevista Episódio 7 - de 19 Fev 2020',
+            'description': 'Bruno Nogueira - É um dos mais originais humoristas portugueses e de maior êxito! Bruno Nogueira na Grande Entrevista com Vítor Gonçalves.'
+        },
+    }, {
+        'url': 'https://www.rtp.pt/play/estudoemcasa/p7776/e539826/portugues-1-ano',
+        'info_dict': {
+            'id': 'portugues-1-ano',
+            'ext': 'mp4',
+            'title': 'Português - 1.º ano , aula 45 -  27 Abr 2021 - Estudo Em Casa - RTP',
+            'description': 'A História do Pedrito Coelho, de Beatrix Potter. O dígrafo \'lh\' - A História do Pedrito Coelho, de Beatrix Potter. O dígrafo \'lh\'.'
+        },
+    }, {
+        'url': 'https://www.rtp.pt/play/zigzag/p5449/e385973/banda-zig-zag',
+        'info_dict': {
+            'id': 'banda-zig-zag',
+            'ext': 'mp4',
+            'title': 'Banda Zig Zag Episódio 1 -  Zig Zag Play - RTP',
+            'description': 'A Amizade é o Nosso Mel - Zig: é a menina que além de tocar também canta. Adora aprender palavras novas e adora ler. Gosta de fazer palavras cruzadas'
+        },
+    }, {
+        'url': 'https://arquivos.rtp.pt/conteudos/liga-dos-ultimos-152/',
+        'info_dict': {
+            'id': 'liga-dos-ultimos-152',
+            'ext': 'mp4',
+            'title': 'Liga dos Últimos – RTP Arquivos',
+            'description': 'Magazine desportivo, com apresentação de Álvaro Costa e comentários em estúdio do professor Hernâni Gonçalves e do sociólogo João Nuno Coelho. Destaque para os jogos de futebol das equipas dos escalões secundários de Portugal, com momentos dos jogos: Agrário de Lamas vs Pampilhoense e Apúlia vs Fragoso.'
         },
     }, {
         'url': 'https://www.rtp.pt/play/p510/aleixo-fm',
@@ -40,8 +81,11 @@ class RTPIE(InfoExtractor):
 
         title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
 
+        if 'Este episódio não se encontra disponível' in title:
+            raise ExtractorError('Episode unavailable', expected=True)
+
         # Replace irrelevant text in title
-        title = title.replace(' - RTP Play - RTP', '')
+        title = re.sub(r' -  ?RTP Play - RTP', '', title)
 
         # Check if it's a video split in parts, if so add part number to title
         part = self._html_search_regex(r'section\-parts.*<span.*>(.+?)</span>.*</ul>', webpage, 'part', default=None)
@@ -52,69 +96,94 @@ class RTPIE(InfoExtractor):
         js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
 
         json_string_for_config = ''
+        filekey_found = False
 
-        # Verify JS object since it isn't pure JSON and maybe it needs some decodings
+        # Verify JS object since it isn't pure JSON and probably needs some fixing
         for line in js_object.splitlines():
             stripped_line = line.strip()
 
-            # If JS object key is 'file'
-            if re.match('file ?:', stripped_line):
+            # If JS object key is 'fileKey'
+            if re.match('fileKey ?:', stripped_line):
+                filekey_found = True
                 if 'decodeURIComponent' in stripped_line:
-                    # 1) The file URL is inside object and with HLS encoded...
-                    hls_encoded = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0]
-                    hls_encoded = hls_encoded.replace('"', '').replace('\'', '').replace(',', '')
+                    # 1) The value is an encoded URL
+                    encoded_url = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0]
+                    encoded_url = re.sub(r'[\s"\',]', '', encoded_url)
+
                     if 'atob' in stripped_line:
-                        decoded_file_url = compat_b64decode(
+                        # Most of the times 'atob' approach is used but not always so we need to be sure
+                        decoded_url = compat_b64decode(
                             compat_urllib_parse_unquote(
-                                hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
+                                encoded_url)).decode('utf-8')
                     else:
-                        decoded_file_url = compat_urllib_parse_unquote(hls_encoded)
+                        # If no 'atob' we just need to unquote it
+                        decoded_url = compat_urllib_parse_unquote(encoded_url)
 
-                    # Workaround for new behaviour
-                    decoded_file_url = decoded_file_url.replace('streaming-vod.rtp.pt/hls/', 'streaming-ondemand.rtp.pt/').replace('.mp4/', '/')
-
-                    # Insert the decoded HLS file URL into pure JSON string
-                    json_string_for_config += '\nfile: "' + decoded_file_url + '",'
+                    # Insert the (relative) decoded URL in JSON
+                    json_string_for_config += f'\nfileKey: "{decoded_url}",'
                 else:
-                    # 2) ... or the file URL is not encoded so keep it that way
-                    json_string_for_config += '\n' + line
+                    # 2) ... or the value URL is not encoded so keep it that way
+                    json_string_for_config += f'\n{stripped_line}'
 
-            elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line) and not re.match('.*extraSettings ?:', stripped_line):
-                # Ignore commented lines, 'fileKey' entry since it is no longer supported by RTP and also 'extraSettings'
-                json_string_for_config += '\n' + line
+            elif (
+                not stripped_line.startswith("//")
+                and not re.match('.*extraSettings ?:', stripped_line)
+                and (not filekey_found or (filekey_found and not re.match('file ?:', stripped_line)))
+            ):
+                # Ignore commented lines and 'extraSettings'. Also ignore 'file' if 'fileKey' already exists
+                json_string_for_config += f'\n{stripped_line}'
 
         # Finally send pure JSON string for JSON parsing
         config = self._parse_json(json_string_for_config, video_id, js_to_json)
 
-        # Check if file URL is directly a string or is still inside object
-        if isinstance(config['file'], str):
+        if 'fileKey' in config:
+            # 'fileKey' has priority over 'file' on our end
+            file_url = config['fileKey']
+        elif 'file' in config:
+            # 'RTP Arquivos' still uses old regular non-encoded 'file' key
             file_url = config['file']
         else:
-            file_url = config['file']['hls']
+            raise ExtractorError('No valid media source found in page')
 
         ext = determine_ext(file_url)
 
-        if ext == 'm3u8':
-            # Download via m3u8 file
+        if ext == 'mp4':
+            # Due to recent changes, we need to hardcode the URL like this and download it using 'm3u8'
+            file_url = f'https://streaming-vod.rtp.pt/hls{file_url}/index-v1-a1.m3u8'
+
+            formats = self._extract_m3u8_formats(
+                file_url, video_id, 'mp4', 'm3u8_native',
+                m3u8_id='hls')
+        elif ext == 'm3u8':
+            # It can be downloaded without any further changes
             formats = self._extract_m3u8_formats(
                 file_url, video_id, 'mp4', 'm3u8_native',
                 m3u8_id='hls')
-
-            self._sort_formats(formats)
         else:
+            # Need to set basepath
+            file_url = f'https://cdn-ondemand.rtp.pt{file_url}'
             formats = [{
                 'url': file_url,
                 'ext': ext,
             }]
 
-        if config.get('mediaType') == 'audio':
+        if config['mediaType'] == 'audio':
             for f in formats:
                 f['vcodec'] = 'none'
 
+        subtitles = {}
+        if 'vtt' in config:
+            sub_lang, sub_lang_full, sub_url = config['vtt'][0]
+            subtitles.setdefault(sub_lang, []).append({
+                'url': sub_url,
+                'ext': 'vtt',
+            })
+
         return {
             'id': video_id,
             'title': title,
             'formats': formats,
-            'description': self._html_search_meta(['description', 'twitter:description'], webpage),
-            'thumbnail': config.get('poster') or self._og_search_thumbnail(webpage),
+            'subtitles': subtitles,
+            'description': self._html_search_meta(['og:description', 'description', 'twitter:description'], webpage),
+            'thumbnail': config['poster'] or self._og_search_thumbnail(webpage),
         }

From 1058c29c74453e80ad918889a0101894a370c9b7 Mon Sep 17 00:00:00 2001
From: vallovic <vallovic@gmail.com>
Date: Sun, 31 Oct 2021 11:16:05 +0000
Subject: [PATCH 9/9] Make extractor compatible with Python 2.6+

---
 youtube_dl/extractor/rtp.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/youtube_dl/extractor/rtp.py b/youtube_dl/extractor/rtp.py
index ffa915075..1c84e9e8d 100644
--- a/youtube_dl/extractor/rtp.py
+++ b/youtube_dl/extractor/rtp.py
@@ -90,7 +90,7 @@ class RTPIE(InfoExtractor):
         # Check if it's a video split in parts, if so add part number to title
         part = self._html_search_regex(r'section\-parts.*<span.*>(.+?)</span>.*</ul>', webpage, 'part', default=None)
         if part:
-            title = f'{title} {part}'
+            title = '{title} {part}'.format(title=title, part=part)
 
         # Get JS object
         js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
@@ -120,10 +120,10 @@ class RTPIE(InfoExtractor):
                         decoded_url = compat_urllib_parse_unquote(encoded_url)
 
                     # Insert the (relative) decoded URL in JSON
-                    json_string_for_config += f'\nfileKey: "{decoded_url}",'
+                    json_string_for_config += '\nfileKey: "{decoded_url}",'.format(decoded_url=decoded_url)
                 else:
                     # 2) ... or the value URL is not encoded so keep it that way
-                    json_string_for_config += f'\n{stripped_line}'
+                    json_string_for_config += '\n{stripped_line}'.format(stripped_line=stripped_line)
 
             elif (
                 not stripped_line.startswith("//")
@@ -131,7 +131,7 @@ class RTPIE(InfoExtractor):
                 and (not filekey_found or (filekey_found and not re.match('file ?:', stripped_line)))
             ):
                 # Ignore commented lines and 'extraSettings'. Also ignore 'file' if 'fileKey' already exists
-                json_string_for_config += f'\n{stripped_line}'
+                json_string_for_config += '\n{stripped_line}'.format(stripped_line=stripped_line)
 
         # Finally send pure JSON string for JSON parsing
         config = self._parse_json(json_string_for_config, video_id, js_to_json)
@@ -149,7 +149,7 @@ class RTPIE(InfoExtractor):
 
         if ext == 'mp4':
             # Due to recent changes, we need to hardcode the URL like this and download it using 'm3u8'
-            file_url = f'https://streaming-vod.rtp.pt/hls{file_url}/index-v1-a1.m3u8'
+            file_url = 'https://streaming-vod.rtp.pt/hls{file_url}/index-v1-a1.m3u8'.format(file_url=file_url)
 
             formats = self._extract_m3u8_formats(
                 file_url, video_id, 'mp4', 'm3u8_native',
@@ -161,7 +161,7 @@ class RTPIE(InfoExtractor):
                 m3u8_id='hls')
         else:
             # Need to set basepath
-            file_url = f'https://cdn-ondemand.rtp.pt{file_url}'
+            file_url = 'https://cdn-ondemand.rtp.pt{file_url}'.format(file_url=file_url)
             formats = [{
                 'url': file_url,
                 'ext': ext,