PEP8: applied even more rules
[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'''(?x)^https?://(?:www\.)?9gag\.tv/
13         (?:
14             v/(?P<numid>[0-9]+)|
15             p/(?P<id>[a-zA-Z0-9]+)/(?P<display_id>[^?#/]+)
16         )
17     '''
18
19     _TESTS = [{
20         "url": "http://9gag.tv/v/1912",
21         "info_dict": {
22             "id": "1912",
23             "ext": "mp4",
24             "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!)",
25             "title": "\"People Are Awesome 2013\" Is Absolutely Awesome",
26             "view_count": int,
27             "thumbnail": "re:^https?://",
28         },
29         'add_ie': ['Youtube']
30     }, {
31         'url': 'http://9gag.tv/p/KklwM/alternate-banned-opening-scene-of-gravity?ref=fsidebar',
32         'info_dict': {
33             'id': 'KklwM',
34             'ext': 'mp4',
35             'display_id': 'alternate-banned-opening-scene-of-gravity',
36             "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.",
37             'title': "Banned Opening Scene Of \"Gravity\" That Changes The Whole Movie",
38         },
39     }]
40
41     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         video_id = mobj.group('numid') or mobj.group('id')
44         display_id = mobj.group('display_id') or video_id
45
46         webpage = self._download_webpage(url, display_id)
47
48         post_view = json.loads(self._html_search_regex(
49             r'var postView = new app\.PostView\({\s*post:\s*({.+?}),\s*posts:\s*prefetchedCurrentPost', webpage, 'post view'))
50
51         youtube_id = post_view['videoExternalId']
52         title = post_view['title']
53         description = post_view['description']
54         view_count = str_to_int(post_view['externalView'])
55         thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
56
57         return {
58             '_type': 'url_transparent',
59             'url': youtube_id,
60             'ie_key': 'Youtube',
61             'id': video_id,
62             'display_id': display_id,
63             'title': title,
64             'description': description,
65             'view_count': view_count,
66             'thumbnail': thumbnail,
67         }