PetterKraabol / Twitch-Python

Object-oriented Twitch API for Python developers
https://pypi.org/project/twitch-python
MIT License
214 stars 37 forks source link

unhandled missing v5/Commenter causes crash #34

Open mgrandi opened 3 years ago

mgrandi commented 3 years ago

running tcd fails on a certain video because it seems the user the comment belongs to doesn't exist anymore or something, so the twitch api returns 'null' for that field, which twitch-python doesn't handle and throws a NPE

(tcd_venv) PS C:\Users\mark\Code\Personal\git\get_twitch_vod_chats> tcd --verbose --video 873505779 --format "json,default,irc,srt,ssa" --debug --output "tcd_output.log" --timezone "UTC"
NES speedruns
Downloading JSON data
Traceback (most recent call last):
  File "C:\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\mark\Temp\tcd_venv\Scripts\tcd.exe\__main__.py", line 7, in <module>
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\tcd\__init__.py", line 92, in main
    Downloader().videos(Arguments().video_ids)
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\tcd\downloader.py", line 183, in videos
    self.video(video)
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\tcd\downloader.py", line 100, in video
    for comment in video.comments:
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\twitch\v5\resources\comments.py", line 27, in __iter__
    yield v5.Comment(api=self._api, data=comment)
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\twitch\v5\models\comment.py", line 84, in __init__
    self.commenter: Commenter = Commenter(data.get('commenter'))
  File "c:\users\mark\temp\tcd_venv\lib\site-packages\twitch\v5\models\comment.py", line 18, in __init__
    self.display_name: str = data.get('display_name')
AttributeError: 'NoneType' object has no attribute 'get'

i added logging to both tcd and twitch-python (which i feel you should add to both as an aside, would have made it easier to debug) and as the video will probably be aged out by the time you look at this, i have the full api response output if you want.

Here is the relevant api response (a python dict, not JSON), note the None as the value for commenter

              {'_id': 'f1887ffa-1831-48f0-a989-26b5535b3627',
               'channel_id': '26642416',
               'commenter': None,
               'content_id': '873505779',
               'content_offset_seconds': 10472.579,
               'content_type': 'video',
               'created_at': '2021-01-14T21:54:30.279Z',
               'message': {'body': 'rambo!',
                           'fragments': [{'text': 'rambo!'}],
                           'is_action': False,
                           'user_badges': [{'_id': 'subscriber',
                                            'version': '3'},
                                           {'_id': 'premium', 'version': '1'}],
                           'user_notice_params': {}},
               'source': 'chat',
               'state': 'published',
               'updated_at': '2021-01-14T21:54:30.279Z'},

its fixed by adding a check to see if data.get("commenter") is none, and if so, just set self.commenter = None in v5/models/commenter.py : Comment.__init__, however all of the formatters except for JSON expect a commenter, so those parts of the code need to handle a potential missing commenter