python-youtubechat icon indicating copy to clipboard operation
python-youtubechat copied to clipboard

How to get a broadcastId for someone else's stream?

Open dmikushin opened this issue 3 years ago • 3 comments

Thank you for this great library! I'm wondering how do I determine what should I set for the first argument of get_live_chat_id_for_broadcast_id() call for someone's stream? The stream URL is known.

dmikushin avatar Mar 03 '21 22:03 dmikushin

Were you able to get it? I now faced the same problem, I cannot find the broadcastId for one stream.

DamirLut avatar Mar 16 '21 15:03 DamirLut

How do you mean someone else's stream. The way this works is oath_creds grants the permission for one specific Youtube account. I have two accounts. The first one is my own account en the second account i've created is for my chatbot. Created two oauth_creds files (oath_cred-main en oauth_creds-bot) and granted the bot moderator rights for live chat messages. Broadcast ID is pulled from oauth_creds-main and the chat_obj uses oauth_creds-bot. Like so

livechat_id = get_live_chat_id_for_broadcast_id(broadcast_id,'oauth_creds-main') chat_obj = YoutubeLiveChat('oauth_creds-bot', [livechat_id])

wvthoog avatar Jul 07 '21 09:07 wvthoog

I modified some of the existing functions in ytchat.py to get the Live Chat ID from a channel ID. They are a bit heavy on the API calls as it uses the /search/ endpoint but as long as you don't call it too much it should be ok.

I am pretty new to Python so please excuse some of the mess but I am open to suggestions on how to make it better.


def get_liveid(cid):
    url = "https://www.googleapis.com/youtube/v3/search?"
    params = {'part': 'snippet', 'channelId': cid, 'maxResults': 1,
              'order': 'date', 'eventType': 'live', 'type': 'video'}
    params = urlencode(params)
    uri = (url + params)
    method = 'GET'

    resp, content = http.request(uri, method, headers=None, body=None)
    content_type, content_type_params = cgi.parse_header(resp.get('content-type', 'application/json; charset=UTF-8'))
    charset = content_type_params.get('charset', 'UTF-8')
    data = loads(content.decode(charset))

    if data['pageInfo']['totalResults'] == 0:
        print("No streams found")
    else:
        video_id = data['items'][0]['id']['videoId']
        return video_id


def get_livechatid(video_id):
    url = "https://youtube.googleapis.com/youtube/v3/videos?"
    params = {'part': 'liveStreamingDetails', 'id': video_id}
    params = urlencode(params)
    uri = (url + params)
    method = 'GET'

    resp, content = http.request(uri, method, headers=None, body=None)
    content_type, content_type_params = cgi.parse_header(resp.get('content-type', 'application/json; charset=UTF-8'))
    charset = content_type_params.get('charset', 'UTF-8')
    data = loads(content.decode(charset))

    live_chat_id = data['items'][0]['liveStreamingDetails']['activeLiveChatId']

    return live_chat_id

bigsby-exe avatar Mar 03 '22 00:03 bigsby-exe