[expotv] parse m3u8 manifest
[youtube-dl] / youtube_dl / extractor / expotv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     unified_strdate,
9 )
10
11
12 class ExpoTVIE(InfoExtractor):
13     _VALID_URL = r'https?://www\.expotv\.com/videos/[^?#]*/(?P<id>[0-9]+)($|[?#])'
14     _TEST = {
15         'url': 'http://www.expotv.com/videos/reviews/1/24/LinneCardscom/17561',
16         'md5': '2985e6d7a392b2f7a05e0ca350fe41d0',
17         'info_dict': {
18             'id': '17561',
19             'ext': 'mp4',
20             'upload_date': '20060212',
21             'title': 'My Favorite Online Scrapbook Store',
22             'view_count': int,
23             'description': 'You\'ll find most everything you need at this virtual store front.',
24             'uploader': 'Anna T.',
25             'thumbnail': 're:^https?://.*\.jpg$',
26         }
27     }
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         video_id = mobj.group('id')
32
33         webpage = self._download_webpage(url, video_id)
34         player_key = self._search_regex(
35             r'<param name="playerKey" value="([^"]+)"', webpage, 'player key')
36         config = self._download_json(
37                 'http://client.expotv.com/video/config/%s/%s' % (video_id, player_key),
38                 video_id,
39                 note='Downloading video configuration')
40
41         formats = []
42         for fcfg in config['sources']:
43             if fcfg['type'] == 'm3u8':
44                 formats.extend(self._extract_m3u8_formats(fcfg['file'], video_id))
45             else:
46                 formats.append({
47                     'url': fcfg['file'],
48                     'height': int_or_none(fcfg.get('height')),
49                     'format_id': fcfg.get('label'),
50                     'ext': self._search_regex(
51                         r'filename=.*\.([a-z0-9_A-Z]+)&', fcfg['file'],
52                         'file extension', default=None),
53                 })
54         self._sort_formats(formats)
55
56         title = self._og_search_title(webpage)
57         description = self._og_search_description(webpage)
58         thumbnail = config.get('image')
59         view_count = int_or_none(self._search_regex(
60             r'<h5>Plays: ([0-9]+)</h5>', webpage, 'view counts'))
61         uploader = self._search_regex(
62             r'<div class="reviewer">\s*<img alt="([^"]+)"', webpage, 'uploader',
63             fatal=False)
64         upload_date = unified_strdate(self._search_regex(
65             r'<h5>Reviewed on ([0-9/.]+)</h5>', webpage, 'upload date',
66             fatal=False))
67
68         return {
69             'id': video_id,
70             'formats': formats,
71             'title': title,
72             'description': description,
73             'view_count': view_count,
74             'thumbnail': thumbnail,
75             'uploader': uploader,
76             'upload_date': upload_date,
77         }