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