My web app uses Pytchat module once a day to retrieve YouTube live stream information, but about 1 out of every 60 video IDs fail to retrieve, and this issue only occurs on Heroku.
I’ve searched extensively for common factors among the videos that cause errors with the YouTube API, but I couldn’t find any.
Here is my environment:
- Python 3.12.4
- Pytchat 0.5.5(latest)
Here is my script:
import pytchat
import requests, json
# Check if the API communication works
url = requests.get("http://api.aoikujira.com/kawase/json/usd")
text = url.text
json_currency = json.loads(text)
print(json_currency)
print("Successful in API communication")
ids = "g5vKkS19nq8,kHo37taBFP8,6IJpVVGIxno,LPI_FnuvfhE,dWhmdgpeBts,NXcmsY5iXkw,ECATLuOt1AE"
ids = ids.split(",")
# To view the error, please run this code in heroku server.
# chat = pytchat.create(video_id=ids[0],interruptable=False)
# Try to create pytchat objects with wrong ids
for id in ids:
try:
chat = pytchat.create(video_id=id,interruptable=False)
print("Successful in create")
except:
print("Failed in create")
Here is the error:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/util/__init__.py", line 107, in get_channelid
raise IndexError
IndexError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/test_script/mini_livetest.py", line 15, in <module>
chat = pytchat.create(video_id=ids[0],interruptable=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/core/__init__.py", line 7, in create
return PytchatCore(_vid, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/core/pytchat.py", line 96, in __init__
self._setup()
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/core/pytchat.py", line 106, in _setup
channel_id=util.get_channelid(self._client, self._video_id),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/util/__init__.py", line 110, in get_channelid
ret = get_channelid_2nd(client, video_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/pytchat/util/__init__.py", line 119, in get_channelid_2nd
raise InvalidVideoIdException(f"Cannot find channel id for video id:{video_id}. This video id seems to be invalid.")
pytchat.exceptions.InvalidVideoIdException: Cannot find channel id for video id:g5vKkS19nq8. This video id seems to be invalid.
The code worked not only in local, but also in colab: https://colab.research.google.com/drive/1npGihQ4KVHFn1tBMpAbY7HDFVsDk4CNW
BEN BEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.