dramafever.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. class DramaFeverIE(InfoExtractor):
  6. IE_NAME = 'dramafever'
  7. _VALID_URL = r'^https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+/[0-9]+)/'
  8. _TESTS = [{
  9. 'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
  10. 'info_dict': {
  11. 'id': '4512.1',
  12. 'ext': 'flv',
  13. 'title': 'Cooking with Shin 4512.1',
  14. 'upload_date': '20140702',
  15. 'description': 'Served at all special occasions and featured in the hit drama Heirs, Shin cooks Red Bean Rice.',
  16. }
  17. }]
  18. def _real_extract(self, url):
  19. video_id = self._match_id(url).replace("/", ".")
  20. consumer_secret = self._get_consumer_secret(video_id)
  21. ep_json = self._download_json(
  22. "http://www.dramafever.com/amp/episode/feed.json?guid=%s" % video_id,
  23. video_id, note='Downloading episode metadata',
  24. errnote="Video may not be available for your location")["channel"]["item"]
  25. title = ep_json["media-group"]["media-title"]
  26. description = ep_json["media-group"]["media-description"]
  27. thumbnail = ep_json["media-group"]["media-thumbnail"]["@attributes"]["url"]
  28. duration = int(ep_json["media-group"]["media-content"][0]["@attributes"]["duration"])
  29. mobj = re.match(r"([0-9]{4})-([0-9]{2})-([0-9]{2})", ep_json["pubDate"])
  30. upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3) if mobj is not None else None
  31. formats = []
  32. for vid_format in ep_json["media-group"]["media-content"]:
  33. src = vid_format["@attributes"]["url"]
  34. if '.f4m' in src:
  35. formats.extend(self._extract_f4m_formats(src, video_id))
  36. self._sort_formats(formats)
  37. video_subtitles = self.extract_subtitles(video_id, consumer_secret)
  38. return {
  39. 'id': video_id,
  40. 'title': title,
  41. 'description': description,
  42. 'thumbnail': thumbnail,
  43. 'upload_date': upload_date,
  44. 'duration': duration,
  45. 'formats': formats,
  46. 'subtitles': video_subtitles,
  47. }
  48. def _get_consumer_secret(self, video_id):
  49. df_js = self._download_webpage(
  50. "http://www.dramafever.com/static/126960d/v2/js/plugins/jquery.threadedcomments.js", video_id)
  51. return self._search_regex(r"'cs': '([0-9a-zA-Z]+)'", df_js, "cs")
  52. def _get_episodes(self, series_id, consumer_secret, episode_filter=None):
  53. _PAGE_SIZE = 60
  54. curr_page = 1
  55. max_pages = curr_page + 1
  56. results = []
  57. while max_pages >= curr_page:
  58. page_url = "http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d" % \
  59. (consumer_secret, series_id, _PAGE_SIZE, curr_page)
  60. series = self._download_json(
  61. page_url, series_id, note="Downloading series json page #%d" % curr_page)
  62. max_pages = series['num_pages']
  63. results.extend([ep for ep in series['value'] if episode_filter is None or episode_filter(ep)])
  64. curr_page += 1
  65. return results
  66. def _get_subtitles(self, video_id, consumer_secret):
  67. def match_episode(ep):
  68. return ep['guid'] == video_id
  69. res = None
  70. info = self._get_episodes(
  71. video_id.split(".")[0], consumer_secret, episode_filter=match_episode)
  72. if len(info) == 1 and info[0]['subfile'] != '':
  73. res = {'en': [{'url': info[0]['subfile'], 'ext': 'srt'}]}
  74. return res
  75. class DramaFeverSeriesIE(DramaFeverIE):
  76. IE_NAME = 'dramafever:series'
  77. _VALID_URL = r'^https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+)/\d*[a-zA-Z_][a-zA-Z0-9_]*/'
  78. _TESTS = [{
  79. 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
  80. 'info_dict': {
  81. 'id': '4512',
  82. 'title': 'Cooking with Shin',
  83. 'description': 'Professional chef and cooking instructor Shin Kim takes some of the delicious dishes featured in your favorite dramas and shows you how to make them right at home.',
  84. },
  85. 'playlist_count': 4,
  86. }, {
  87. 'url': 'http://www.dramafever.com/drama/124/IRIS/',
  88. 'info_dict': {
  89. 'id': '124',
  90. 'title': 'IRIS',
  91. 'description': 'Lee Byung Hun and Kim Tae Hee star in this powerhouse drama and ratings megahit of action, intrigue and romance.',
  92. },
  93. 'playlist_count': 20,
  94. }]
  95. def _real_extract(self, url):
  96. series_id = self._match_id(url)
  97. consumer_secret = self._get_consumer_secret(series_id)
  98. series_json = self._download_json(
  99. "http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s" % (consumer_secret, series_id),
  100. series_id, note='Downloading series metadata')["series"][series_id]
  101. title = series_json["name"]
  102. description = series_json["description_short"]
  103. episodes = self._get_episodes(series_id, consumer_secret)
  104. entries = []
  105. for ep in episodes:
  106. entries.append(self.url_result(
  107. 'http://www.dramafever.com%s' % ep['episode_url'], 'DramaFever', ep['guid']))
  108. return self.playlist_result(entries, series_id, title, description)