Fix "invalid escape sequences" error on Python 3.6
[youtube-dl] / youtube_dl / extractor / sendtonews.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .jwplatform import JWPlatformBaseIE
7 from ..utils import (
8     float_or_none,
9     parse_iso8601,
10     update_url_query,
11 )
12
13
14 class SendtoNewsIE(JWPlatformBaseIE):
15     _VALID_URL = r'https?://embed\.sendtonews\.com/player2/embedplayer\.php\?.*\bSC=(?P<id>[0-9A-Za-z-]+)'
16
17     _TEST = {
18         # From http://cleveland.cbslocal.com/2016/05/16/indians-score-season-high-15-runs-in-blowout-win-over-reds-rapid-reaction/
19         'url': 'http://embed.sendtonews.com/player2/embedplayer.php?SC=GxfCe0Zo7D-175909-5588&type=single&autoplay=on&sound=YES',
20         'info_dict': {
21             'id': 'GxfCe0Zo7D-175909-5588'
22         },
23         'playlist_count': 9,
24         # test the first video only to prevent lengthy tests
25         'playlist': [{
26             'info_dict': {
27                 'id': '198180',
28                 'ext': 'mp4',
29                 'title': 'Recap: CLE 5, LAA 4',
30                 'description': '8/14/16: Naquin, Almonte lead Indians in 5-4 win',
31                 'duration': 57.343,
32                 'thumbnail': r're:https?://.*\.jpg$',
33                 'upload_date': '20160815',
34                 'timestamp': 1471221961,
35             },
36         }],
37         'params': {
38             # m3u8 download
39             'skip_download': True,
40         },
41     }
42
43     _URL_TEMPLATE = '//embed.sendtonews.com/player2/embedplayer.php?SC=%s'
44
45     @classmethod
46     def _extract_url(cls, webpage):
47         mobj = re.search(r'''(?x)<script[^>]+src=([\'"])
48             (?:https?:)?//embed\.sendtonews\.com/player/responsiveembed\.php\?
49                 .*\bSC=(?P<SC>[0-9a-zA-Z-]+).*
50             \1>''', webpage)
51         if mobj:
52             sc = mobj.group('SC')
53             return cls._URL_TEMPLATE % sc
54
55     def _real_extract(self, url):
56         playlist_id = self._match_id(url)
57
58         data_url = update_url_query(
59             url.replace('embedplayer.php', 'data_read.php'),
60             {'cmd': 'loadInitial'})
61         playlist_data = self._download_json(data_url, playlist_id)
62
63         entries = []
64         for video in playlist_data['playlistData'][0]:
65             info_dict = self._parse_jwplayer_data(
66                 video['jwconfiguration'],
67                 require_title=False, rtmp_params={'no_resume': True})
68
69             thumbnails = []
70             if video.get('thumbnailUrl'):
71                 thumbnails.append({
72                     'id': 'normal',
73                     'url': video['thumbnailUrl'],
74                 })
75             if video.get('smThumbnailUrl'):
76                 thumbnails.append({
77                     'id': 'small',
78                     'url': video['smThumbnailUrl'],
79                 })
80             info_dict.update({
81                 'title': video['S_headLine'],
82                 'description': video.get('S_fullStory'),
83                 'thumbnails': thumbnails,
84                 'duration': float_or_none(video.get('SM_length')),
85                 'timestamp': parse_iso8601(video.get('S_sysDate'), delimiter=' '),
86             })
87             entries.append(info_dict)
88
89         return self.playlist_result(entries, playlist_id)