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.

160 lines
5.7 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. clean_html,
  12. determine_ext,
  13. int_or_none,
  14. parse_iso8601,
  15. )
  16. class DramaFeverIE(InfoExtractor):
  17. IE_NAME = 'dramafever'
  18. _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
  19. _TEST = {
  20. 'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
  21. 'info_dict': {
  22. 'id': '4512.1',
  23. 'ext': 'flv',
  24. 'title': 'Cooking with Shin 4512.1',
  25. 'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
  26. 'thumbnail': 're:^https?://.*\.jpg',
  27. 'timestamp': 1404336058,
  28. 'upload_date': '20140702',
  29. 'duration': 343,
  30. }
  31. }
  32. def _real_extract(self, url):
  33. video_id = self._match_id(url).replace('/', '.')
  34. try:
  35. feed = self._download_json(
  36. 'http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id,
  37. video_id, 'Downloading episode JSON')['channel']['item']
  38. except ExtractorError as e:
  39. if isinstance(e.cause, compat_HTTPError):
  40. raise ExtractorError(
  41. 'Currently unavailable in your country.', expected=True)
  42. raise
  43. media_group = feed.get('media-group', {})
  44. formats = []
  45. for media_content in media_group['media-content']:
  46. src = media_content.get('@attributes', {}).get('url')
  47. if not src:
  48. continue
  49. ext = determine_ext(src)
  50. if ext == 'f4m':
  51. formats.extend(self._extract_f4m_formats(
  52. src, video_id, f4m_id='hds'))
  53. elif ext == 'm3u8':
  54. formats.extend(self._extract_m3u8_formats(
  55. src, video_id, 'mp4', m3u8_id='hls'))
  56. else:
  57. formats.append({
  58. 'url': src,
  59. })
  60. self._sort_formats(formats)
  61. title = media_group.get('media-title')
  62. description = media_group.get('media-description')
  63. duration = int_or_none(media_group['media-content'][0].get('@attributes', {}).get('duration'))
  64. thumbnail = self._proto_relative_url(
  65. media_group.get('media-thumbnail', {}).get('@attributes', {}).get('url'))
  66. timestamp = parse_iso8601(feed.get('pubDate'), ' ')
  67. subtitles = {}
  68. for media_subtitle in media_group.get('media-subTitle', []):
  69. lang = media_subtitle.get('@attributes', {}).get('lang')
  70. href = media_subtitle.get('@attributes', {}).get('href')
  71. if not lang or not href:
  72. continue
  73. subtitles[lang] = [{
  74. 'ext': 'ttml',
  75. 'url': href,
  76. }]
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': description,
  81. 'thumbnail': thumbnail,
  82. 'timestamp': timestamp,
  83. 'duration': duration,
  84. 'formats': formats,
  85. 'subtitles': subtitles,
  86. }
  87. class DramaFeverSeriesIE(InfoExtractor):
  88. IE_NAME = 'dramafever:series'
  89. _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
  90. _TESTS = [{
  91. 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
  92. 'info_dict': {
  93. 'id': '4512',
  94. 'title': 'Cooking with Shin',
  95. 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
  96. },
  97. 'playlist_count': 4,
  98. }, {
  99. 'url': 'http://www.dramafever.com/drama/124/IRIS/',
  100. 'info_dict': {
  101. 'id': '124',
  102. 'title': 'IRIS',
  103. 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
  104. },
  105. 'playlist_count': 20,
  106. }]
  107. _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
  108. _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
  109. def _get_consumer_secret(self, video_id):
  110. mainjs = self._download_webpage(
  111. 'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js',
  112. video_id, 'Downloading main.js', fatal=False)
  113. if not mainjs:
  114. return self._CONSUMER_SECRET
  115. return self._search_regex(
  116. r"var\s+cs\s*=\s*'([^']+)'", mainjs,
  117. 'consumer secret', default=self._CONSUMER_SECRET)
  118. def _real_extract(self, url):
  119. series_id = self._match_id(url)
  120. consumer_secret = self._get_consumer_secret(series_id)
  121. series = self._download_json(
  122. 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
  123. % (consumer_secret, series_id),
  124. series_id, 'Downloading series JSON')['series'][series_id]
  125. title = clean_html(series['name'])
  126. description = clean_html(series.get('description') or series.get('description_short'))
  127. entries = []
  128. for page_num in itertools.count(1):
  129. episodes = self._download_json(
  130. 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
  131. % (consumer_secret, series_id, self._PAGE_SIZE, page_num),
  132. series_id, 'Downloading episodes JSON page #%d' % page_num)
  133. for episode in episodes.get('value', []):
  134. entries.append(self.url_result(
  135. compat_urlparse.urljoin(url, episode['episode_url']),
  136. 'DramaFever', episode.get('guid')))
  137. if page_num == episodes['num_pages']:
  138. break
  139. return self.playlist_result(entries, series_id, title, description)