[amcnetworks] Add new extractor
[youtube-dl] / youtube_dl / extractor / amcnetworks.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .theplatform import ThePlatformIE
5 from ..utils import (
6     update_url_query,
7     parse_age_limit,
8 )
9
10
11 class AMCNetworksIE(ThePlatformIE):
12     _VALID_URL = r'https?://(?:www\.)?(?:amc|bbcamerica|ifc|wetv)\.com/(?:movies/|shows/[^/]+/(?:full-episodes/)?season-\d+/episode-\d+(?:-(?:[^/]+/)?|/))(?P<id>[^/?#]+)'
13     _TESTS = [{
14         'url': 'http://www.ifc.com/shows/maron/season-04/episode-01/step-1',
15         'md5': '',
16         'info_dict': {
17             'id': 's3MX01Nl4vPH',
18             'ext': 'mp4',
19             'title': 'Step 1',
20             'description': 'In denial about his current situation, Marc is reluctantly convinced by his friends to enter rehab. Starring Marc Maron and Constance Zimmer.',
21             'age_limit': 17,
22             'upload_date': '20160505',
23             'timestamp': 1462468831,
24             'uploader': 'AMCN',
25         },
26         'params': {
27             # m3u8 download
28             'skip_download': True,
29         },
30     }, {
31         'url': 'http://www.bbcamerica.com/shows/the-hunt/full-episodes/season-1/episode-01-the-hardest-challenge',
32         'only_matching': True,
33     }, {
34         'url': 'http://www.amc.com/shows/preacher/full-episodes/season-01/episode-00/pilot',
35         'only_matching': True,
36     }, {
37         'url': 'http://www.wetv.com/shows/million-dollar-matchmaker/season-01/episode-06-the-dumped-dj-and-shallow-hal',
38         'only_matching': True,
39     }, {
40         'url': 'http://www.ifc.com/movies/chaos',
41         'only_matching': True,
42     }]
43
44     def _real_extract(self, url):
45         display_id = self._match_id(url)
46         webpage = self._download_webpage(url, display_id)
47         query = {
48             'mbr': 'true',
49             'manifest': 'm3u',
50         }
51         media_url = self._search_regex(r'window\.platformLinkURL\s*=\s*[\'"]([^\'"]+)', webpage, 'media url')
52         theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
53             r'https?://link.theplatform.com/s/([^?]+)', media_url, 'theplatform_path'), display_id)
54         info = self._parse_theplatform_metadata(theplatform_metadata)
55         video_id = theplatform_metadata['pid']
56         title = theplatform_metadata['title']
57         rating = theplatform_metadata['ratings'][0]['rating']
58         auth_required = self._search_regex(r'window\.authRequired\s*=\s*(true|false);', webpage, 'auth required')
59         if auth_required == 'true':
60             requestor_id = self._search_regex(r'window\.requestor_id\s*=\s*[\'"]([^\'"]+)', webpage, 'requestor id')
61             resource = self._get_mvpd_resource(requestor_id, title, video_id, rating)
62             query['auth'] = self._extract_mvpd_auth(url, video_id, requestor_id, resource)
63         media_url = update_url_query(media_url, query)
64         formats, subtitles = self._extract_theplatform_smil(media_url, video_id)
65         self._sort_formats(formats)
66         info.update({
67             'id': video_id,
68             'subtiles': subtitles,
69             'formats': formats,
70             'age_limit': parse_age_limit(parse_age_limit(rating)),
71         })
72         return info