PixelBlurb
PixelBlurb

Reputation: 58

pytchat python library cannot find valid livechat comment id YouTube API

I am trying to use the pytchat library to get information about my moderated livestream chats. The two main pieces of information I want are these: the message text (I can get this reliably) and the comment id (less reliable apparently). The pytchat library allows access to YouTube request information in a variety of different ways. All seem to give me the same botched output. Using this code:

from pytchat import CompatibleProcessor, create
import time, sys
chat_ = create(sys.argv[1], processor = CompatibleProcessor())
while chat_.is_alive():
  try:
    data = chat_.get()
    polling = data['pollingIntervalMillis']/1000
    for c in data['items']:
      if c.get('snippet'):
        print(c['snippet']['displayMessage'] + ' || ' + c['id'])
        time.sleep(polling/len(data['items']))
  except KeyboardInterrupt:
    chat_.terminate()

I get a readout of every comment as it is provided by YouTube. The message bit is fine. c['id'] however is a different story. The comment ids stored by this element are usually formatted in this way:

LCC.CjkKGkNQRHhoYUNEdVBrQ0ZWRzE1UWNkdEhZQzlREhtDT0dDMFBYMXRfa0NGWTZUd2dFZDdkZ0Uydzk%3D

This is not an actual comment id. I know as much, because whenever I pass this comment id to YouTube API in a delete request I get the following:

{'error': {'code': 404, 'message': 'The liveChatMessage that you are trying to delete cannot be found.', 'errors': [{'message': 'The liveChatMessage that you are trying to delete cannot be found.', 'domain': 'youtube.liveChat', 'reason': 'liveChatMessageNotFound'}]}}

Making a POST request with curl yields a different style of comment id:

LCC.CjgKDQoLQlRERDlEYVFZdHMqJwoYVUMxSE1UdDVjb0VWNi1tV1BhZEJRU0l3EgtCVEREOURhUVl0cxI5ChpDUER4aGFDRHVQa0NGVkcxNVFjZHRIWUM5URIbQ09HQzBQWDF0X2tDRlk2VHdnRWQ3ZGdFMnc5

I haven't tried deleting comments with this type of comment id, but just looking at it makes me think it is infinitely more valid than whatever is being supplied by pytchat.

Is this expected behavior for pytchat? I haven't seen anyone else use the library this way. If so, how can I get the actual comment id? (preferably without supplying any authorization tokens)

Edit (example c snippets with associated livestream video-ids):

xLsoN8z9xq:
{'kind': 'youtube#liveChatMessage', 'etag': '', 'id': 'LCC.CjoKGkNKSGVfTml5dXZrQ0Zjc1dyUVlkOUhNSnN3EhxDUFROd0txc3V2a0NGV1doMkFVZHNFY0l1ZzEw', 'snippet': {'type': 'textMessageEvent', 'liveChatId': '', 'authorChannelId': 'UCdyotM4u3QW4Nvqr-iZghyQ', 'publishedAt': '2022-08-09T18:33:27.877Z', 'hasDisplayContent': True, 'displayMessage': 'all hail for lord yacine', 'textMessageDetails': {'messageText': 'all hail for lord yacine'}}, 'authorDetails': {'channelId': 'UCdyotM4u3QW4Nvqr-iZghyQ', 'channelUrl': 'http://www.youtube.com/channel/UCdyotM4u3QW4Nvqr-iZghyQ', 'displayName': 'rahil', 'profileImageUrl': 'https://yt4.ggpht.com/ytc/AMLnZu_nFXi8dLoAOUeyiS0wcqLivdaFyTOc1yYQjbiOqg=s64-c-k-c0x00ffffff-no-rj', 'isVerified': False, 'isChatOwner': False, 'isChatSponsor': False, 'isChatModerator': False}}

mx9EqnEhg74:
{'kind': 'youtube#liveChatMessage', 'etag': '', 'id': 'LCC.CjsKGkNPdmprckt5dXZrQ0ZXS3Q1UWNkSWM4S0hnEh1DSm10azkydXV2a0NGYW9tZ3dvZDI5UUYzQS0xMw%3D%3D', 'snippet': {'type': 'textMessageEvent', 'liveChatId': '', 'authorChannelId': 'UCgkIDx-V7zMjnCrEtXaxUWQ', 'publishedAt': '2022-08-09T18:32:06.449Z', 'hasDisplayContent': True, 'displayMessage': 'yuck', 'textMessageDetails': {'messageText': 'yuck'}}, 'authorDetails': {'channelId': 'UCgkIDx-V7zMjnCrEtXaxUWQ', 'channelUrl': 'http://www.youtube.com/channel/UCgkIDx-V7zMjnCrEtXaxUWQ', 'displayName': 'grace', 'profileImageUrl': 'https://yt4.ggpht.com/nDFvN9uOZw7eP3-NQhIZZmXH-I5J30FEF629JjJ-NMpADvWLy3Q6BIkqtT-tX8drsVprLcNw=s64-c-k-c0x00ffffff-no-rj', 'isVerified': False, 'isChatOwner': False, 'isChatSponsor': False, 'isChatModerator': False}}

