ninegag.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import str_to_int
  5. class NineGagIE(InfoExtractor):
  6. IE_NAME = '9gag'
  7. _VALID_URL = r'https?://(?:www\.)?9gag(?:\.com/tv|\.tv)/p/(?P<id>[a-zA-Z0-9]+)(?:/(?P<display_id>[^?#/]+))?'
  8. _TESTS = [{
  9. 'url': 'http://9gag.com/tv/p/Kk2X5/people-are-awesome-2013-is-absolutely-awesome',
  10. 'info_dict': {
  11. 'id': 'Kk2X5',
  12. 'ext': 'mp4',
  13. 'description': 'This 3-minute video will make you smile and then make you feel untalented and insignificant. Anyway, you should share this awesomeness. (Thanks, Dino!)',
  14. 'title': '\"People Are Awesome 2013\" Is Absolutely Awesome',
  15. 'uploader_id': 'UCdEH6EjDKwtTe-sO2f0_1XA',
  16. 'uploader': 'CompilationChannel',
  17. 'upload_date': '20131110',
  18. 'view_count': int,
  19. },
  20. 'add_ie': ['Youtube'],
  21. }, {
  22. 'url': 'http://9gag.com/tv/p/aKolP3',
  23. 'info_dict': {
  24. 'id': 'aKolP3',
  25. 'ext': 'mp4',
  26. 'title': 'This Guy Travelled 11 countries In 44 days Just To Make This Amazing Video',
  27. 'description': "I just saw more in 1 minute than I've seen in 1 year. This guy's video is epic!!",
  28. 'uploader_id': 'rickmereki',
  29. 'uploader': 'Rick Mereki',
  30. 'upload_date': '20110803',
  31. 'view_count': int,
  32. },
  33. 'add_ie': ['Vimeo'],
  34. }, {
  35. 'url': 'http://9gag.com/tv/p/KklwM',
  36. 'only_matching': True,
  37. }, {
  38. 'url': 'http://9gag.tv/p/Kk2X5',
  39. 'only_matching': True,
  40. }]
  41. _EXTERNAL_VIDEO_PROVIDER = {
  42. '1': {
  43. 'url': '%s',
  44. 'ie_key': 'Youtube',
  45. },
  46. '2': {
  47. 'url': 'http://player.vimeo.com/video/%s',
  48. 'ie_key': 'Vimeo',
  49. },
  50. '3': {
  51. 'url': 'http://instagram.com/p/%s',
  52. 'ie_key': 'Instagram',
  53. },
  54. '4': {
  55. 'url': 'http://vine.co/v/%s',
  56. 'ie_key': 'Vine',
  57. },
  58. }
  59. def _real_extract(self, url):
  60. mobj = re.match(self._VALID_URL, url)
  61. video_id = mobj.group('id')
  62. display_id = mobj.group('display_id') or video_id
  63. webpage = self._download_webpage(url, display_id)
  64. post_view = self._parse_json(
  65. self._search_regex(
  66. r'var\s+postView\s*=\s*new\s+app\.PostView\({\s*post:\s*({.+?})\s*,\s*posts:\s*prefetchedCurrentPost',
  67. webpage, 'post view'),
  68. display_id)
  69. ie_key = None
  70. source_url = post_view.get('sourceUrl')
  71. if not source_url:
  72. external_video_id = post_view['videoExternalId']
  73. external_video_provider = post_view['videoExternalProvider']
  74. source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
  75. ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
  76. title = post_view['title']
  77. description = post_view.get('description')
  78. view_count = str_to_int(post_view.get('externalView'))
  79. thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
  80. return {
  81. '_type': 'url_transparent',
  82. 'url': source_url,
  83. 'ie_key': ie_key,
  84. 'id': video_id,
  85. 'display_id': display_id,
  86. 'title': title,
  87. 'description': description,
  88. 'view_count': view_count,
  89. 'thumbnail': thumbnail,
  90. }