Twitch API - PetterKraabol/Twitch-Python GitHub Wiki

Usage

# Imports

import twitch
import twitch.helix as helix
import twitch.v5 as v5
# New Twitch API (Helix)
helix_api = twitch.Helix(client_id: str,
                         use_cache: bool = False,
                         cache_duration: timedelta = timedelta(minutes=30),
                         rate_limit: int = 30)
 
# Twitch API v5
v5_api = twitch.V5(client_id: str,
                   rate_limit: int = None,
                   use_cache: bool = False,
                   cache_duration: timedelta = timedelta(minutes=30))
# Users
# https://dev.twitch.tv/docs/api/reference/#get-users

# Get users by name or id. Can be comma separated, lists and tuples.
users: helix.Users = helix_api.users(445588, ['sodapoppin', 'drdisrespect'], 'b0aty')
user: helix.User = helix_api.user('zarlach')
# Videos
# https://dev.twitch.tv/docs/api/reference/#get-videos

# Get videos by comma separated ids 
videos: helix.Videos = helix_api.videos([445588, 334466])
videos: helix.Videos = helix_api.videos(user_id='sodapoppin', first=10)
video: helix.Video = helix_api.video(445588)

# Comments

# Get comments from video ID
comments: v5.Comments = v5_api.comments(445588)

Caching

To avoid repeating API calls, caching can be enabled to to increase speed and reduce the request rate.

from datetime import timedelta

import twitch

helix = twitch.Helix('client-id', use_cache=True, cache_duration=timedelta(minutes=10))