jfKfPfyJRdk:
{'kind': 'youtube#liveChatMessage', 'etag': '', 'id': 'LCC.CkUKGkNPcV9oWVd5dXZrQ0ZhTWlyUVlkSFJjSEVnEidDUDZYMDdxd3V2a0NGYy1JbFFJZHBxSUxRUTE2NjAwNjk4Mjk0Njk%3D', 'snippet': {'type': 'textMessageEvent', 'liveChatId': '', 'authorChannelId': 'UChOT0CZQ9ZwiwxNfkdI72tQ', 'publishedAt': '2022-08-09T18:30:31.860Z', 'hasDisplayContent': True, 'displayMessage': 'mds eu tô em um tédio absurdo KKKKKK', 'textMessageDetails': {'messageText': 'mds eu tô em um tédio absurdo KKKKKK'}}, 'authorDetails': {'channelId': 'UChOT0CZQ9ZwiwxNfkdI72tQ', 'channelUrl': 'http://www.youtube.com/channel/UChOT0CZQ9ZwiwxNfkdI72tQ', 'displayName': 'Ruth.', 'profileImageUrl': 'https://yt4.ggpht.com/ytc/AMLnZu-P2JRuFGU5ibeJiBIflPSkhUyS_0Zqz3w6CClZBg=s64-c-k-c0x00ffffff-no-rj', 'isVerified': False, 'isChatOwner': False, 'isChatSponsor': False, 'isChatModerator': False}}

Yet another edit: I read over the pytchat source code and found this suspect snippet of code (in __init__.py):

from base64 import a85decode as dc
_sml = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9<FC/iuG%G#jBOQ!ICLqcS5tQB2;gCZ)?UdXC;f$GR3)MM2<(0>O7mh!,G@+K5?SO9T@okV").decode()
_smr = dc(b"BQS?8F#ks-GB\\6`H#IhIF^eo7@rH3;H#IhIF^eor06T''Ch\\'(?XmbXF>%9<FC/iuG%G#jBOQ!iEb03+@<k(QAU-F)8U=fDGsP557S5F7CiNH7;)D3N77^*B6YU@\\?WfBr0emZX=#^").decode()

These are links to YouTube with API keys as parameters. If you look at YouTube's own documentation for their API (or go looking anywhere else) you'll soon find that the youtubei/v1 API is no longer supported. In fact, I cannot find documentation for the old API anywhere. It seems I am at a dead end.

Upvotes: 1

Views: 432

Answers (2)

PixelBlurb
PixelBlurb

Reputation: 58

This is a stand-in answer until I can provide some code snippets that will make some things clear. First and foremost, if you are wondering why pytchat does not provide valid live-chat comment ids, I have to say I do not know for sure. I will however render my best guess so far: The version of the YouTube API pytchat uses is very old. In fact it might predate the ability to delete live-chat comments (my desired goal). The pytchat source code I quoted above helped me achieve this hypothesis. The _sml and _smr variables are set to:

'https://www.youtube.com/youtubei/v1/live_chat/get_live_chat_replay?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'

and:

'https://www.youtube.com/youtubei/v1/live_chat/get_live_chat?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'

respectively. These variables are used throughout the code in get requests made via the httpx library.

Some method might exist for deleting YouTube live-chat comments via this old API, but I have yet to find documentation on it.

Until later, PixelBlurb

Upvotes: 0

The sample data you provided in your question doesn't have a value in the the liveChatId property:

snippet.liveChatId string

The ID that uniquely identifies the live chat with which the message is associated. The live chat ID associated with a broadcast is returned in the liveBroadcast resource's snippet.liveChatId property.

With this, you should check the LiveBroadcasts: list endpoint in order to return the liveChatId.

By checking this ticket on Issue Tracker, I use the liveChatId in the YouTube Data API official documentation; however, I got that (for this example), the live chat is no longer live.

Try-it example with LiveChatMessages: list

Result:

{
  "error": {
    "code": 403,
    "message": "The live chat is no longer live.",
    "errors": [
      {
        "message": "The live chat is no longer live.",
        "domain": "youtube.liveChat",
        "reason": "liveChatEnded"
      }
    ]
  }
}

You can see in this example, I'm using a liveChatId as parameter, but, this ID looks very different from the "id" shown in your snippets...

This looks to me like a bug with the API, but, I admit I haven't tried the LiveChatMessages and LiveBroadcast endpoints.

If you consider this is a bug or if you require further help, consider check the Issue Tracker and see if someone else has posted a similar issue of yours or post there the issue as well.

Upvotes: 1

Related Questions