[Streams] Minimize Twitch API calls #4938

Merged
PredaaA merged 7 commits from streams/twitch-minimize-api-calls into V3/develop 2021-04-05 22:10:42 +02:00
PredaaA commented 2021-04-05 13:53:30 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

The is_online method has been rewrote, it removes two uneeded API calls, first was /helix/users because it was called twice, and second was /helix/games to get game name, which is already existing in /helix/streams that we call too.

This PR depends off #4883.

### Type - [ ] Bugfix - [x] Enhancement - [ ] New feature ### Description of the changes The `is_online` method has been rewrote, it removes two uneeded API calls, first was `/helix/users` because it was called twice, and second was `/helix/games` to get game name, which is already existing in `/helix/streams` that we call too. This PR depends off #4883.
palmtree5 (Migrated from github.com) reviewed 2021-04-05 13:53:30 +02:00
Jackenmen (Migrated from github.com) requested changes 2021-04-05 16:24:43 +02:00
Jackenmen (Migrated from github.com) commented 2021-04-05 15:00:04 +02:00
            final_data.update(stream_data)
```suggestion final_data.update(stream_data) ```
Jackenmen (Migrated from github.com) commented 2021-04-05 15:42:40 +02:00

Before, this code ensured that follows data is not empty and it should continue to do that.
Also, let's use tuple unpacking to simplify this a bit.

            __, follows_data = await self.get_data(TWITCH_FOLLOWS_ENDPOINT, {"to_id": self.id})
            if follows_data:
                final_data["followers"] = follows_data["total"]
Before, this code ensured that follows data is not empty and it should continue to do that. Also, let's use tuple unpacking to simplify this a bit. ```suggestion __, follows_data = await self.get_data(TWITCH_FOLLOWS_ENDPOINT, {"to_id": self.id}) if follows_data: final_data["followers"] = follows_data["total"] ```
Jackenmen (Migrated from github.com) commented 2021-04-05 15:43:22 +02:00

This is meant to contain the API response:

            raise APIError(stream_data)
This is meant to contain the API response: ```suggestion raise APIError(stream_data) ```
Jackenmen (Migrated from github.com) commented 2021-04-05 15:32:06 +02:00

While this code minimizes the max amount of API calls made, it currently increases the minimum amount of API calls made - before it was just 1 API call to check if the stream is online and then fail but now it's first fetching user profile whether it would be required or not.

It would be great to first only fetch user profile if ID is not filled and later, only if stream turns out to be online, fetch the user profile if it weren't fetched earlier.

While this code minimizes the *max* amount of API calls made, it currently increases the *minimum* amount of API calls made - before it was just 1 API call to check if the stream is online and then fail but now it's first fetching user profile whether it would be required or not. It would be great to first only fetch user profile *if* ID is not filled *and* later, only if stream turns out to be online, fetch the user profile if it weren't fetched earlier.
Jackenmen (Migrated from github.com) reviewed 2021-04-05 21:46:21 +02:00
@ -337,73 +337,59 @@ class TwitchStream(Stream):
return None, {}
Jackenmen (Migrated from github.com) commented 2021-04-05 21:46:21 +02:00
            if user_profile_data is None:
                __, user_profile_data = await self._fetch_user_profile()

            final_data = dict.fromkeys(
                ("game_name", "followers", "login", "profile_image_url", "view_count")
            )

            if user_profile_data is not None:
                final_data["login"] = user_profile_data["login"]
                final_data["profile_image_url"] = user_profile_data["profile_image_url"]
                final_data["view_count"] = user_profile_data["view_count"]
```suggestion if user_profile_data is None: __, user_profile_data = await self._fetch_user_profile() final_data = dict.fromkeys( ("game_name", "followers", "login", "profile_image_url", "view_count") ) if user_profile_data is not None: final_data["login"] = user_profile_data["login"] final_data["profile_image_url"] = user_profile_data["profile_image_url"] final_data["view_count"] = user_profile_data["view_count"] ```
Jackenmen (Migrated from github.com) reviewed 2021-04-05 21:52:08 +02:00
@ -337,73 +337,59 @@ class TwitchStream(Stream):
return None, {}
Jackenmen (Migrated from github.com) commented 2021-04-05 21:51:41 +02:00

Sorry, that's my bad...

                user_profile_data = await self._fetch_user_profile()
Sorry, that's my bad... ```suggestion user_profile_data = await self._fetch_user_profile() ```
Jackenmen (Migrated from github.com) commented 2021-04-05 21:51:51 +02:00
        user_profile_data = None
```suggestion user_profile_data = None ```
Jackenmen (Migrated from github.com) commented 2021-04-05 21:51:56 +02:00
            user_profile_data = await self._fetch_user_profile()
```suggestion user_profile_data = await self._fetch_user_profile() ```
Jackenmen (Migrated from github.com) reviewed 2021-04-05 21:58:21 +02:00
@ -337,73 +337,59 @@ class TwitchStream(Stream):
return None, {}
Jackenmen (Migrated from github.com) commented 2021-04-05 21:58:12 +02:00

make_embed() takes use of a few more keys:

            final_data["user_name"] = self.name = stream_data["user_name"]
            final_data["game_name"] = stream_data["game_name"]
            final_data["thumbnail_url"] = stream_data["thumbnail_url"]
            final_data["title"] = stream_data["title"]
            final_data["type"] = stream_data["type"]
`make_embed()` takes use of a few more keys: ```suggestion final_data["user_name"] = self.name = stream_data["user_name"] final_data["game_name"] = stream_data["game_name"] final_data["thumbnail_url"] = stream_data["thumbnail_url"] final_data["title"] = stream_data["title"] final_data["type"] = stream_data["type"] ```
Jackenmen (Migrated from github.com) approved these changes 2021-04-05 22:10:34 +02:00
Jackenmen (Migrated from github.com) left a comment

Looks good now, thanks for PRing

Looks good now, thanks for PRing
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!4938
No description provided.