nextmedia.py 7.3 KB

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