PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
798 stars 163 forks source link

occasional date parsing errors for PubSubChannelPointsMessage in ext/pubsub/models.py #227

Closed charlesmadere closed 3 years ago

charlesmadere commented 3 years ago

Sometimes, when constructing a new PubSubChannelPointsMessage instance, this line will cause an exception to be thrown:

self.timestamp = datetime.datetime.strptime(
    redemption["redeemed_at"][0:25] + redemption["redeemed_at"][28:], "%Y-%m-%dT%H:%M:%S.%fZ"
)

It seems that the Z character doesn't always end up being a part of the string, and as such, the strptime call fails.

I wrote a really stupid fix for this, that seems to work for me:

Change the self.timestamp line within PubSubChannelPointsMessage's __init__ to this:

self.timestamp = parseDate(redemption["redeemed_at"])

Then add this method somewhere within ext/pubsub/models.py:

def parseDate(originalDateStr: str) -> datetime:
    dateStr = originalDateStr[0:25] + originalDateStr[28:]

    if dateStr[-1] != 'Z':
        dateStr = dateStr + 'Z'

    return datetime.datetime.strptime(dateStr, "%Y-%m-%dT%H:%M:%S.%fZ")

Interestingly, within PubSubBitsBadgeMessage's __init__, there is another striptime call that looks identical to the one in PubSubChannelPointsMessage. This use however does not seem to have raised any exceptions for me. But regardless, I still use my above parseDate() method with it and it seems to be fine.

charlesmadere commented 3 years ago

Here is a string that caused the exception to be thrown:

2021-10-21T05:46:31.5128275Z

And another one:

2021-10-21T01:24:49.5940343Z
chillymosh commented 3 years ago

This is due to Twitch not actually sticking to a set date format for everything, some return up to the seconds and some into milliseconds. We have already discussed this in the discord, which I recommend you join, and are looking to address it soon.

EvieePy commented 3 years ago

Thanks. This issue keeps popping up so I will take a look at it in the morning.

charlesmadere commented 3 years ago

this issue appears to be completely fixed as of release v2.1.0