You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_request
  5. from ..utils import parse_iso8601, ExtractorError
  6. class CtsNewsIE(InfoExtractor):
  7. # https connection failed (Connection reset)
  8. _VALID_URL = r'http://news\.cts\.com\.tw/[a-z]+/[a-z]+/\d+/(?P<id>\d+)\.html'
  9. _TESTS = [{
  10. 'url': 'http://news.cts.com.tw/cts/international/201501/201501291578109.html',
  11. 'md5': 'a9875cb790252b08431186d741beaabe',
  12. 'info_dict': {
  13. 'id': '201501291578109',
  14. 'ext': 'mp4',
  15. 'title': '以色列.真主黨交火 3人死亡',
  16. 'description': 'md5:95e9b295c898b7ff294f09d450178d7d',
  17. 'timestamp': 1422528540,
  18. 'upload_date': '20150129',
  19. }
  20. }, {
  21. # News count not appear on page but still available in database
  22. 'url': 'http://news.cts.com.tw/cts/international/201309/201309031304098.html',
  23. 'md5': '3aee7e0df7cdff94e43581f54c22619e',
  24. 'info_dict': {
  25. 'id': '201309031304098',
  26. 'ext': 'mp4',
  27. 'title': '韓國31歲童顏男 貌如十多歲小孩',
  28. 'description': 'md5:f183feeba3752b683827aab71adad584',
  29. 'thumbnail': 're:^https?://.*\.jpg$',
  30. 'timestamp': 1378205880,
  31. 'upload_date': '20130903',
  32. }
  33. }, {
  34. # With Youtube embedded video
  35. 'url': 'http://news.cts.com.tw/cts/money/201501/201501291578003.html',
  36. 'md5': '1d842c771dc94c8c3bca5af2cc1db9c5',
  37. 'add_ie': ['Youtube'],
  38. 'info_dict': {
  39. 'id': 'OVbfO7d0_hQ',
  40. 'ext': 'mp4',
  41. 'title': 'iPhone6熱銷 蘋果財報亮眼',
  42. 'description': 'md5:f395d4f485487bb0f992ed2c4b07aa7d',
  43. 'thumbnail': 're:^https?://.*\.jpg$',
  44. 'upload_date': '20150128',
  45. 'uploader_id': 'TBSCTS',
  46. 'uploader': '中華電視公司',
  47. }
  48. }]
  49. def _real_extract(self, url):
  50. news_id = self._match_id(url)
  51. page = self._download_webpage(url, news_id)
  52. if self._search_regex(r'(CTSPlayer2)', page, 'CTSPlayer2 identifier', default=None):
  53. feed_url = self._html_search_regex(
  54. r'(http://news\.cts\.com\.tw/action/mp4feed\.php\?news_id=\d+)',
  55. page, 'feed url')
  56. video_url = self._download_webpage(feed_url, news_id)
  57. else:
  58. self.to_screen('Not CTSPlayer video, trying Youtube...')
  59. youtube_url = self._search_regex(
  60. r'src="(//www\.youtube\.com/embed/[^"]+)"', page, 'youtube url',
  61. default=None)
  62. if not youtube_url:
  63. raise ExtractorError('The news includes no videos!', expected=True)
  64. return {
  65. '_type': 'url',
  66. 'url': youtube_url,
  67. 'ie_key': 'Youtube',
  68. }
  69. description = self._html_search_meta('description', page)
  70. title = self._html_search_meta('title', page)
  71. thumbnail = self._html_search_meta('image', page)
  72. datetime_str = self._html_search_regex(
  73. r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2})', page, 'date and time')
  74. # Transform into ISO 8601 format with timezone info
  75. datetime_str = datetime_str.replace('/', '-') + ':00+0800'
  76. timestamp = parse_iso8601(datetime_str, delimiter=' ')
  77. # Note: the news count may decrease as time goes by
  78. # It should be a bug in CTS website
  79. req = compat_urllib_request.Request(
  80. 'http://news.cts.com.tw/action/news_count.php?callback=cb&news_id=' + news_id)
  81. req.add_header('Referer', url)
  82. newscount_page = self._download_webpage(req, news_id)
  83. news_count = self._search_regex(r'cb\((\d+)\)', newscount_page, 'news count')
  84. return {
  85. 'id': news_id,
  86. 'url': video_url,
  87. 'title': title,
  88. 'description': description,
  89. 'thumbnail': thumbnail,
  90. 'timestamp': timestamp,
  91. 'view_count': news_count,
  92. }