ninegag.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import str_to_int
  6. class NineGagIE(InfoExtractor):
  7. IE_NAME = '9gag'
  8. _VALID_URL = r'https?://(?:www\.)?9gag\.com/tv/p/(?P<id>[a-zA-Z0-9]+)/(?P<display_id>[^?#/]+)'
  9. _TESTS = [{
  10. "url": "http://9gag.com/tv/p/Kk2X5/people-are-awesome-2013-is-absolutely-awesome",
  11. "info_dict": {
  12. "id": "Kk2X5",
  13. "ext": "mp4",
  14. "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!)",
  15. "title": "\"People Are Awesome 2013\" Is Absolutely Awesome",
  16. 'uploader_id': 'UCdEH6EjDKwtTe-sO2f0_1XA',
  17. 'uploader': 'CompilationChannel',
  18. 'upload_date': '20131110',
  19. "view_count": int,
  20. "thumbnail": "re:^https?://",
  21. },
  22. 'add_ie': ['Youtube']
  23. }, {
  24. 'url': 'http://9gag.com/tv/p/KklwM/alternate-banned-opening-scene-of-gravity?ref=fsidebar',
  25. 'info_dict': {
  26. 'id': 'KklwM',
  27. 'ext': 'mp4',
  28. 'display_id': 'alternate-banned-opening-scene-of-gravity',
  29. "description": "While Gravity was a pretty awesome movie already, YouTuber Krishna Shenoi came up with a way to improve upon it, introducing a much better solution to Sandra Bullock's seemingly endless tumble in space. The ending is priceless.",
  30. 'title': "Banned Opening Scene Of \"Gravity\" That Changes The Whole Movie",
  31. 'uploader': 'Krishna Shenoi',
  32. 'upload_date': '20140401',
  33. 'uploader_id': 'krishnashenoi93',
  34. },
  35. 'add_ie': ['Youtube']
  36. }]
  37. _EXTERNAL_VIDEO_PROVIDER = {
  38. '1': {
  39. 'url': '%s',
  40. 'ie_key': 'Youtube',
  41. },
  42. '2': {
  43. 'url': 'http://player.vimeo.com/video/%s',
  44. 'ie_key': 'Vimeo',
  45. },
  46. '3': {
  47. 'url': 'http://instagram.com/p/%s',
  48. 'ie_key': 'Instagram',
  49. },
  50. '4': {
  51. 'url': 'http://vine.co/v/%s',
  52. 'ie_key': 'Vine',
  53. },
  54. }
  55. def _real_extract(self, url):
  56. mobj = re.match(self._VALID_URL, url)
  57. video_id = mobj.group('id')
  58. display_id = mobj.group('display_id')
  59. webpage = self._download_webpage(url, display_id)
  60. post_view = json.loads(self._html_search_regex(
  61. r'var postView = new app\.PostView\({\s*post:\s*({.+?}),\s*posts:\s*prefetchedCurrentPost', webpage, 'post view'))
  62. ie_key = None
  63. source_url = post_view.get('sourceUrl')
  64. if not source_url or source_url == '':
  65. external_video_id = post_view['videoExternalId']
  66. external_video_provider = post_view['videoExternalProvider']
  67. source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
  68. ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
  69. title = post_view['title']
  70. description = post_view['description']
  71. view_count = str_to_int(post_view['externalView'])
  72. thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
  73. return {
  74. '_type': 'url_transparent',
  75. 'url': source_url,
  76. 'ie_key': ie_key,
  77. 'id': video_id,
  78. 'display_id': display_id,
  79. 'title': title,
  80. 'description': description,
  81. 'view_count': view_count,
  82. 'thumbnail': thumbnail,
  83. }