Skip to content

Latest commit

 

History

History
124 lines (96 loc) · 3.21 KB

python.md

File metadata and controls

124 lines (96 loc) · 3.21 KB

Python — Twitch Scraper

Call the hosted Twitch Scraper Actor from Python using the official apify-client. All code here is client-side — it calls the Actor on the Apify platform.

Install

pip install apify-client

Set up the client

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])  # your Apify API token

Run a mode and read the dataset

# Sweep the top games for live streams (high volume)
run = client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "topStreams",
    "gameFanout": 30,
    "gameSort": "VIEWER_COUNT",
    "maxResults": 500,
})

items = list(client.dataset(run["defaultDatasetId"]).iterate_items())
print(f"Got {len(items)} rows")
print(items[0])

Batch channel / streamer profiles (lead-gen)

run = client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "user",
    "logins": ["shroud", "pokimane", "xqc", "ninja"],
})

for ch in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(f'{ch["displayName"]} — {ch.get("followers")} followers — live: {ch.get("isLive")}')

Live streams for one or more games

run = client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "streamsByGame",
    "games": ["VALORANT", "League of Legends", "Just Chatting"],
    "maxResults": 1000,
})

streams = list(client.dataset(run["defaultDatasetId"]).iterate_items())

# Rank live streams by current viewers
top = sorted(streams, key=lambda s: s.get("viewersCount") or 0, reverse=True)[:10]
for s in top:
    print(f'{s["displayName"]}: {s.get("viewersCount")} — {s.get("title")}')

Top games, VODs, clips and search

# Top games ranked by viewers
client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "topGames",
    "gameSort": "VIEWER_COUNT",
    "maxResults": 100,
})

# A channel's VODs, newest first
client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "videos",
    "login": "shroud",
    "videoSort": "TIME",
})

# A channel's top clips, last 7 days
client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "clips",
    "login": "shroud",
    "clipPeriod": "LAST_WEEK",
    "clipSort": "VIEWS_DESC",
})

# Search channels by keyword
client.actor("logiover/twitch-scraper").call(run_input={
    "mode": "search",
    "query": "valorant",
    "searchIndex": "CHANNEL",
})

Export to CSV with pandas

import pandas as pd

items = list(client.dataset(run["defaultDatasetId"]).iterate_items())
df = pd.DataFrame(items)
df.to_csv("twitch.csv", index=False)
print(df.head())

Only fetch specific fields

page = client.dataset(run["defaultDatasetId"]).list_items(
    fields=["_mode", "login", "displayName", "followers", "liveViewers", "twitchUrl"],
    clean=True,
)
print(page.items)

Notes

  • .call(run_input=...) starts the run and waits for it to finish; use .start(run_input=...) for fire-and-forget.
  • iterate_items() streams the whole dataset without loading it all into memory at once.
  • Every row carries a _mode field so multiple modes can be merged into one dataset.
  • Docs: https://docs.apify.com/api/client/python/