ninegag.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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',
  25. 'only_matching': True,
  26. }]
  27. _EXTERNAL_VIDEO_PROVIDER = {
  28. '1': {
  29. 'url': '%s',
  30. 'ie_key': 'Youtube',
  31. },
  32. '2': {
  33. 'url': 'http://player.vimeo.com/video/%s',
  34. 'ie_key': 'Vimeo',
  35. },
  36. '3': {
  37. 'url': 'http://instagram.com/p/%s',
  38. 'ie_key': 'Instagram',
  39. },
  40. '4': {
  41. 'url': 'http://vine.co/v/%s',
  42. 'ie_key': 'Vine',
  43. },
  44. }
  45. def _real_extract(self, url):
  46. mobj = re.match(self._VALID_URL, url)
  47. video_id = mobj.group('id')
  48. display_id = mobj.group('display_id') or video_id
  49. webpage = self._download_webpage(url, display_id)
  50. post_view = json.loads(self._html_search_regex(
  51. r'var postView = new app\.PostView\({\s*post:\s*({.+?}),\s*posts:\s*prefetchedCurrentPost', webpage, 'post view'))
  52. ie_key = None
  53. source_url = post_view.get('sourceUrl')
  54. if not source_url:
  55. external_video_id = post_view['videoExternalId']
  56. external_video_provider = post_view['videoExternalProvider']
  57. source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
  58. ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
  59. title = post_view['title']
  60. description = post_view['description']
  61. view_count = str_to_int(post_view['externalView'])
  62. thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
  63. return {
  64. '_type': 'url_transparent',
  65. 'url': source_url,
  66. 'ie_key': ie_key,
  67. 'id': video_id,
  68. 'display_id': display_id,
  69. 'title': title,
  70. 'description': description,
  71. 'view_count': view_count,
  72. 'thumbnail': thumbnail,
  73. }