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.

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