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