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.

101 lines
3.6 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import int_or_none
  6. class CollegeHumorIE(InfoExtractor):
  7. _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/(video|embed|e)/(?P<videoid>[0-9]+)/?(?P<shorttitle>.*)$'
  8. _TESTS = [
  9. {
  10. 'url': 'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
  11. 'md5': 'dcc0f5c1c8be98dc33889a191f4c26bd',
  12. 'info_dict': {
  13. 'id': '6902724',
  14. 'ext': 'mp4',
  15. 'title': 'Comic-Con Cosplay Catastrophe',
  16. 'description': "Fans get creative this year at San Diego. Too creative. And yes, that's really Joss Whedon.",
  17. 'age_limit': 13,
  18. 'duration': 187,
  19. },
  20. }, {
  21. 'url': 'http://www.collegehumor.com/video/3505939/font-conference',
  22. 'md5': '72fa701d8ef38664a4dbb9e2ab721816',
  23. 'info_dict': {
  24. 'id': '3505939',
  25. 'ext': 'mp4',
  26. 'title': 'Font Conference',
  27. 'description': "This video wasn't long enough, so we made it double-spaced.",
  28. 'age_limit': 10,
  29. 'duration': 179,
  30. },
  31. }, {
  32. # embedded youtube video
  33. 'url': 'http://www.collegehumor.com/embed/6950306',
  34. 'info_dict': {
  35. 'id': 'Z-bao9fg6Yc',
  36. 'ext': 'mp4',
  37. 'title': 'Young Americans Think President John F. Kennedy Died THIS MORNING IN A CAR ACCIDENT!!!',
  38. 'uploader': 'Mark Dice',
  39. 'uploader_id': 'MarkDice',
  40. 'description': 'md5:62c3dab9351fac7bb44b53b69511d87f',
  41. 'upload_date': '20140127',
  42. },
  43. 'params': {
  44. 'skip_download': True,
  45. },
  46. 'add_ie': ['Youtube'],
  47. },
  48. ]
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VALID_URL, url)
  51. video_id = mobj.group('videoid')
  52. jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
  53. data = json.loads(self._download_webpage(
  54. jsonUrl, video_id, 'Downloading info JSON'))
  55. vdata = data['video']
  56. if vdata.get('youtubeId') is not None:
  57. return {
  58. '_type': 'url',
  59. 'url': vdata['youtubeId'],
  60. 'ie_key': 'Youtube',
  61. }
  62. AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
  63. rating = vdata.get('rating')
  64. if rating:
  65. age_limit = AGE_LIMITS.get(rating.lower())
  66. else:
  67. age_limit = None # None = No idea
  68. PREFS = {'high_quality': 2, 'low_quality': 0}
  69. formats = []
  70. for format_key in ('mp4', 'webm'):
  71. for qname, qurl in vdata.get(format_key, {}).items():
  72. formats.append({
  73. 'format_id': format_key + '_' + qname,
  74. 'url': qurl,
  75. 'format': format_key,
  76. 'preference': PREFS.get(qname),
  77. })
  78. self._sort_formats(formats)
  79. duration = int_or_none(vdata.get('duration'), 1000)
  80. like_count = int_or_none(vdata.get('likes'))
  81. return {
  82. 'id': video_id,
  83. 'title': vdata['title'],
  84. 'description': vdata.get('description'),
  85. 'thumbnail': vdata.get('thumbnail'),
  86. 'formats': formats,
  87. 'age_limit': age_limit,
  88. 'duration': duration,
  89. 'like_count': like_count,
  90. }