From 367cc95aa76731aa951e949b325ac9c909af639d Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Thu, 29 Jan 2015 03:49:56 +0800 Subject: [PATCH 1/2] [CtsNews] Add new extractor --- youtube_dl/extractor/__init__.py | 1 + youtube_dl/extractor/ctsnews.py | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 youtube_dl/extractor/ctsnews.py diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 03c56156a..72c5e4973 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -82,6 +82,7 @@ from .crunchyroll import ( CrunchyrollShowPlaylistIE ) from .cspan import CSpanIE +from .ctsnews import CtsNewsIE from .dailymotion import ( DailymotionIE, DailymotionPlaylistIE, diff --git a/youtube_dl/extractor/ctsnews.py b/youtube_dl/extractor/ctsnews.py new file mode 100644 index 000000000..e1d8c814e --- /dev/null +++ b/youtube_dl/extractor/ctsnews.py @@ -0,0 +1,51 @@ +# coding: utf-8 +from __future__ import unicode_literals + +from .common import InfoExtractor +from ..utils import parse_iso8601, ExtractorError + + +class CtsNewsIE(InfoExtractor): + _VALID_URL = r'http://news\.cts\.com\.tw/[a-z]+/[a-z]+/\d+/(?P\d+)\.html' + _TESTS = [{ + 'url': 'http://news.cts.com.tw/cts/international/201309/201309031304098.html', + 'md5': '3aee7e0df7cdff94e43581f54c22619e', + 'info_dict': { + 'id': '201309031304098', + 'ext': 'mp4', + 'title': '韓國31歲童顏男 貌如十多歲小孩', + 'description': 'md5:f183feeba3752b683827aab71adad584', + 'thumbnail': 're:^https?://.*\.jpg$', + 'timestamp': 1378205880, + 'upload_date': '20130903', + } + }] + + def _real_extract(self, url): + news_id = self._match_id(url) + page = self._download_webpage(url, news_id) + + if not self._search_regex(r'(CTSPlayer2)', page, 'CTSPlayer2 identifier', fatal=False): + raise ExtractorError('The news includes no videos!') + + feed_pattern = r'(http://news.cts.com.tw/action/mp4feed.php\?news_id=\d+)' + feed_url = self._html_search_regex(feed_pattern, page, 'feed url') + feed_page = self._download_webpage(feed_url, news_id) + + description = self._html_search_meta('description', page) + title = self._html_search_meta('title', page) + thumbnail = self._html_search_meta('image', page) + + datetime_pattern = r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2})' + datetime_str = self._html_search_regex(datetime_pattern, page, 'date and time') + time = (datetime_str + ':00+08:00').replace('/', '-') + timestamp = parse_iso8601(time, delimiter=' ') + + return { + 'id': news_id, + 'title': title, + 'description': description, + 'url': feed_page, + 'thumbnail': thumbnail, + 'timestamp': timestamp + } From e683a48d0e8650e1d9e8c323cacb468b104ea7bc Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Fri, 30 Jan 2015 05:38:05 +0800 Subject: [PATCH 2/2] [ctsnews] Detect youtube embedde videos --- youtube_dl/extractor/ctsnews.py | 75 +++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/youtube_dl/extractor/ctsnews.py b/youtube_dl/extractor/ctsnews.py index e1d8c814e..35f3756f5 100644 --- a/youtube_dl/extractor/ctsnews.py +++ b/youtube_dl/extractor/ctsnews.py @@ -1,13 +1,27 @@ -# coding: utf-8 +# -*- coding: utf-8 -*- from __future__ import unicode_literals from .common import InfoExtractor +from ..compat import compat_urllib_request from ..utils import parse_iso8601, ExtractorError class CtsNewsIE(InfoExtractor): + # https connection failed (Connection reset) _VALID_URL = r'http://news\.cts\.com\.tw/[a-z]+/[a-z]+/\d+/(?P\d+)\.html' _TESTS = [{ + 'url': 'http://news.cts.com.tw/cts/international/201501/201501291578109.html', + 'md5': 'a9875cb790252b08431186d741beaabe', + 'info_dict': { + 'id': '201501291578109', + 'ext': 'mp4', + 'title': '以色列.真主黨交火 3人死亡', + 'description': 'md5:95e9b295c898b7ff294f09d450178d7d', + 'timestamp': 1422528540, + 'upload_date': '20150129', + } + }, { + # News count not appear on page but still available in database 'url': 'http://news.cts.com.tw/cts/international/201309/201309031304098.html', 'md5': '3aee7e0df7cdff94e43581f54c22619e', 'info_dict': { @@ -19,33 +33,70 @@ class CtsNewsIE(InfoExtractor): 'timestamp': 1378205880, 'upload_date': '20130903', } + }, { + # With Youtube embedded video + 'url': 'http://news.cts.com.tw/cts/money/201501/201501291578003.html', + 'md5': '1d842c771dc94c8c3bca5af2cc1db9c5', + 'add_ie': ['Youtube'], + 'info_dict': { + 'id': 'OVbfO7d0_hQ', + 'ext': 'mp4', + 'title': 'iPhone6熱銷 蘋果財報亮眼', + 'description': 'md5:f395d4f485487bb0f992ed2c4b07aa7d', + 'thumbnail': 're:^https?://.*\.jpg$', + 'upload_date': '20150128', + 'uploader_id': 'TBSCTS', + 'uploader': '中華電視公司', + } }] def _real_extract(self, url): news_id = self._match_id(url) page = self._download_webpage(url, news_id) - if not self._search_regex(r'(CTSPlayer2)', page, 'CTSPlayer2 identifier', fatal=False): - raise ExtractorError('The news includes no videos!') + if self._search_regex(r'(CTSPlayer2)', page, 'CTSPlayer2 identifier', default=None): + feed_url = self._html_search_regex( + r'(http://news\.cts\.com\.tw/action/mp4feed\.php\?news_id=\d+)', + page, 'feed url') + video_url = self._download_webpage(feed_url, news_id) + else: + self.to_screen('Not CTSPlayer video, trying Youtube...') + youtube_url = self._search_regex( + r'src="(//www\.youtube\.com/embed/[^"]+)"', page, 'youtube url', + default=None) + if not youtube_url: + raise ExtractorError('The news includes no videos!', expected=True) - feed_pattern = r'(http://news.cts.com.tw/action/mp4feed.php\?news_id=\d+)' - feed_url = self._html_search_regex(feed_pattern, page, 'feed url') - feed_page = self._download_webpage(feed_url, news_id) + return { + '_type': 'url', + 'url': youtube_url, + 'ie_key': 'Youtube', + } description = self._html_search_meta('description', page) title = self._html_search_meta('title', page) thumbnail = self._html_search_meta('image', page) - datetime_pattern = r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2})' - datetime_str = self._html_search_regex(datetime_pattern, page, 'date and time') - time = (datetime_str + ':00+08:00').replace('/', '-') - timestamp = parse_iso8601(time, delimiter=' ') + datetime_str = self._html_search_regex( + r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2})', page, 'date and time') + # Transform into ISO 8601 format with timezone info + datetime_str = datetime_str.replace('/', '-') + ':00+0800' + timestamp = parse_iso8601(datetime_str, delimiter=' ') + + # Note: the news count may decrease as time goes by + # It should be a bug in CTS website + req = compat_urllib_request.Request( + 'http://news.cts.com.tw/action/news_count.php?callback=cb&news_id=' + news_id) + req.add_header('Referer', url) + newscount_page = self._download_webpage(req, news_id) + news_count = self._search_regex(r'cb\((\d+)\)', newscount_page, 'news count') return { 'id': news_id, + 'url': video_url, 'title': title, 'description': description, - 'url': feed_page, 'thumbnail': thumbnail, - 'timestamp': timestamp + 'timestamp': timestamp, + 'view_count': news_count, }