Benjamin-Loison / YouTube-operational-API

YouTube operational API works when YouTube Data API v3 fails.
372 stars 43 forks source link

Update comments #289

Open Benjamin-Loison opened 1 month ago

Benjamin-Loison commented 1 month ago

Someone asked me this feature on Discord.

Benjamin-Loison commented 1 month ago
curl https://www.youtube.com/youtubei/v1/comment/update_comment -H 'Content-Type: application/json' -H 'X-Goog-AuthUser: 1' -H 'Origin: https://www.youtube.com' -H 'Authorization: SAPISIDHASH CENSORED' -H 'Cookie: __Secure-1PSIDTS=sidts-CENSORED; __Secure-1PSID=CENSORED; __Secure-1PAPISID=CENSORED' --data-raw '{"context": {"client": {"clientName": "WEB", "clientVersion": "2.20240712.01.00"}}, "commentText": "Test", "updateCommentParams": "CENSORED"}'
Benjamin-Loison commented 1 month ago

https://www.youtube.com/watch?v=373TksynowI&lc=UgwtrfUlH98urhZFaXR4AaABAg

https://yt.lemnoslife.com/noKey/commentThreads?part=snippet&id=UgwtrfUlH98urhZFaXR4AaABAg

import requests
import json
import blackboxprotobuf
import base64
import hashlib
import time

SAPISID = 'CENSORED'
__Secure_1PSIDTS = 'CENSORED'
__Secure_1PSID = 'CENSORED'
COMMENT_ID = 'UgwtrfUlH98urhZFaXR4AaABAg'
VIDEO_ID_THE_COMMENT_IS_POSTED_ON = '373TksynowI'
NEW_COMMENT = 'This comment has been modified by web-scraping!'

currentTime = int(time.time())
sapisidhash = f'{currentTime}_' + hashlib.sha1(f'{currentTime} {SAPISID} https://www.youtube.com'.encode('ascii')).digest().hex()

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

message = {
    '1': COMMENT_ID,
    '4': VIDEO_THE_COMMENT_IS_POSTED_ON,
}

typedef = {
    '1': {
        'type': 'string'
    },
    '4': {
        'type': 'string'
    },
}

params = getBase64Protobuf(message, typedef)

URL = 'https://www.youtube.com/youtubei/v1/comment/update_comment'

headers = {
    'Origin': 'https://www.youtube.com',
    'Authorization': f'SAPISIDHASH {sapisidhash}',
    'X-Goog-AuthUser': '1',
}

cookies = {
    '__Secure-1PSIDTS': __Secure_1PSIDTS,
    '__Secure-1PSID': __Secure_1PSID,
    '__Secure-1PAPISID': SAPISID,
}

data = {
    'context': {
        'client': {
            'clientName': 'WEB',
            'clientVersion': '2.20240325.01.00',
        }
    },
    'commentText': NEW_COMMENT,
    'updateCommentParams': params,
}

data = requests.post(URL, headers = headers, cookies = cookies, json = data).json()
#print(json.dumps(data, indent = 4))
print('STATUS_SUCCEEDED' in json.dumps(data, indent = 4))