dramafever.py 6.0 KB

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