Modify bytes combination

This commit is contained in:
taizan-hokuto
2020-04-18 00:55:56 +09:00
parent 3243d69d7a
commit 84bae4ad2a
2 changed files with 101 additions and 94 deletions

View File

@@ -12,6 +12,7 @@ Author: taizan-hokuto (2019) @taizan205
ver 0.0.1 2019.10.05 ver 0.0.1 2019.10.05
''' '''
def _gen_vid(video_id): def _gen_vid(video_id):
"""generate video_id parameter. """generate video_id parameter.
Parameter Parameter
@@ -40,9 +41,11 @@ def _gen_vid(video_id):
b64enc(reduce(lambda x, y: x+y, item)).decode() b64enc(reduce(lambda x, y: x+y, item)).decode()
).encode() ).encode()
def _nval(val): def _nval(val):
"""convert value to byte array""" """convert value to byte array"""
if val<0: raise ValueError if val < 0:
raise ValueError
buf = b'' buf = b''
while val >> 7: while val >> 7:
m = val & 0xFF | 0x80 m = val & 0xFF | 0x80
@@ -51,6 +54,7 @@ def _nval(val):
buf += val.to_bytes(1, 'big') buf += val.to_bytes(1, 'big')
return buf return buf
def _build(video_id, seektime, topchat_only): def _build(video_id, seektime, topchat_only):
switch_01 = b'\x04' if topchat_only else b'\x01' switch_01 = b'\x04' if topchat_only else b'\x01'
if seektime < 0: if seektime < 0:
@@ -75,7 +79,8 @@ def _build(video_id, seektime, topchat_only):
sep_3 = b'\x00\x58\x03\x60' sep_3 = b'\x00\x58\x03\x60'
sep_4 = b'\x68' + parity + b'\x72\x04\x08' sep_4 = b'\x68' + parity + b'\x72\x04\x08'
sep_5 = b'\x10' + parity + b'\x78\x00' sep_5 = b'\x10' + parity + b'\x78\x00'
body = [
body = b''.join([
sep_0, sep_0,
_nval(len(vid)), _nval(len(vid)),
vid, vid,
@@ -90,9 +95,7 @@ def _build(video_id, seektime, topchat_only):
sep_4, sep_4,
switch_01, switch_01,
sep_5 sep_5
] ])
body = reduce(lambda x, y: x+y, body)
return urllib.parse.quote( return urllib.parse.quote(
b64enc(header_magic + b64enc(header_magic +
@@ -101,6 +104,7 @@ def _build(video_id, seektime, topchat_only):
).decode() ).decode()
) )
def getparam(video_id, seektime=0, topchat_only=False): def getparam(video_id, seektime=0, topchat_only=False):
''' '''
Parameter Parameter

View File

@@ -11,6 +11,8 @@ Author: taizan-hokuto (2019) @taizan205
ver 0.0.1 2019.10.05 ver 0.0.1 2019.10.05
''' '''
def _gen_vid(video_id): def _gen_vid(video_id):
"""generate video_id parameter. """generate video_id parameter.
Parameter Parameter
@@ -44,6 +46,7 @@ def _gen_vid(video_id):
b64enc(reduce(lambda x, y: x+y, item)).decode() b64enc(reduce(lambda x, y: x+y, item)).decode()
).encode() ).encode()
def _tzparity(video_id, times): def _tzparity(video_id, times):
t = 0 t = 0
for i, s in enumerate(video_id): for i, s in enumerate(video_id):
@@ -55,9 +58,11 @@ def _tzparity(video_id,times):
return ((times ^ t) % 2).to_bytes(1, 'big') return ((times ^ t) % 2).to_bytes(1, 'big')
def _nval(val): def _nval(val):
"""convert value to byte array""" """convert value to byte array"""
if val<0: raise ValueError if val < 0:
raise ValueError
buf = b'' buf = b''
while val >> 7: while val >> 7:
m = val & 0xFF | 0x80 m = val & 0xFF | 0x80
@@ -66,6 +71,7 @@ def _nval(val):
buf += val.to_bytes(1, 'big') buf += val.to_bytes(1, 'big')
return buf return buf
def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only): def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
# _short_type2 # _short_type2
switch_01 = b'\x04' if topchat_only else b'\x01' switch_01 = b'\x04' if topchat_only else b'\x01'
@@ -99,7 +105,7 @@ def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
sep_9 = b'\x88\x01\x00\xA0\x01' sep_9 = b'\x88\x01\x00\xA0\x01'
timestamp5 = _nval(_ts5) timestamp5 = _nval(_ts5)
body = [ body = b''.join([
sep_0, sep_0,
_nval(len(vid)), _nval(len(vid)),
vid, vid,
@@ -121,15 +127,13 @@ def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
ts_3_end, ts_3_end,
timestamp4, timestamp4,
sep_6, sep_6,
switch_01,# switch_01,
sep_7, sep_7,
switch_01,# switch_01,
sep_8, sep_8,
sep_9, sep_9,
timestamp5 timestamp5
] ])
body = reduce(lambda x, y: x+y, body)
return urllib.parse.quote( return urllib.parse.quote(
b64enc(header_magic + b64enc(header_magic +
@@ -161,4 +165,3 @@ def getparam(video_id, past_sec = 0, topchat_only = False):
if True, fetch only 'top chat' if True, fetch only 'top chat'
''' '''
return _build(video_id, *_times(past_sec), topchat_only) return _build(video_id, *_times(past_sec), topchat_only)