[presstv] Added extractor PressTV.
[youtube-dl] / youtube_dl / extractor / presstv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import str_to_int
7
8
9 class PressTVIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?presstv\.ir/Video/(?P<y>[0-9]+)/(?P<m>[0-9]+)/(?P<d>[0-9]+)/(?P<id>[0-9]+)/'
11
12     _TEST = {
13         'url': 'http://www.presstv.ir/Video/2015/10/04/431915/Max-Igan-Press-TV-Face-to-Face',
14         'md5': 'e95736ac75088b5f1e5bbb68f248f90d',
15         'info_dict': {
16             'id': '431915',
17             'ext': 'mp4',
18             'title': 'Press TV’s full interview with Max Igan',
19             'upload_date': '20151004',
20             'thumbnail': 'http://217.218.67.233/photo/20151004/d5c333ad-98f9-4bd3-bc3e-a1ad6a192803.jpg',
21             'description': ('Watch Press TV’s full interview with Max Igan, a radio talk show host and political '
22                             'commentator.\nThe interview, conducted on Press TV’s Face '
23                             'to Face program, was aired on October 3, 2015.')
24         }
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29         webpage = self._download_webpage(url, video_id)
30
31         # extract video URL from webpage
32         video_url = self._html_search_regex(r'<input type="hidden" id="inpPlayback" value="([^"]+)" />', webpage,
33                                             'Video URL')
34
35         # build list of available formats
36         # specified in http://www.presstv.ir/Scripts/playback.js
37         base_url = 'http://192.99.219.222:82/presstv'
38         formats = [
39             {
40                 'url': base_url + video_url,
41                 'format': '1080p mp4',
42                 'format_id': '1080p'
43             }, {
44                 'url': base_url + video_url.replace(".mp4", "_low800.mp4"),
45                 'format': '720p mp4',
46                 'format_id': '720p'
47             }, {
48                 'url': base_url + video_url.replace(".mp4", "_low400.mp4"),
49                 'format': '360p mp4',
50                 'format_id': '360p'
51             }, {
52                 'url': base_url + video_url.replace(".mp4", "_low200.mp4"),
53                 'format': '180p mp4',
54                 'format_id': '180p'
55             }
56         ]
57         formats.reverse()
58
59         # extract video metadata
60         title = self._html_search_meta('title', webpage, 'Title', True)
61         title = title.partition(' - ')[2]
62
63         description = self._html_search_regex(r'<div class="media-text nano-content">(.*?)</div>', webpage,
64                                               'Description', flags=re.DOTALL)
65
66         thumbnail = self._html_search_meta('og:image', webpage, 'Thumbnail', True)
67
68         year = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload year', group='y'))
69         month = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload month', group='m'))
70         day = str_to_int(self._search_regex(PressTVIE._VALID_URL, url, 'Upload day', group='d'))
71         upload_date = '%04d%02d%02d' % (year, month, day)
72
73         return {
74             'id': video_id,
75             'title': title,
76             'formats': formats,
77             'thumbnail': thumbnail,
78             'upload_date': upload_date,
79             'description': description
80         }