]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/nextmedia.py
[nextmedia] Support redirected URLs
[yt-dlp.git] / youtube_dl / extractor / nextmedia.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urlparse
6 from ..utils import parse_iso8601
7
8
9 class NextMediaIE(InfoExtractor):
10 IE_DESC = '蘋果日報'
11 _VALID_URL = r'https?://hk\.apple\.nextmedia\.com/[^/]+/[^/]+/(?P<date>\d+)/(?P<id>\d+)'
12 _TESTS = [{
13 'url': 'http://hk.apple.nextmedia.com/realtime/news/20141108/53109199',
14 'md5': 'dff9fad7009311c421176d1ac90bfe4f',
15 'info_dict': {
16 'id': '53109199',
17 'ext': 'mp4',
18 'title': '【佔領金鐘】50外國領事議員撐場 讚學生勇敢香港有希望',
19 'thumbnail': r're:^https?://.*\.jpg$',
20 'description': 'md5:28222b9912b6665a21011b034c70fcc7',
21 'timestamp': 1415456273,
22 'upload_date': '20141108',
23 }
24 }]
25
26 _URL_PATTERN = r'\{ url: \'(.+)\' \}'
27
28 def _real_extract(self, url):
29 news_id = self._match_id(url)
30 page = self._download_webpage(url, news_id)
31 return self._extract_from_nextmedia_page(news_id, url, page)
32
33 def _extract_from_nextmedia_page(self, news_id, url, page):
34 redirection_url = self._search_regex(
35 r'window\.location\.href\s*=\s*([\'"])(?P<url>(?!\1).+)\1',
36 page, 'redirection URL', default=None, group='url')
37 if redirection_url:
38 return self.url_result(compat_urlparse.urljoin(url, redirection_url))
39
40 title = self._fetch_title(page)
41 video_url = self._search_regex(self._URL_PATTERN, page, 'video url')
42
43 attrs = {
44 'id': news_id,
45 'title': title,
46 'url': video_url, # ext can be inferred from url
47 'thumbnail': self._fetch_thumbnail(page),
48 'description': self._fetch_description(page),
49 }
50
51 timestamp = self._fetch_timestamp(page)
52 if timestamp:
53 attrs['timestamp'] = timestamp
54 else:
55 attrs['upload_date'] = self._fetch_upload_date(url)
56
57 return attrs
58
59 def _fetch_title(self, page):
60 return self._og_search_title(page)
61
62 def _fetch_thumbnail(self, page):
63 return self._og_search_thumbnail(page)
64
65 def _fetch_timestamp(self, page):
66 dateCreated = self._search_regex('"dateCreated":"([^"]+)"', page, 'created time')
67 return parse_iso8601(dateCreated)
68
69 def _fetch_upload_date(self, url):
70 return self._search_regex(self._VALID_URL, url, 'upload date', group='date')
71
72 def _fetch_description(self, page):
73 return self._og_search_property('description', page)
74
75
76 class NextMediaActionNewsIE(NextMediaIE):
77 IE_DESC = '蘋果日報 - 動新聞'
78 _VALID_URL = r'https?://hk\.dv\.nextmedia\.com/actionnews/[^/]+/(?P<date>\d+)/(?P<id>\d+)/\d+'
79 _TESTS = [{
80 'url': 'http://hk.dv.nextmedia.com/actionnews/hit/20150121/19009428/20061460',
81 'md5': '05fce8ffeed7a5e00665d4b7cf0f9201',
82 'info_dict': {
83 'id': '19009428',
84 'ext': 'mp4',
85 'title': '【壹週刊】細10年男友偷食 50歲邵美琪再失戀',
86 'thumbnail': r're:^https?://.*\.jpg$',
87 'description': 'md5:cd802fad1f40fd9ea178c1e2af02d659',
88 'timestamp': 1421791200,
89 'upload_date': '20150120',
90 }
91 }]
92
93 def _real_extract(self, url):
94 news_id = self._match_id(url)
95 actionnews_page = self._download_webpage(url, news_id)
96 article_url = self._og_search_url(actionnews_page)
97 article_page = self._download_webpage(article_url, news_id)
98 return self._extract_from_nextmedia_page(news_id, url, article_page)
99
100
101 class AppleDailyIE(NextMediaIE):
102 IE_DESC = '臺灣蘋果日報'
103 _VALID_URL = r'https?://(www|ent)\.appledaily\.com\.tw/[^/]+/[^/]+/[^/]+/(?P<date>\d+)/(?P<id>\d+)(/.*)?'
104 _TESTS = [{
105 'url': 'http://ent.appledaily.com.tw/enews/article/entertainment/20150128/36354694',
106 'md5': 'a843ab23d150977cc55ef94f1e2c1e4d',
107 'info_dict': {
108 'id': '36354694',
109 'ext': 'mp4',
110 'title': '周亭羽走過摩鐵陰霾2男陪吃 九把刀孤寒看醫生',
111 'thumbnail': r're:^https?://.*\.jpg$',
112 'description': 'md5:2acd430e59956dc47cd7f67cb3c003f4',
113 'upload_date': '20150128',
114 }
115 }, {
116 'url': 'http://www.appledaily.com.tw/realtimenews/article/strange/20150128/550549/%E4%B8%8D%E6%BB%BF%E8%A2%AB%E8%B8%A9%E8%85%B3%E3%80%80%E5%B1%B1%E6%9D%B1%E5%85%A9%E5%A4%A7%E5%AA%BD%E4%B8%80%E8%B7%AF%E6%89%93%E4%B8%8B%E8%BB%8A',
117 'md5': '86b4e9132d158279c7883822d94ccc49',
118 'info_dict': {
119 'id': '550549',
120 'ext': 'mp4',
121 'title': '不滿被踩腳 山東兩大媽一路打下車',
122 'thumbnail': r're:^https?://.*\.jpg$',
123 'description': 'md5:175b4260c1d7c085993474217e4ab1b4',
124 'upload_date': '20150128',
125 }
126 }, {
127 'url': 'http://www.appledaily.com.tw/animation/realtimenews/new/20150128/5003671',
128 'md5': '03df296d95dedc2d5886debbb80cb43f',
129 'info_dict': {
130 'id': '5003671',
131 'ext': 'mp4',
132 'title': '20正妹熱舞 《刀龍傳說Online》火辣上市',
133 'thumbnail': r're:^https?://.*\.jpg$',
134 'description': 'md5:23c0aac567dc08c9c16a3161a2c2e3cd',
135 'upload_date': '20150128',
136 },
137 'skip': 'redirect to http://www.appledaily.com.tw/animation/',
138 }, {
139 # No thumbnail
140 'url': 'http://www.appledaily.com.tw/animation/realtimenews/new/20150128/5003673/',
141 'md5': 'b06182cd386ea7bc6115ec7ff0f72aeb',
142 'info_dict': {
143 'id': '5003673',
144 'ext': 'mp4',
145 'title': '半夜尿尿 好像會看到___',
146 'description': 'md5:61d2da7fe117fede148706cdb85ac066',
147 'upload_date': '20150128',
148 },
149 'expected_warnings': [
150 'video thumbnail',
151 ],
152 'skip': 'redirect to http://www.appledaily.com.tw/animation/',
153 }, {
154 'url': 'http://www.appledaily.com.tw/appledaily/article/supplement/20140417/35770334/',
155 'md5': 'eaa20e6b9df418c912d7f5dec2ba734d',
156 'info_dict': {
157 'id': '35770334',
158 'ext': 'mp4',
159 'title': '咖啡占卜測 XU裝熟指數',
160 'thumbnail': r're:^https?://.*\.jpg$',
161 'description': 'md5:7b859991a6a4fedbdf3dd3b66545c748',
162 'upload_date': '20140417',
163 },
164 }, {
165 'url': 'http://www.appledaily.com.tw/actionnews/appledaily/7/20161003/960588/',
166 'only_matching': True,
167 }, {
168 # Redirected from http://ent.appledaily.com.tw/enews/article/entertainment/20150128/36354694
169 'url': 'http://ent.appledaily.com.tw/section/article/headline/20150128/36354694',
170 'only_matching': True,
171 }]
172
173 _URL_PATTERN = r'\{url: \'(.+)\'\}'
174
175 def _fetch_title(self, page):
176 return (self._html_search_regex(r'<h1 id="h1">([^<>]+)</h1>', page, 'news title', default=None) or
177 self._html_search_meta('description', page, 'news title'))
178
179 def _fetch_thumbnail(self, page):
180 return self._html_search_regex(r"setInitialImage\(\'([^']+)'\)", page, 'video thumbnail', fatal=False)
181
182 def _fetch_timestamp(self, page):
183 return None
184
185 def _fetch_description(self, page):
186 return self._html_search_meta('description', page, 'news description')