From bfe96d7bea7c5227456bf1aecca51907c8f30c51 Mon Sep 17 00:00:00 2001
From: Philip Huppert <philip@zeilen-sprung.de>
Date: Fri, 9 Oct 2015 18:38:11 +0200
Subject: [PATCH 1/3] [presstv] Added extractor PressTV.

Fixes #7060
---
 youtube_dl/extractor/extractors.py |  1 +
 youtube_dl/extractor/presstv.py    | 80 ++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 youtube_dl/extractor/presstv.py

diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py
index de29c7956f..c2fa83918f 100644
--- a/youtube_dl/extractor/extractors.py
+++ b/youtube_dl/extractor/extractors.py
@@ -583,6 +583,7 @@ from .pornhub import (
 from .pornotube import PornotubeIE
 from .pornovoisines import PornoVoisinesIE
 from .pornoxo import PornoXOIE
+from .presstv import PressTVIE
 from .primesharetv import PrimeShareTVIE
 from .promptfile import PromptFileIE
 from .prosiebensat1 import ProSiebenSat1IE
diff --git a/youtube_dl/extractor/presstv.py b/youtube_dl/extractor/presstv.py
new file mode 100644
index 0000000000..724d8b1c43
--- /dev/null
+++ b/youtube_dl/extractor/presstv.py
@@ -0,0 +1,80 @@
+# coding: utf-8
+from __future__ import unicode_literals
+import re
+
+from .common import InfoExtractor
+from ..utils import str_to_int
+
+
+class PressTVIE(InfoExtractor):
+    _VALID_URL = r'https?://(?:www\.)?presstv\.ir/Video/(?P<y>[0-9]+)/(?P<m>[0-9]+)/(?P<d>[0-9]+)/(?P<id>[0-9]+)/'
+
+    _TEST = {
+        'url': 'http://www.presstv.ir/Video/2015/10/04/431915/Max-Igan-Press-TV-Face-to-Face',
+        'md5': 'e95736ac75088b5f1e5bbb68f248f90d',
+        'info_dict': {
+            'id': '431915',
+            'ext': 'mp4',
+            'title': 'Press TV’s full interview with Max Igan',
+            'upload_date': '20151004',
+            'thumbnail': 'http://217.218.67.233/photo/20151004/d5c333ad-98f9-4bd3-bc3e-a1ad6a192803.jpg',
+            'description': ('Watch Press TV’s full interview with Max Igan, a radio talk show host and political '
+                            'commentator.\nThe interview, conducted on Press TV’s Face '
+                            'to Face program, was aired on October 3, 2015.')
+        }
+    }
+
+    def _real_extract(self, url):
+        video_id = self._match_id(url)
+        webpage = self._download_webpage(url, video_id)
+
+        # extract video URL from webpage
+        video_url = self._html_search_regex(r'<input type="hidden" id="inpPlayback" value="([^"]+)" />', webpage,
+                                            'Video URL')
+
+        # build list of available formats
+        # specified in http://www.presstv.ir/Scripts/playback.js
+        base_url = 'http://192.99.219.222:82/presstv'
+        formats = [
+            {
+                'url': base_url + video_url,
+                'format': '1080p mp4',
+                'format_id': '1080p'
+            }, {
+                'url': base_url + video_url.replace(".mp4", "_low800.mp4"),
+                'format': '720p mp4',
+                'format_id': '720p'
+            }, {
+                'url': base_url + video_url.replace(".mp4", "_low400.mp4"),
+                'format': '360p mp4',
+                'format_id': '360p'
+            }, {
+                'url': base_url + video_url.replace(".mp4", "_low200.mp4"),
+                'format': '180p mp4',
+                'format_id': '180p'
+            }
+        ]
+        formats.reverse()
+
+        # extract video metadata
+        title = self._html_search_meta('title', webpage, 'Title', True)
+        title = title.partition(' - ')[2]
+
+        description = self._html_search_regex(r'<div class="media-text nano-content">(.*?)</div>', webpage,
+                                              'Description', flags=re.DOTALL)
+
+        thumbnail = self._html_search_meta('og:image', webpage, 'Thumbnail', True)
+
+        year = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload year', group='y'))
+        month = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload month', group='m'))
+        day = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload day', group='d'))
+        upload_date = '%04d%02d%02d' % (year, month, day)
+
+        return {
+            'id': video_id,
+            'title': title,
+            'formats': formats,
+            'thumbnail': thumbnail,
+            'upload_date': upload_date,
+            'description': description
+        }

From 95153a960d098d75e6100e38e77fdaa32f5267a2 Mon Sep 17 00:00:00 2001
From: Philip Huppert <philip@zeilen-sprung.de>
Date: Sat, 9 Apr 2016 16:14:05 +0200
Subject: [PATCH 2/3] [presstv] updated extractor and tests to work with
 current PressTV website

---
 youtube_dl/extractor/presstv.py | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/youtube_dl/extractor/presstv.py b/youtube_dl/extractor/presstv.py
index 724d8b1c43..9af6780c11 100644
--- a/youtube_dl/extractor/presstv.py
+++ b/youtube_dl/extractor/presstv.py
@@ -7,20 +7,20 @@ from ..utils import str_to_int
 
 
 class PressTVIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?presstv\.ir/Video/(?P<y>[0-9]+)/(?P<m>[0-9]+)/(?P<d>[0-9]+)/(?P<id>[0-9]+)/'
+    _VALID_URL = r'https?://(?:www\.)?presstv\.ir/[^/]+/(?P<y>[0-9]+)/(?P<m>[0-9]+)/(?P<d>[0-9]+)/(?P<id>[0-9]+)/'
 
     _TEST = {
-        'url': 'http://www.presstv.ir/Video/2015/10/04/431915/Max-Igan-Press-TV-Face-to-Face',
-        'md5': 'e95736ac75088b5f1e5bbb68f248f90d',
+        'url': 'http://www.presstv.ir/Detail/2016/04/09/459911/Australian-sewerage-treatment-facility-/',
+        'md5': '5d7e3195a447cb13e9267e931d8dd5a5',
         'info_dict': {
-            'id': '431915',
+            'id': '459911',
             'ext': 'mp4',
-            'title': 'Press TV’s full interview with Max Igan',
-            'upload_date': '20151004',
-            'thumbnail': 'http://217.218.67.233/photo/20151004/d5c333ad-98f9-4bd3-bc3e-a1ad6a192803.jpg',
-            'description': ('Watch Press TV’s full interview with Max Igan, a radio talk show host and political '
-                            'commentator.\nThe interview, conducted on Press TV’s Face '
-                            'to Face program, was aired on October 3, 2015.')
+            'title': 'Organic mattresses used to clean waste water',
+            'upload_date': '20160409',
+            'thumbnail': 'http://media.presstv.com/photo/20160409/41719129-76fa-4372-a09d-bf348278eb5d.jpg',
+            'description': ('A trial program at an Australian sewerage treatment facility hopes to change '
+                            'the way waste water is treated by using plant mattresses to reduce chemical '
+                            'and electricity use.')
         }
     }
 
@@ -58,12 +58,10 @@ class PressTVIE(InfoExtractor):
 
         # extract video metadata
         title = self._html_search_meta('title', webpage, 'Title', True)
-        title = title.partition(' - ')[2]
-
-        description = self._html_search_regex(r'<div class="media-text nano-content">(.*?)</div>', webpage,
-                                              'Description', flags=re.DOTALL)
+        title = title.partition('-')[2].strip()
 
         thumbnail = self._html_search_meta('og:image', webpage, 'Thumbnail', True)
+        description = self._html_search_meta('og:description', webpage, 'Description', True)
 
         year = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload year', group='y'))
         month = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload month', group='m'))

From de728757ad7218ce175649ec0d3f0b5723f2c580 Mon Sep 17 00:00:00 2001
From: Philip Huppert <philip@zeilen-sprung.de>
Date: Sun, 10 Apr 2016 16:36:44 +0200
Subject: [PATCH 3/3] [presstv] Refactored extractor.

---
 youtube_dl/extractor/presstv.py | 52 +++++++++++++++------------------
 1 file changed, 23 insertions(+), 29 deletions(-)

diff --git a/youtube_dl/extractor/presstv.py b/youtube_dl/extractor/presstv.py
index 9af6780c11..755e325285 100644
--- a/youtube_dl/extractor/presstv.py
+++ b/youtube_dl/extractor/presstv.py
@@ -17,10 +17,8 @@ class PressTVIE(InfoExtractor):
             'ext': 'mp4',
             'title': 'Organic mattresses used to clean waste water',
             'upload_date': '20160409',
-            'thumbnail': 'http://media.presstv.com/photo/20160409/41719129-76fa-4372-a09d-bf348278eb5d.jpg',
-            'description': ('A trial program at an Australian sewerage treatment facility hopes to change '
-                            'the way waste water is treated by using plant mattresses to reduce chemical '
-                            'and electricity use.')
+            'thumbnail': 're:^https?://.*\.jpg',
+            'description': 'md5:20002e654bbafb6908395a5c0cfcd125'
         }
     }
 
@@ -35,38 +33,34 @@ class PressTVIE(InfoExtractor):
         # build list of available formats
         # specified in http://www.presstv.ir/Scripts/playback.js
         base_url = 'http://192.99.219.222:82/presstv'
-        formats = [
-            {
-                'url': base_url + video_url,
-                'format': '1080p mp4',
-                'format_id': '1080p'
-            }, {
-                'url': base_url + video_url.replace(".mp4", "_low800.mp4"),
-                'format': '720p mp4',
-                'format_id': '720p'
-            }, {
-                'url': base_url + video_url.replace(".mp4", "_low400.mp4"),
-                'format': '360p mp4',
-                'format_id': '360p'
-            }, {
-                'url': base_url + video_url.replace(".mp4", "_low200.mp4"),
-                'format': '180p mp4',
-                'format_id': '180p'
-            }
+        _formats = [
+            ("180p", "_low200.mp4"),
+            ("360p", "_low400.mp4"),
+            ("720p", "_low800.mp4"),
+            ("1080p", ".mp4")
         ]
-        formats.reverse()
+
+        formats = []
+        for fmt in _formats:
+            format_id, extension = fmt
+            formats.append({
+                'url': base_url + video_url[:-4] + extension,
+                'format_id': format_id
+            })
 
         # extract video metadata
         title = self._html_search_meta('title', webpage, 'Title', True)
         title = title.partition('-')[2].strip()
 
-        thumbnail = self._html_search_meta('og:image', webpage, 'Thumbnail', True)
-        description = self._html_search_meta('og:description', webpage, 'Description', True)
+        thumbnail = self._og_search_thumbnail(webpage)
+        description = self._og_search_description(webpage)
 
-        year = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload year', group='y'))
-        month = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload month', group='m'))
-        day = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload day', group='d'))
-        upload_date = '%04d%02d%02d' % (year, month, day)
+        match = re.match(PressTVIE._VALID_URL, url)
+        upload_date = '%04d%02d%02d' % (
+            str_to_int(match.group('y')),
+            str_to_int(match.group('m')),
+            str_to_int(match.group('d'))
+        )
 
         return {
             'id': video_id,