Add a 'parity'

This commit is contained in:
taizan-hokuto
2019-11-11 19:59:25 +09:00
parent 45f3274907
commit 47d0464b14
2 changed files with 51 additions and 7 deletions

View File

@@ -5,6 +5,16 @@ import math
import random import random
import urllib.parse import urllib.parse
'''
Generate continuation parameter of youtube replay chat.
Author: taizan-hokuto (2019)
ver 0.0.1 2019.10.05
if you use/copy this module
please tell me -> https://twitter.com/taizan205
'''
def _gen_vid(video_id): def _gen_vid(video_id):
"""generate video_id parameter. """generate video_id parameter.
@@ -45,11 +55,24 @@ def _nval(val):
buf += val.to_bytes(1,'big') buf += val.to_bytes(1,'big')
return buf return buf
def _tzparity(video_id,times):
t=0
for i,s in enumerate(video_id):
ss = ord(s)
if(ss % 2 == 0):
t += ss*(12-i)
else:
t ^= ss*i
return ((times^t) % 2).to_bytes(1,'big')
def get(video_id, seektime = 0, topchatonly = False): def get(video_id, seektime = 0, topchatonly = False):
switch_01 = b'\x04' if topchatonly else b'\x01' switch_01 = b'\x04' if topchatonly else b'\x01'
if seektime<0: if seektime < 0:
raise ValueError('seektime is 0 or positive number.') raise ValueError('seektime is 0 or positive number.')
if seektime == 0: if seektime == 0:
times =_nval(1) times =_nval(1)
@@ -57,6 +80,8 @@ def get(video_id, seektime = 0, topchatonly = False):
else: else:
times =_nval(int(seektime*1000000)) times =_nval(int(seektime*1000000))
switch = b'\x03' switch = b'\x03'
parity = _tzparity(video_id, seektime)
header_magic= b'\xA2\x9D\xB0\xD3\x04' header_magic= b'\xA2\x9D\xB0\xD3\x04'
sep_0 = b'\x1A' sep_0 = b'\x1A'
vid = _gen_vid(video_id) vid = _gen_vid(video_id)
@@ -66,9 +91,8 @@ def get(video_id, seektime = 0, topchatonly = False):
sep_2 = b'\x52\x1C\x08\x00\x10\x00\x18\x00\x20\x00' sep_2 = b'\x52\x1C\x08\x00\x10\x00\x18\x00\x20\x00'
chkstr = b'\x2A\x0E\x73\x74\x61\x74\x69\x63\x63\x68\x65\x63\x6B\x73\x75\x6D\x40' chkstr = b'\x2A\x0E\x73\x74\x61\x74\x69\x63\x63\x68\x65\x63\x6B\x73\x75\x6D\x40'
sep_3 = b'\x00\x58\x03\x60' sep_3 = b'\x00\x58\x03\x60'
sep_4 = b'\x68\x00\x72\x04\x08' sep_4 = b'\x68'+parity+b'\x72\x04\x08'
sep_5 = b'\x10\x00\x78\x00' sep_5 = b'\x10'+parity+b'\x78\x00'
body = [ body = [
sep_0, sep_0,
_nval(len(vid)), _nval(len(vid)),

View File

@@ -1,11 +1,19 @@
from base64 import urlsafe_b64encode as b64enc from base64 import urlsafe_b64encode as b64enc
from functools import reduce from functools import reduce
import calendar, datetime, pytz import calendar, datetime, pytz
import math
import random import random
import urllib.parse import urllib.parse
'''
Generate continuation parameter of youtube live chat.
Author: taizan-hokuto (2019)
ver 0.0.1 2019.10.05
if you use/copy this module
please tell me -> https://twitter.com/taizan205
'''
def _gen_vid(video_id): def _gen_vid(video_id):
"""generate video_id parameter. """generate video_id parameter.
Parameter Parameter
@@ -39,6 +47,17 @@ 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):
t=0
for i,s in enumerate(video_id):
ss = ord(s)
if(ss % 2 == 0):
t += ss*(12-i)
else:
t ^= ss*i
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
@@ -53,15 +72,16 @@ def _nval(val):
def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchatonly = False): def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchatonly = False):
#_short_type2 #_short_type2
switch_01 = b'\x04' if topchatonly else b'\x01' switch_01 = b'\x04' if topchatonly else b'\x01'
header_magic= b'\xD2\x87\xCC\xC8\x03' parity = _tzparity(video_id, _ts1^_ts2^_ts3^_ts4^_ts5)
header_magic= b'\xD2\x87\xCC\xC8\x03'
sep_0 = b'\x1A' sep_0 = b'\x1A'
vid = _gen_vid(video_id) vid = _gen_vid(video_id)
time_tag = b'\x28' time_tag = b'\x28'
timestamp1 = _nval(_ts1) timestamp1 = _nval(_ts1)
sep_1 = b'\x30\x00\x38\x00\x40\x02\x4A' sep_1 = b'\x30\x00\x38\x00\x40\x02\x4A'
un_len = b'\x2B' un_len = b'\x2B'
sep_2 = b'\x08\x00\x10\x00\x18\x00\x20\x00' sep_2 = b'\x08'+parity+b'\x10\x00\x18\x00\x20\x00'
chkstr = b'\x2A\x0E\x73\x74\x61\x74\x69\x63\x63\x68\x65\x63\x6B\x73\x75\x6D' chkstr = b'\x2A\x0E\x73\x74\x61\x74\x69\x63\x63\x68\x65\x63\x6B\x73\x75\x6D'
sep_3 = b'\x3A\x00\x40\x00\x4A' sep_3 = b'\x3A\x00\x40\x00\x4A'
sep_4_len = b'\x02' sep_4_len = b'\x02'