[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / dlive.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class DLiveVODIE(InfoExtractor):
11     IE_NAME = 'dlive:vod'
12     _VALID_URL = r'https?://(?:www\.)?dlive\.tv/p/(?P<uploader_id>.+?)\+(?P<id>[^/?#&]+)'
13     _TESTS = [{
14         'url': 'https://dlive.tv/p/pdp+3mTzOl4WR',
15         'info_dict': {
16             'id': '3mTzOl4WR',
17             'ext': 'mp4',
18             'title': 'Minecraft with james charles epic',
19             'upload_date': '20190701',
20             'timestamp': 1562011015,
21             'uploader_id': 'pdp',
22         }
23     }, {
24         'url': 'https://dlive.tv/p/pdpreplay+D-RD-xSZg',
25         'only_matching': True,
26     }]
27
28     def _real_extract(self, url):
29         uploader_id, vod_id = re.match(self._VALID_URL, url).groups()
30         broadcast = self._download_json(
31             'https://graphigo.prd.dlive.tv/', vod_id,
32             data=json.dumps({'query': '''query {
33   pastBroadcast(permlink:"%s+%s") {
34     content
35     createdAt
36     length
37     playbackUrl
38     title
39     thumbnailUrl
40     viewCount
41   }
42 }''' % (uploader_id, vod_id)}).encode())['data']['pastBroadcast']
43         title = broadcast['title']
44         formats = self._extract_m3u8_formats(
45             broadcast['playbackUrl'], vod_id, 'mp4', 'm3u8_native')
46         self._sort_formats(formats)
47         return {
48             'id': vod_id,
49             'title': title,
50             'uploader_id': uploader_id,
51             'formats': formats,
52             'description': broadcast.get('content'),
53             'thumbnail': broadcast.get('thumbnailUrl'),
54             'timestamp': int_or_none(broadcast.get('createdAt'), 1000),
55             'view_count': int_or_none(broadcast.get('viewCount')),
56         }
57
58
59 class DLiveStreamIE(InfoExtractor):
60     IE_NAME = 'dlive:stream'
61     _VALID_URL = r'https?://(?:www\.)?dlive\.tv/(?!p/)(?P<id>[\w.-]+)'
62
63     def _real_extract(self, url):
64         display_name = self._match_id(url)
65         user = self._download_json(
66             'https://graphigo.prd.dlive.tv/', display_name,
67             data=json.dumps({'query': '''query {
68   userByDisplayName(displayname:"%s") {
69     livestream {
70       content
71       createdAt
72       title
73       thumbnailUrl
74       watchingCount
75     }
76     username
77   }
78 }''' % display_name}).encode())['data']['userByDisplayName']
79         livestream = user['livestream']
80         title = livestream['title']
81         username = user['username']
82         formats = self._extract_m3u8_formats(
83             'https://live.prd.dlive.tv/hls/live/%s.m3u8' % username,
84             display_name, 'mp4')
85         self._sort_formats(formats)
86         return {
87             'id': display_name,
88             'title': self._live_title(title),
89             'uploader': display_name,
90             'uploader_id': username,
91             'formats': formats,
92             'description': livestream.get('content'),
93             'thumbnail': livestream.get('thumbnailUrl'),
94             'is_live': True,
95             'timestamp': int_or_none(livestream.get('createdAt'), 1000),
96             'view_count': int_or_none(livestream.get('watchingCount')),
97         }