[ninegag] remove unnecessary condition
[youtube-dl] / youtube_dl / extractor / ninegag.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import str_to_int
8
9
10 class NineGagIE(InfoExtractor):
11     IE_NAME = '9gag'
12     _VALID_URL = r'https?://(?:www\.)?9gag\.com/tv/p/(?P<id>[a-zA-Z0-9]+)/(?P<display_id>[^?#/]+)'
13
14     _TESTS = [{
15         "url": "http://9gag.com/tv/p/Kk2X5/people-are-awesome-2013-is-absolutely-awesome",
16         "info_dict": {
17             "id": "Kk2X5",
18             "ext": "mp4",
19             "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!)",
20             "title": "\"People Are Awesome 2013\" Is Absolutely Awesome",
21             'uploader_id': 'UCdEH6EjDKwtTe-sO2f0_1XA',
22             'uploader': 'CompilationChannel',
23             'upload_date': '20131110',
24             "view_count": int,
25             "thumbnail": "re:^https?://",
26         },
27         'add_ie': ['Youtube']
28     }, {
29         'url': 'http://9gag.com/tv/p/KklwM/alternate-banned-opening-scene-of-gravity?ref=fsidebar',
30         'info_dict': {
31             'id': 'KklwM',
32             'ext': 'mp4',
33             'display_id': 'alternate-banned-opening-scene-of-gravity',
34             "description": "While Gravity was a pretty awesome movie already, YouTuber Krishna Shenoi came up with a way to improve upon it, introducing a much better solution to Sandra Bullock's seemingly endless tumble in space. The ending is priceless.",
35             'title': "Banned Opening Scene Of \"Gravity\" That Changes The Whole Movie",
36             'uploader': 'Krishna Shenoi',
37             'upload_date': '20140401',
38             'uploader_id': 'krishnashenoi93',
39         },
40         'add_ie': ['Youtube']
41     }]
42     _EXTERNAL_VIDEO_PROVIDER = {
43         '1': {
44             'url': '%s',
45             'ie_key': 'Youtube',
46         },
47         '2': {
48             'url': 'http://player.vimeo.com/video/%s',
49             'ie_key': 'Vimeo',
50         },
51         '3': {
52             'url': 'http://instagram.com/p/%s',
53             'ie_key': 'Instagram',
54         },
55         '4': {
56             'url': 'http://vine.co/v/%s',
57             'ie_key': 'Vine',
58         },
59     }
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         video_id = mobj.group('id')
64         display_id = mobj.group('display_id')
65
66         webpage = self._download_webpage(url, display_id)
67
68         post_view = json.loads(self._html_search_regex(
69             r'var postView = new app\.PostView\({\s*post:\s*({.+?}),\s*posts:\s*prefetchedCurrentPost', webpage, 'post view'))
70
71         ie_key = None
72         source_url = post_view.get('sourceUrl')
73         if not source_url:
74             external_video_id = post_view['videoExternalId']
75             external_video_provider = post_view['videoExternalProvider']
76             source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
77             ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
78         title = post_view['title']
79         description = post_view['description']
80         view_count = str_to_int(post_view['externalView'])
81         thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
82
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         }