Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ClientType into Twitch class as an attribute #437

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

guihkx
Copy link
Contributor

@guihkx guihkx commented Feb 13, 2024

This allows us to use the base URL as specified in ClientInfo for requests made to Twitch.

Previously, we'd hardcode www.twitch.tv as the base URL to make channel-related requests, but because we're currently spoofing the mobile client, that would always cause a HTTP redirect to m.twitch.tv.

This change avoids that HTTP redirect, and potentially saves a whopping 150 ms (or more 😱) when making channel-related requests.

Before:

Screenshot_20240123_032830

After:

Screenshot_20240123_032608

Copy link
Owner

@DevilXD DevilXD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I see this can be done slightly different though, while also cleaning up the code a little.

@DevilXD DevilXD added the Enhancement New feature or request label Feb 13, 2024
@guihkx
Copy link
Contributor Author

guihkx commented Feb 13, 2024

My Python skills are basically none, so please bear with me. 😅

In any case, let me know if it looks good now.

@DevilXD
Copy link
Owner

DevilXD commented Feb 13, 2024

Hey, no worries, you've got it right 🙂

Although I didn't really think through the logistics of the new attribute, and it would be best to avoid repacking it like that. I didn't really want to bother you with this, but to avoid repacking the object, the entire thing had to be moved from module-level to inside the class, rewriting all existing references, so I just pushed a new commit to address it. If you want to, you can incorporate it into yours, or edit the commit message, I didn't really know how to name it 😅

@guihkx
Copy link
Contributor Author

guihkx commented Feb 13, 2024

No worries, thank you for implementing it for me!

However, I just tested your latest changes, and I'm getting an exception:

13:26:28: Watching: Rainbow6
13:26:28: INFO: Websocket[1] connecting...
13:26:28: INFO: Websocket[2] connecting...
13:26:28: INFO: Websocket[3] connecting...
13:26:28: INFO: Websocket[4] connecting...
13:26:28: INFO: Websocket[5] connecting...
13:26:28: INFO: Websocket[6] connecting...
13:26:28: INFO: Websocket[7] connecting...
13:26:28: ERROR: Exception in task
13:26:28: Traceback (most recent call last):
13:26:28:   File "/home/gui/dev/TwitchDropsMiner/utils.py", line 139, in wrapper
13:26:28:     await afunc(*args, **kwargs)
13:26:28:   File "/home/gui/dev/TwitchDropsMiner/twitch.py", line 1047, in _watch_loop
13:26:28:     succeeded: bool = await channel.send_watch()
13:26:28:                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
13:26:28:   File "/home/gui/dev/TwitchDropsMiner/channel.py", line 379, in send_watch
13:26:28:     self._spade_url = await self.get_spade_url()
13:26:28:                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
13:26:28:   File "/home/gui/dev/TwitchDropsMiner/channel.py", line 231, in get_spade_url
13:26:28:     async with self._twitch.request("GET", self.url) as response:
13:26:28:                                            ^^^^^^^^
13:26:28:   File "/home/gui/dev/TwitchDropsMiner/channel.py", line 154, in url
13:26:28:     return URLType(f"{self._twitch.client_info.CLIENT_URL}/{self._login}")
13:26:28:                       ^^^^^^^^^^^^^^^^^^^^^^^^
13:26:28: AttributeError: 'Twitch' object has no attribute 'client_info'

@DevilXD
Copy link
Owner

DevilXD commented Feb 13, 2024

Ah, right 😓 The attribute name is _client_type instead of client_info.

This allows us to use the base URL as specified in ClientInfo for
requests made to Twitch.

Previously, we'd hardcode "www.twitch.tv" as the base URL to make
channel-related requests, but because we're currently spoofing the
mobile client, that would always cause a HTTP redirect to m.twitch.tv.

This change avoids that HTTP redirect, and potentially saves a whopping
150 ms (or more 😱) when making channel-related requests.

Co-authored-by: DevilXD <[email protected]>
@guihkx guihkx changed the title Use m.twitch.tv to load the channel page Move ClientType into Twitch class as an attribute Feb 14, 2024
@guihkx
Copy link
Contributor Author

guihkx commented Feb 14, 2024

Thank you! I've squashed your commit into mine and added you as a co-author.

I'm using your original commit title, and I tweaked its description.

Hopefully it's good enough!

Copy link
Owner

@DevilXD DevilXD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you for your contribution 😊

@DevilXD DevilXD merged commit dbd3cd8 into DevilXD:master Feb 14, 2024
4 checks passed
@guihkx guihkx deleted the use-m-twitch-tv branch February 14, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants