-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdlive.py
More file actions
184 lines (159 loc) · 5.52 KB
/
Copy pathdlive.py
File metadata and controls
184 lines (159 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
$description Global live-streaming platform owned by BitTorrent, Inc.
$url dlive.tv
$type live, vod
$metadata id
$metadata author
$metadata category
$metadata title
"""
import re
from textwrap import dedent
from urllib.parse import parse_qsl, urlparse
from streamlink.logger import getLogger
from streamlink.plugin import Plugin, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.hls import HLSStream
from streamlink.utils.times import fromtimestamp, now
log = getLogger(__name__)
class DLiveHLSStream(HLSStream):
URL_SIGN = "https://live.prd.dlive.tv/hls/sign/url"
URL_KEY_EXPIRES = "Expires"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.unsigned_url = self.args["url"]
self.signed_url = ""
self.signed_url_expires = fromtimestamp(timestamp=0)
@property
def url(self):
if self.signed_url_expires < now():
log.debug("Getting new signed HLS playlist URL")
self.signed_url = self._get_signed_url()
params = dict(parse_qsl(urlparse(self.signed_url).query))
expires = int(params.get(self.URL_KEY_EXPIRES, "0"))
self.signed_url_expires = fromtimestamp(timestamp=expires)
return self.signed_url
def _get_signed_url(self):
return self.session.http.post(
self.URL_SIGN,
json={"playlisturi": self.unsigned_url},
schema=validate.Schema(validate.url()),
)
@pluginmatcher(
name="live",
pattern=re.compile(
r"https?://(?:www\.)?dlive\.tv/(?P<channel>[^/?#]+)(?:$|[?#])",
),
)
@pluginmatcher(
name="vod",
pattern=re.compile(
r"https?://(?:www\.)?dlive\.tv/p/(?P<video>[^/?#]+)(?:$|[?#])",
),
)
class DLive(Plugin):
URL_API = "https://graphigo.prd.dlive.tv/"
URL_LIVE = "https://live.prd.dlive.tv/hls/live/{username}.m3u8"
QUALITY_WEIGHTS = {
"src": 1080,
}
@classmethod
def stream_weight(cls, stream: str) -> tuple[float, str]:
weight = cls.QUALITY_WEIGHTS.get(stream)
if weight:
return weight, "dlive"
return super().stream_weight(stream)
def _get_streams_video(self, video):
log.debug(f"Getting video HLS streams for {video}")
self.id = video
hls_url, self.author, self.category, self.title = self.session.http.post(
self.URL_API,
json={
"query": dedent(f"""
query {{
pastBroadcast(permlink:"{video}") {{
playbackUrl
creator {{
username
}}
category {{
title
}}
title
}}
}}
"""),
},
schema=validate.Schema(
validate.parse_json(),
{
"data": {
"pastBroadcast": {
"playbackUrl": validate.url(path=validate.endswith(".m3u8")),
"creator": {
"username": str,
},
"category": {
"title": str,
},
"title": str,
},
},
},
validate.get(("data", "pastBroadcast")),
validate.union_get(
"playbackUrl",
("creator", "username"),
("category", "title"),
"title",
),
),
)
return HLSStream.parse_variant_playlist(self.session, hls_url)
def _get_streams_live(self, channel):
log.debug(f"Getting live HLS streams for {channel}")
self.author = channel
username, self.title = self.session.http.post(
self.URL_API,
json={
"query": dedent(f"""
query {{
userByDisplayName(displayname:"{channel}") {{
username
livestream {{
title
}}
}}
}}
"""),
},
schema=validate.Schema(
validate.parse_json(),
{
"data": {
"userByDisplayName": {
"username": str,
"livestream": {
"title": str,
},
},
},
},
validate.get(("data", "userByDisplayName")),
validate.union_get(
"username",
("livestream", "title"),
),
),
)
return DLiveHLSStream.parse_variant_playlist(self.session, self.URL_LIVE.format(username=username))
def _get_streams(self):
self.session.http.headers.update({
"Origin": "https://dlive.tv",
"Referer": "https://dlive.tv/",
})
if self.matches["live"]:
return self._get_streams_live(self.match["channel"])
if self.matches["vod"]:
return self._get_streams_video(self.match["video"])
__plugin__ = DLive