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.

102 lines
3.4 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. 'url': 'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
  10. 'md5': 'dcc0f5c1c8be98dc33889a191f4c26bd',
  11. 'info_dict': {
  12. 'id': '6902724',
  13. 'ext': 'mp4',
  14. 'title': 'Comic-Con Cosplay Catastrophe',
  15. 'description': "Fans get creative this year at San Diego. Too creative. And yes, that's really Joss Whedon.",
  16. 'age_limit': 13,
  17. 'duration': 187,
  18. },
  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. {
  34. 'url': 'http://www.collegehumor.com/embed/6950306',
  35. 'info_dict': {
  36. 'id': 'Z-bao9fg6Yc',
  37. 'ext': 'mp4',
  38. 'title': 'Young Americans Think President John F. Kennedy Died THIS MORNING IN A CAR ACCIDENT!!!',
  39. 'uploader': 'Mark Dice',
  40. 'uploader_id': 'MarkDice',
  41. 'description': 'md5:62c3dab9351fac7bb44b53b69511d87f',
  42. 'upload_date': '20140127',
  43. },
  44. 'params': {
  45. 'skip_download': True,
  46. },
  47. 'add_ie': ['Youtube'],
  48. },
  49. ]
  50. def _real_extract(self, url):
  51. mobj = re.match(self._VALID_URL, url)
  52. video_id = mobj.group('videoid')
  53. jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
  54. data = json.loads(self._download_webpage(
  55. jsonUrl, video_id, 'Downloading info JSON'))
  56. vdata = data['video']
  57. if vdata.get('youtubeId') is not None:
  58. return {
  59. '_type': 'url',
  60. 'url': vdata['youtubeId'],
  61. 'ie_key': 'Youtube',
  62. }
  63. AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
  64. rating = vdata.get('rating')
  65. if rating:
  66. age_limit = AGE_LIMITS.get(rating.lower())
  67. else:
  68. age_limit = None # None = No idea
  69. PREFS = {'high_quality': 2, 'low_quality': 0}
  70. formats = []
  71. for format_key in ('mp4', 'webm'):
  72. for qname, qurl in vdata.get(format_key, {}).items():
  73. formats.append({
  74. 'format_id': format_key + '_' + qname,
  75. 'url': qurl,
  76. 'format': format_key,
  77. 'preference': PREFS.get(qname),
  78. })
  79. self._sort_formats(formats)
  80. duration = int_or_none(vdata.get('duration'), 1000)
  81. like_count = int_or_none(vdata.get('likes'))
  82. return {
  83. 'id': video_id,
  84. 'title': vdata['title'],
  85. 'description': vdata.get('description'),
  86. 'thumbnail': vdata.get('thumbnail'),
  87. 'formats': formats,
  88. 'age_limit': age_limit,
  89. 'duration': duration,
  90. 'like_count': like_count,
  91. }