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.

104 lines
3.6 KiB

  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|embed)/(?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. 'url': 'http://9gag.com/tv/embed/a5Dmvl',
  42. 'only_matching': True,
  43. }]
  44. _EXTERNAL_VIDEO_PROVIDER = {
  45. '1': {
  46. 'url': '%s',
  47. 'ie_key': 'Youtube',
  48. },
  49. '2': {
  50. 'url': 'http://player.vimeo.com/video/%s',
  51. 'ie_key': 'Vimeo',
  52. },
  53. '3': {
  54. 'url': 'http://instagram.com/p/%s',
  55. 'ie_key': 'Instagram',
  56. },
  57. '4': {
  58. 'url': 'http://vine.co/v/%s',
  59. 'ie_key': 'Vine',
  60. },
  61. }
  62. def _real_extract(self, url):
  63. mobj = re.match(self._VALID_URL, url)
  64. video_id = mobj.group('id')
  65. display_id = mobj.group('display_id') or video_id
  66. webpage = self._download_webpage(url, display_id)
  67. post_view = self._parse_json(
  68. self._search_regex(
  69. r'var\s+postView\s*=\s*new\s+app\.PostView\({\s*post:\s*({.+?})\s*,\s*posts:\s*prefetchedCurrentPost',
  70. webpage, 'post view'),
  71. display_id)
  72. ie_key = None
  73. source_url = post_view.get('sourceUrl')
  74. if not source_url:
  75. external_video_id = post_view['videoExternalId']
  76. external_video_provider = post_view['videoExternalProvider']
  77. source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
  78. ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
  79. title = post_view['title']
  80. description = post_view.get('description')
  81. view_count = str_to_int(post_view.get('externalView'))
  82. thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
  83. return {
  84. '_type': 'url_transparent',
  85. 'url': source_url,
  86. 'ie_key': ie_key,
  87. 'id': video_id,
  88. 'display_id': display_id,
  89. 'title': title,
  90. 'description': description,
  91. 'view_count': view_count,
  92. 'thumbnail': thumbnail,
  93. }