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.

134 lines
4.7 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import random
  5. import string
  6. from .common import InfoExtractor
  7. from ..utils import find_xpath_attr
  8. class HowStuffWorksIE(InfoExtractor):
  9. _VALID_URL = r'https?://[\da-z-]+\.howstuffworks\.com/(?:[^/]+/)*\d+-(?P<id>.+?)-video\.htm'
  10. _TESTS = [
  11. {
  12. 'url': 'http://adventure.howstuffworks.com/5266-cool-jobs-iditarod-musher-video.htm',
  13. 'info_dict': {
  14. 'id': '450221',
  15. 'display_id': 'cool-jobs-iditarod-musher',
  16. 'ext': 'flv',
  17. 'title': 'Cool Jobs - Iditarod Musher',
  18. 'description': 'md5:82bb58438a88027b8186a1fccb365f90',
  19. 'thumbnail': 're:^https?://.*\.jpg$',
  20. },
  21. 'params': {
  22. # md5 is not consistent
  23. 'skip_download': True
  24. }
  25. },
  26. {
  27. 'url': 'http://adventure.howstuffworks.com/39516-deadliest-catch-jakes-farewell-pots-video.htm',
  28. 'info_dict': {
  29. 'id': '553470',
  30. 'display_id': 'deadliest-catch-jakes-farewell-pots',
  31. 'ext': 'mp4',
  32. 'title': 'Deadliest Catch: Jake\'s Farewell Pots',
  33. 'description': 'md5:9632c346d5e43ee238028c9cefd8dbbc',
  34. 'thumbnail': 're:^https?://.*\.jpg$',
  35. },
  36. 'params': {
  37. # md5 is not consistent
  38. 'skip_download': True
  39. }
  40. },
  41. {
  42. 'url': 'http://entertainment.howstuffworks.com/arts/2706-sword-swallowing-1-by-dan-meyer-video.htm',
  43. 'info_dict': {
  44. 'id': '440011',
  45. 'display_id': 'sword-swallowing-1-by-dan-meyer',
  46. 'ext': 'flv',
  47. 'title': 'Sword Swallowing #1 by Dan Meyer',
  48. 'description': 'md5:b2409e88172913e2e7d3d1159b0ef735',
  49. 'thumbnail': 're:^https?://.*\.jpg$',
  50. },
  51. 'params': {
  52. # md5 is not consistent
  53. 'skip_download': True
  54. }
  55. },
  56. ]
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. display_id = mobj.group('id')
  60. webpage = self._download_webpage(url, display_id)
  61. content_id = self._search_regex(r'var siteSectionId="(\d+)";', webpage, 'content id')
  62. mp4 = self._search_regex(
  63. r'''(?xs)var\s+clip\s*=\s*{\s*
  64. .+?\s*
  65. content_id\s*:\s*%s\s*,\s*
  66. .+?\s*
  67. mp4\s*:\s*\[(.*?),?\]\s*
  68. };\s*
  69. videoData\.push\(clip\);''' % content_id,
  70. webpage, 'mp4', fatal=False, default=None)
  71. smil = self._download_xml(
  72. 'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % content_id,
  73. content_id, 'Downloading video SMIL')
  74. http_base = find_xpath_attr(
  75. smil,
  76. './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
  77. 'name',
  78. 'httpBase').get('content')
  79. def random_string(str_len=0):
  80. return ''.join([random.choice(string.ascii_uppercase) for _ in range(str_len)])
  81. URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=%s&g=%s' % (random_string(5), random_string(12))
  82. formats = []
  83. if mp4:
  84. for video in json.loads('[%s]' % mp4):
  85. bitrate = video['bitrate']
  86. fmt = {
  87. 'url': video['src'].replace('http://pmd.video.howstuffworks.com', http_base) + URL_SUFFIX,
  88. 'format_id': bitrate,
  89. }
  90. m = re.search(r'(?P<vbr>\d+)[Kk]', bitrate)
  91. if m:
  92. fmt['vbr'] = int(m.group('vbr'))
  93. formats.append(fmt)
  94. else:
  95. for video in smil.findall(
  96. './/{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
  97. vbr = int(video.attrib['system-bitrate']) / 1000
  98. formats.append({
  99. 'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
  100. 'format_id': '%dk' % vbr,
  101. 'vbr': vbr,
  102. })
  103. self._sort_formats(formats)
  104. title = self._og_search_title(webpage)
  105. TITLE_SUFFIX = ' : HowStuffWorks'
  106. if title.endswith(TITLE_SUFFIX):
  107. title = title[:-len(TITLE_SUFFIX)]
  108. description = self._og_search_description(webpage)
  109. thumbnail = self._og_search_thumbnail(webpage)
  110. return {
  111. 'id': content_id,
  112. 'display_id': display_id,
  113. 'title': title,
  114. 'description': description,
  115. 'thumbnail': thumbnail,
  116. 'formats': formats,
  117. }