dramafever.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. from .amp import AMPIE
  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(AMPIE):
  19. _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
  20. _NETRC_MACHINE = 'dramafever'
  21. _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
  22. _consumer_secret = None
  23. def _get_consumer_secret(self):
  24. mainjs = self._download_webpage(
  25. 'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js',
  26. None, 'Downloading main.js', fatal=False)
  27. if not mainjs:
  28. return self._CONSUMER_SECRET
  29. return self._search_regex(
  30. r"var\s+cs\s*=\s*'([^']+)'", mainjs,
  31. 'consumer secret', default=self._CONSUMER_SECRET)
  32. def _real_initialize(self):
  33. self._login()
  34. self._consumer_secret = self._get_consumer_secret()
  35. def _login(self):
  36. (username, password) = self._get_login_info()
  37. if username is None:
  38. return
  39. login_form = {
  40. 'username': username,
  41. 'password': password,
  42. }
  43. request = compat_urllib_request.Request(
  44. self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  45. response = self._download_webpage(
  46. request, None, 'Logging in as %s' % username)
  47. if all(logout_pattern not in response
  48. for logout_pattern in ['href="/accounts/logout/"', '>Log out<']):
  49. error = self._html_search_regex(
  50. r'(?s)class="hidden-xs prompt"[^>]*>(.+?)<',
  51. response, 'error message', default=None)
  52. if error:
  53. raise ExtractorError('Unable to login: %s' % error, expected=True)
  54. raise ExtractorError('Unable to log in')
  55. class DramaFeverIE(DramaFeverBaseIE):
  56. IE_NAME = 'dramafever'
  57. _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
  58. _TEST = {
  59. 'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
  60. 'info_dict': {
  61. 'id': '4512.1',
  62. 'ext': 'flv',
  63. 'title': 'Cooking with Shin 4512.1',
  64. 'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
  65. 'thumbnail': 're:^https?://.*\.jpg',
  66. 'timestamp': 1404336058,
  67. 'upload_date': '20140702',
  68. 'duration': 343,
  69. },
  70. 'params': {
  71. # m3u8 download
  72. 'skip_download': True,
  73. },
  74. }
  75. def _real_extract(self, url):
  76. video_id = self._match_id(url).replace('/', '.')
  77. try:
  78. info = self._extract_feed_info('http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id)
  79. except ExtractorError as e:
  80. if isinstance(e.cause, compat_HTTPError):
  81. raise ExtractorError(
  82. 'Currently unavailable in your country.', expected=True)
  83. raise
  84. series_id, episode_number = video_id.split('.')
  85. episode_info = self._download_json(
  86. # We only need a single episode info, so restricting page size to one episode
  87. # and dealing with page number as with episode number
  88. r'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_number=%s&page_size=1'
  89. % (self._consumer_secret, series_id, episode_number),
  90. video_id, 'Downloading episode info JSON', fatal=False)
  91. if episode_info:
  92. value = episode_info.get('value')
  93. if value:
  94. subfile = value[0].get('subfile') or value[0].get('new_subfile')
  95. if subfile and subfile != 'http://www.dramafever.com/st/':
  96. info['subtitiles'].setdefault('English', []).append({
  97. 'ext': 'srt',
  98. 'url': subfile,
  99. })
  100. return info
  101. class DramaFeverSeriesIE(DramaFeverBaseIE):
  102. IE_NAME = 'dramafever:series'
  103. _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
  104. _TESTS = [{
  105. 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
  106. 'info_dict': {
  107. 'id': '4512',
  108. 'title': 'Cooking with Shin',
  109. 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
  110. },
  111. 'playlist_count': 4,
  112. }, {
  113. 'url': 'http://www.dramafever.com/drama/124/IRIS/',
  114. 'info_dict': {
  115. 'id': '124',
  116. 'title': 'IRIS',
  117. 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
  118. },
  119. 'playlist_count': 20,
  120. }]
  121. _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
  122. def _real_extract(self, url):
  123. series_id = self._match_id(url)
  124. series = self._download_json(
  125. 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
  126. % (self._consumer_secret, series_id),
  127. series_id, 'Downloading series JSON')['series'][series_id]
  128. title = clean_html(series['name'])
  129. description = clean_html(series.get('description') or series.get('description_short'))
  130. entries = []
  131. for page_num in itertools.count(1):
  132. episodes = self._download_json(
  133. 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
  134. % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
  135. series_id, 'Downloading episodes JSON page #%d' % page_num)
  136. for episode in episodes.get('value', []):
  137. episode_url = episode.get('episode_url')
  138. if not episode_url:
  139. continue
  140. entries.append(self.url_result(
  141. compat_urlparse.urljoin(url, episode_url),
  142. 'DramaFever', episode.get('guid')))
  143. if page_num == episodes['num_pages']:
  144. break
  145. return self.playlist_result(entries, series_id, title, description)