[abcnews] Improve and remove duplicate test (closes #12851)
[youtube-dl] / youtube_dl / extractor / abcnews.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import calendar
5 import re
6 import time
7
8 from .amp import AMPIE
9 from .common import InfoExtractor
10 from ..compat import compat_urlparse
11
12
13 class AbcNewsVideoIE(AMPIE):
14     IE_NAME = 'abcnews:video'
15     _VALID_URL = r'''(?x)
16                     https?://
17                         abcnews\.go\.com/
18                         (?:
19                             [^/]+/video/(?P<display_id>[0-9a-z-]+)-|
20                             video/embed\?.*?\bid=
21                         )
22                         (?P<id>\d+)
23                     '''
24
25     _TESTS = [{
26         'url': 'http://abcnews.go.com/ThisWeek/video/week-exclusive-irans-foreign-minister-zarif-20411932',
27         'info_dict': {
28             'id': '20411932',
29             'ext': 'mp4',
30             'display_id': 'week-exclusive-irans-foreign-minister-zarif',
31             'title': '\'This Week\' Exclusive: Iran\'s Foreign Minister Zarif',
32             'description': 'George Stephanopoulos goes one-on-one with Iranian Foreign Minister Dr. Javad Zarif.',
33             'duration': 180,
34             'thumbnail': r're:^https?://.*\.jpg$',
35         },
36         'params': {
37             # m3u8 download
38             'skip_download': True,
39         },
40     }, {
41         'url': 'http://abcnews.go.com/video/embed?id=46979033',
42         'only_matching': True,
43     }, {
44         'url': 'http://abcnews.go.com/2020/video/2020-husband-stands-teacher-jail-student-affairs-26119478',
45         'only_matching': True,
46     }]
47
48     def _real_extract(self, url):
49         mobj = re.match(self._VALID_URL, url)
50         display_id = mobj.group('display_id')
51         video_id = mobj.group('id')
52         info_dict = self._extract_feed_info(
53             'http://abcnews.go.com/video/itemfeed?id=%s' % video_id)
54         info_dict.update({
55             'id': video_id,
56             'display_id': display_id,
57         })
58         return info_dict
59
60
61 class AbcNewsIE(InfoExtractor):
62     IE_NAME = 'abcnews'
63     _VALID_URL = r'https?://abcnews\.go\.com/(?:[^/]+/)+(?P<display_id>[0-9a-z-]+)/story\?id=(?P<id>\d+)'
64
65     _TESTS = [{
66         'url': 'http://abcnews.go.com/Blotter/News/dramatic-video-rare-death-job-america/story?id=10498713#.UIhwosWHLjY',
67         'info_dict': {
68             'id': '10498713',
69             'ext': 'flv',
70             'display_id': 'dramatic-video-rare-death-job-america',
71             'title': 'Occupational Hazards',
72             'description': 'Nightline investigates the dangers that lurk at various jobs.',
73             'thumbnail': r're:^https?://.*\.jpg$',
74             'upload_date': '20100428',
75             'timestamp': 1272412800,
76         },
77         'add_ie': ['AbcNewsVideo'],
78     }, {
79         'url': 'http://abcnews.go.com/Entertainment/justin-timberlake-performs-stop-feeling-eurovision-2016/story?id=39125818',
80         'info_dict': {
81             'id': '39125818',
82             'ext': 'mp4',
83             'display_id': 'justin-timberlake-performs-stop-feeling-eurovision-2016',
84             'title': 'Justin Timberlake Drops Hints For Secret Single',
85             'description': 'Lara Spencer reports the buzziest stories of the day in "GMA" Pop News.',
86             'upload_date': '20160515',
87             'timestamp': 1463329500,
88         },
89         'params': {
90             # m3u8 download
91             'skip_download': True,
92             # The embedded YouTube video is blocked due to copyright issues
93             'playlist_items': '1',
94         },
95         'add_ie': ['AbcNewsVideo'],
96     }, {
97         'url': 'http://abcnews.go.com/Technology/exclusive-apple-ceo-tim-cook-iphone-cracking-software/story?id=37173343',
98         'only_matching': True,
99     }]
100
101     def _real_extract(self, url):
102         mobj = re.match(self._VALID_URL, url)
103         display_id = mobj.group('display_id')
104         video_id = mobj.group('id')
105
106         webpage = self._download_webpage(url, video_id)
107         video_url = self._search_regex(
108             r'window\.abcnvideo\.url\s*=\s*"([^"]+)"', webpage, 'video URL')
109         full_video_url = compat_urlparse.urljoin(url, video_url)
110
111         youtube_url = self._html_search_regex(
112             r'<iframe[^>]+src="(https://www\.youtube\.com/embed/[^"]+)"',
113             webpage, 'YouTube URL', default=None)
114
115         timestamp = None
116         date_str = self._html_search_regex(
117             r'<span[^>]+class="timestamp">([^<]+)</span>',
118             webpage, 'timestamp', fatal=False)
119         if date_str:
120             tz_offset = 0
121             if date_str.endswith(' ET'):  # Eastern Time
122                 tz_offset = -5
123                 date_str = date_str[:-3]
124             date_formats = ['%b. %d, %Y', '%b %d, %Y, %I:%M %p']
125             for date_format in date_formats:
126                 try:
127                     timestamp = calendar.timegm(time.strptime(date_str.strip(), date_format))
128                 except ValueError:
129                     continue
130             if timestamp is not None:
131                 timestamp -= tz_offset * 3600
132
133         entry = {
134             '_type': 'url_transparent',
135             'ie_key': AbcNewsVideoIE.ie_key(),
136             'url': full_video_url,
137             'id': video_id,
138             'display_id': display_id,
139             'timestamp': timestamp,
140         }
141
142         if youtube_url:
143             entries = [entry, self.url_result(youtube_url, 'Youtube')]
144             return self.playlist_result(entries)
145
146         return entry