nextmedia.py 8.8 KB

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