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.

68 lines
2.3 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. class CollegeHumorIE(InfoExtractor):
  6. _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/(video|embed|e)/(?P<videoid>[0-9]+)/?(?P<shorttitle>.*)$'
  7. _TESTS = [{
  8. 'url': 'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
  9. 'file': '6902724.mp4',
  10. 'md5': 'dcc0f5c1c8be98dc33889a191f4c26bd',
  11. 'info_dict': {
  12. 'title': 'Comic-Con Cosplay Catastrophe',
  13. 'description': 'Fans get creative this year at San Diego. Too',
  14. 'age_limit': 13,
  15. },
  16. },
  17. {
  18. 'url': 'http://www.collegehumor.com/video/3505939/font-conference',
  19. 'file': '3505939.mp4',
  20. 'md5': '72fa701d8ef38664a4dbb9e2ab721816',
  21. 'info_dict': {
  22. 'title': 'Font Conference',
  23. 'description': 'This video wasn\'t long enough, so we made it double-spaced.',
  24. 'age_limit': 10,
  25. },
  26. }]
  27. def _real_extract(self, url):
  28. mobj = re.match(self._VALID_URL, url)
  29. video_id = mobj.group('videoid')
  30. jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
  31. data = json.loads(self._download_webpage(
  32. jsonUrl, video_id, 'Downloading info JSON'))
  33. vdata = data['video']
  34. AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
  35. rating = vdata.get('rating')
  36. if rating:
  37. age_limit = AGE_LIMITS.get(rating.lower())
  38. else:
  39. age_limit = None # None = No idea
  40. PREFS = {'high_quality': 2, 'low_quality': 0}
  41. formats = []
  42. for format_key in ('mp4', 'webm'):
  43. for qname, qurl in vdata[format_key].items():
  44. formats.append({
  45. 'format_id': format_key + '_' + qname,
  46. 'url': qurl,
  47. 'format': format_key,
  48. 'preference': PREFS.get(qname),
  49. })
  50. self._sort_formats(formats)
  51. return {
  52. 'id': video_id,
  53. 'title': vdata['title'],
  54. 'description': vdata.get('description'),
  55. 'thumbnail': vdata.get('thumbnail'),
  56. 'formats': formats,
  57. 'age_limit': age_limit,
  58. }