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.

88 lines
3.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import os.path
  5. from .common import InfoExtractor
  6. from ..compat import compat_urlparse
  7. from ..utils import (
  8. url_basename,
  9. remove_start,
  10. )
  11. class DemocracynowIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?democracynow.org/(?P<id>[^\?]*)'
  13. IE_NAME = 'democracynow'
  14. _TESTS = [{
  15. 'url': 'http://www.democracynow.org/shows/2015/7/3',
  16. 'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
  17. 'info_dict': {
  18. 'id': '2015-0703-001',
  19. 'ext': 'mp4',
  20. 'title': 'July 03, 2015 - Democracy Now!',
  21. 'description': 'A daily independent global news hour with Amy Goodman & Juan González "What to the Slave is 4th of July?": James Earl Jones Reads Frederick Douglass\u2019 Historic Speech : "This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag : "We Shall Overcome": Remembering Folk Icon, Activist Pete Seeger in His Own Words & Songs',
  22. },
  23. }, {
  24. 'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
  25. 'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
  26. 'info_dict': {
  27. 'id': '2015-0703-001',
  28. 'ext': 'mp4',
  29. 'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
  30. 'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
  31. },
  32. }]
  33. def _real_extract(self, url):
  34. display_id = self._match_id(url)
  35. webpage = self._download_webpage(url, display_id)
  36. description = self._og_search_description(webpage)
  37. json_data = self._parse_json(self._search_regex(
  38. r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json'),
  39. display_id)
  40. video_id = None
  41. formats = []
  42. default_lang = 'en'
  43. subtitles = {}
  44. def add_subtitle_item(lang, info_dict):
  45. if lang not in subtitles:
  46. subtitles[lang] = []
  47. subtitles[lang].append(info_dict)
  48. # chapter_file are not subtitles
  49. if 'caption_file' in json_data:
  50. add_subtitle_item(default_lang, {
  51. 'url': compat_urlparse.urljoin(url, json_data['caption_file']),
  52. })
  53. for subtitle_item in json_data.get('captions', []):
  54. lang = subtitle_item.get('language', '').lower() or default_lang
  55. add_subtitle_item(lang, {
  56. 'url': compat_urlparse.urljoin(url, subtitle_item['url']),
  57. })
  58. for key in ('file', 'audio', 'video'):
  59. media_url = json_data.get(key, '')
  60. if not media_url:
  61. continue
  62. media_url = re.sub(r'\?.*', '', compat_urlparse.urljoin(url, media_url))
  63. video_id = video_id or remove_start(os.path.splitext(url_basename(media_url))[0], 'dn')
  64. formats.append({
  65. 'url': media_url,
  66. })
  67. self._sort_formats(formats)
  68. return {
  69. 'id': video_id or display_id,
  70. 'title': json_data['title'],
  71. 'description': description,
  72. 'subtitles': subtitles,
  73. 'formats': formats,
  74. }