]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ninegag.py
[9gag] Modernize
[yt-dlp.git] / youtube_dl / extractor / ninegag.py
CommitLineData
ed850070
JMF
1from __future__ import unicode_literals
2
7fc3fa05 3import re
d7666dff 4import json
7fc3fa05
PH
5
6from .common import InfoExtractor
d7666dff 7from ..utils import str_to_int
7fc3fa05
PH
8
9
10class NineGagIE(InfoExtractor):
11 IE_NAME = '9gag'
6400f8ec 12 _VALID_URL = r'https?://(?:www\.)?9gag(?:\.com/tv|\.tv)/p/(?P<id>[a-zA-Z0-9]+)(?:/(?P<display_id>[^?#/]+))?'
7fc3fa05 13
4be9f8c8 14 _TESTS = [{
de3fc356 15 "url": "http://9gag.com/tv/p/Kk2X5/people-are-awesome-2013-is-absolutely-awesome",
ed850070 16 "info_dict": {
de3fc356 17 "id": "Kk2X5",
d2983ccb 18 "ext": "mp4",
ed850070 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!)",
84769e70 20 "title": "\"People Are Awesome 2013\" Is Absolutely Awesome",
e9404524
PH
21 'uploader_id': 'UCdEH6EjDKwtTe-sO2f0_1XA',
22 'uploader': 'CompilationChannel',
23 'upload_date': '20131110',
84769e70
PH
24 "view_count": int,
25 "thumbnail": "re:^https?://",
7fc3fa05 26 },
ed850070 27 'add_ie': ['Youtube']
9e1a5b84 28 }, {
c3a4e2ec
S
29 'url': 'http://9gag.com/tv/p/KklwM',
30 'only_matching': True,
6400f8ec
S
31 }, {
32 'url': 'http://9gag.tv/p/Kk2X5',
33 'only_matching': True,
4be9f8c8 34 }]
de3fc356 35 _EXTERNAL_VIDEO_PROVIDER = {
36 '1': {
37 'url': '%s',
38 'ie_key': 'Youtube',
39 },
40 '2': {
41 'url': 'http://player.vimeo.com/video/%s',
42 'ie_key': 'Vimeo',
43 },
44 '3': {
45 'url': 'http://instagram.com/p/%s',
46 'ie_key': 'Instagram',
47 },
48 '4': {
49 'url': 'http://vine.co/v/%s',
50 'ie_key': 'Vine',
51 },
52 }
7fc3fa05
PH
53
54 def _real_extract(self, url):
55 mobj = re.match(self._VALID_URL, url)
de3fc356 56 video_id = mobj.group('id')
e28c7946 57 display_id = mobj.group('display_id') or video_id
7fc3fa05 58
4be9f8c8 59 webpage = self._download_webpage(url, display_id)
7fc3fa05 60
c659022b
S
61 post_view = self._parse_json(
62 self._search_regex(
63 r'var\s+postView\s*=\s*new\s+app\.PostView\({\s*post:\s*({.+?})\s*,\s*posts:\s*prefetchedCurrentPost',
64 webpage, 'post view'),
65 display_id)
d7666dff 66
6b8ce312 67 ie_key = None
68 source_url = post_view.get('sourceUrl')
da9f1808 69 if not source_url:
6b8ce312 70 external_video_id = post_view['videoExternalId']
71 external_video_provider = post_view['videoExternalProvider']
72 source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
73 ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
d7666dff 74 title = post_view['title']
8ca2e93e
S
75 description = post_view.get('description')
76 view_count = str_to_int(post_view.get('externalView'))
d7666dff 77 thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')
7fc3fa05
PH
78
79 return {
80 '_type': 'url_transparent',
6b8ce312 81 'url': source_url,
82 'ie_key': ie_key,
7fc3fa05 83 'id': video_id,
4be9f8c8
PH
84 'display_id': display_id,
85 'title': title,
84769e70
PH
86 'description': description,
87 'view_count': view_count,
d7666dff 88 'thumbnail': thumbnail,
7fc3fa05 89 }