[dlive] restrict DLive Stream _VALID_URL regex
[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>[a-zA-Z0-9]+)'
13     _TEST = {
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
25     def _real_extract(self, url):
26         uploader_id, vod_id = re.match(self._VALID_URL, url).groups()
27         broadcast = self._download_json(
28             'https://graphigo.prd.dlive.tv/', vod_id,
29             data=json.dumps({'query': '''query {
30   pastBroadcast(permlink:"%s+%s") {
31     content
32     createdAt
33     length
34     playbackUrl
35     title
36     thumbnailUrl
37     viewCount
38   }
39 }''' % (uploader_id, vod_id)}).encode())['data']['pastBroadcast']
40         title = broadcast['title']
41         formats = self._extract_m3u8_formats(
42             broadcast['playbackUrl'], vod_id, 'mp4', 'm3u8_native')
43         self._sort_formats(formats)
44         return {
45             'id': vod_id,
46             'title': title,
47             'uploader_id': uploader_id,
48             'formats': formats,
49             'description': broadcast.get('content'),
50             'thumbnail': broadcast.get('thumbnailUrl'),
51             'timestamp': int_or_none(broadcast.get('createdAt'), 1000),
52             'view_count': int_or_none(broadcast.get('viewCount')),
53         }
54
55
56 class DLiveStreamIE(InfoExtractor):
57     IE_NAME = 'dlive:stream'
58     _VALID_URL = r'https?://(?:www\.)?dlive\.tv/(?!p/)(?P<id>[\w.-]+)'
59
60     def _real_extract(self, url):
61         display_name = self._match_id(url)
62         user = self._download_json(
63             'https://graphigo.prd.dlive.tv/', display_name,
64             data=json.dumps({'query': '''query {
65   userByDisplayName(displayname:"%s") {
66     livestream {
67       content
68       createdAt
69       title
70       thumbnailUrl
71       watchingCount
72     }
73     username
74   }
75 }''' % display_name}).encode())['data']['userByDisplayName']
76         livestream = user['livestream']
77         title = livestream['title']
78         username = user['username']
79         formats = self._extract_m3u8_formats(
80             'https://live.prd.dlive.tv/hls/live/%s.m3u8' % username,
81             display_name, 'mp4')
82         self._sort_formats(formats)
83         return {
84             'id': display_name,
85             'title': self._live_title(title),
86             'uploader': display_name,
87             'uploader_id': username,
88             'formats': formats,
89             'description': livestream.get('content'),
90             'thumbnail': livestream.get('thumbnailUrl'),
91             'is_live': True,
92             'timestamp': int_or_none(livestream.get('createdAt'), 1000),
93             'view_count': int_or_none(livestream.get('watchingCount')),
94         }