menmob / innertube-documentation

Innertube Routes Documentation
3 stars 0 forks source link

Method to encode from JSON to base64 Protobuf #1

Closed Benjamin-Loison closed 1 year ago

Benjamin-Loison commented 1 year ago

In this Wiki section you give a method to decode a received base64 Protobuf but as I detail in this issue I would like to write in my source code meaningful JSON parameters like:

{
    "2": {
        "2": 1
    }
}

instead of the blacker box: EgIQAQ== but still be able to generate such base64 protobuf on the fly from above JSON. Is there any way to do so, even guessing the schema or so?

menmob commented 1 year ago

You can do this. I believe there is a python package called blackboxprotobuf I have used for it. Let me see if I can find a code example in a few hours.

Benjamin-Loison commented 1 year ago

Thank you for this reference, it seems to be quite what I was looking for.

import blackboxprotobuf
import base64
import json

data = base64.b64decode('EgIQAQ==', altchars = '-_')
message, typedef = blackboxprotobuf.decode_message(data)
print(json.dumps(message, indent = 4))
print(json.dumps(typedef, indent = 4))

##

message = {
    '2': {'2': 1}
}

typedef = {
    '2': {
        'type': 'message',
        'message_typedef': {
            '2': {
                'type': 'int'
            }
        },
        'field_order': [
            ('2', 0)
        ]
    }
}

data = blackboxprotobuf.encode_message(message, typedef)
print(base64.b64encode(data, altchars = b'-_').decode('ascii'))