ninegag.py 3.0 KB

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