Merge branch 'presstv' of https://github.com/Phaeilo/youtube-dl into Phaeilo-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/[^/]+/(?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/Detail/2016/04/09/459911/Australian-sewerage-treatment-facility-/',
14         'md5': '5d7e3195a447cb13e9267e931d8dd5a5',
15         'info_dict': {
16             'id': '459911',
17             'ext': 'mp4',
18             'title': 'Organic mattresses used to clean waste water',
19             'upload_date': '20160409',
20             'thumbnail': 're:^https?://.*\.jpg',
21             'description': 'md5:20002e654bbafb6908395a5c0cfcd125'
22         }
23     }
24
25     def _real_extract(self, url):
26         video_id = self._match_id(url)
27         webpage = self._download_webpage(url, video_id)
28
29         # extract video URL from webpage
30         video_url = self._html_search_regex(r'<input type="hidden" id="inpPlayback" value="([^"]+)" />', webpage,
31                                             'Video URL')
32
33         # build list of available formats
34         # specified in http://www.presstv.ir/Scripts/playback.js
35         base_url = 'http://192.99.219.222:82/presstv'
36         _formats = [
37             ("180p", "_low200.mp4"),
38             ("360p", "_low400.mp4"),
39             ("720p", "_low800.mp4"),
40             ("1080p", ".mp4")
41         ]
42
43         formats = []
44         for fmt in _formats:
45             format_id, extension = fmt
46             formats.append({
47                 'url': base_url + video_url[:-4] + extension,
48                 'format_id': format_id
49             })
50
51         # extract video metadata
52         title = self._html_search_meta('title', webpage, 'Title', True)
53         title = title.partition('-')[2].strip()
54
55         thumbnail = self._og_search_thumbnail(webpage)
56         description = self._og_search_description(webpage)
57
58         match = re.match(PressTVIE._VALID_URL, url)
59         upload_date = '%04d%02d%02d' % (
60             str_to_int(match.group('y')),
61             str_to_int(match.group('m')),
62             str_to_int(match.group('d'))
63         )
64
65         return {
66             'id': video_id,
67             'title': title,
68             'formats': formats,
69             'thumbnail': thumbnail,
70             'upload_date': upload_date,
71             'description': description
72         }