Benjamin-Loison / YouTube-operational-API

YouTube operational API works when YouTube Data API v3 fails.
356 stars 41 forks source link

Retrieve *LIVE*s for *Auto-generated by YouTube* channels #287

Open Benjamin-Loison opened 2 weeks ago

Benjamin-Loison commented 2 weeks ago

As requested by someone as the Stack Overflow question 78703156.

For My region:

curl https://www.youtube.com/channel/UCZtmNrG53nmbq-Ww2VJrxEQ/live

For Worldwide:

curl https://www.youtube.com/youtubei/v1/browse -H 'Content-Type: application/json' --data-raw '{"context": {"client": {"clientName": "WEB", "clientVersion": "2.20240704.03.00"}}, "continuation": "4qmFsgJKEhhVQ1p0bU5yRzUzbm1icS1XdzJWSnJ4RVEaEkVnUnNhWFpsc2dNQ0NCcyUzRJoCGWNoYW5uZWwtZ2FtaW5nLWxpdmUtc2hlbGY%3D"}'
import requests
import json
import blackboxprotobuf
import base64

url = 'https://www.youtube.com/youtubei/v1/browse'

def getBase64Protobuf(message, typedef):
    data = blackboxprotobuf.encode_message(message, typedef)
    return base64.b64encode(data).decode('ascii')

message = {
    '54': {
        '1': 27
    }
}

typedef = {
    '54': {
        'type': 'message',
        'message_typedef': {
            '1': {
                'type': 'int'
            }
        },
        'field_order': [
            '1'
        ]
    }
}

three = getBase64Protobuf(message, typedef)

message = {
    '80226972': {
        '2': 'UCZtmNrG53nmbq-Ww2VJrxEQ',
        '3': three,
    }
}

typedef = {
    '80226972': {
        'type': 'message',
        'message_typedef': {
            '2': {
                'type': 'string'
            },
            '3': {
                'type': 'string'
            },
        },
        'field_order': [
            '2',
            '3',
        ]
    }
}

continuation = getBase64Protobuf(message, typedef)

data = {
    'context': {
        'client': {
            'clientName': 'WEB',
            'clientVersion': '2.20240325.01.00'
        }
    },
    'continuation': continuation
}

data = requests.post(url, json = data).json()
#print(json.dumps(data, indent = 4))
print('ILHA DAS LENDAS' in str(data))

is the most simplified.

Benjamin-Loison commented 2 weeks ago

I was about to publish as a comment:

For My region parsing https://www.youtube.com/channel/UCZtmNrG53nmbq-Ww2VJrxEQ/live source code is enough, but for Worldwide it requires Protobuf manipulation to make it, cleanly in my opinion. https://github.com/Benjamin-Loison/YouTube-operational-API/issues/287

but I realized that Mauricio Arias Olave initially answered my comment not the original post author.