Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active January 30, 2024 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallentx/24d5a78ec3bf20b3e636a168e228f9f5 to your computer and use it in GitHub Desktop.
Save wallentx/24d5a78ec3bf20b3e636a168e228f9f5 to your computer and use it in GitHub Desktop.
List twitch streams that have drops enabled for a given --game_id (defaults to BattleBit Remastered game_id)
import argparse
import subprocess
import json
import re
# You need to have the `twitch-cli` installed and configured
# Define ANSI color codes
colors = {
"red": "\033[0;31m",
"lightblue": "\033[38;5;39m",
"bg_gray": "\033[0;100m",
"purple": "\033[0;35m",
"gray": "\033[38;5;237m\u001b[4m",
"yellow": "\033[0;33m",
"pink": "\033[38;5;200m",
"lime": "\033[38;5;77m",
"cyan": "\033[0;36m",
"orange": "\033[38;5;202m",
"blue": "\033[0;34m",
"reset": "\033[0m",
}
# Set up argument parsing
parser = argparse.ArgumentParser(
description="Fetch Twitch streams with Drops enabled for a specific game."
)
parser.add_argument(
"--game_id", type=str, default="496916", help="Game ID for which to fetch streams"
)
args = parser.parse_args()
# Use the specified game_id or the default
game_id = args.game_id
# Run the Twitch CLI command (default game_id is BattleBit Remastered - 496916)
twitch_cli_command = [
"twitch-cli",
"api",
"get",
"streams",
"-q",
"type=live",
"-q",
f"game_id={game_id}",
]
process = subprocess.run(twitch_cli_command, capture_output=True, text=True)
# Check for errors
if process.returncode != 0:
print(f"Error: {process.stderr}")
exit(1)
# Load the JSON response
try:
data = json.loads(process.stdout)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
exit(1)
# Function to apply color with yellow reset after each pattern
def apply_color(match, color):
return f"{colors[color]}{match.group(0)}{colors['yellow']}"
print("🫳🪂📦 Drops Enabled for:\n")
# Process each stream entry
for stream in data["data"]:
if "DropsEnabled" in stream.get("tags", []):
# User Name
print(colors["purple"] + stream["user_name"] + colors["reset"])
# Title with colorization
title = stream["title"]
title = re.sub(
r"(\[.*?\]|\{.*?\}|\(.*?\)|<.*?>)",
lambda m: colors["pink"]
+ m.group(0)[0]
+ colors["lime"]
+ m.group(0)[1:-1]
+ colors["pink"]
+ m.group(0)[-1]
+ colors["yellow"],
title,
)
title = re.sub(
r"(^|[^!])!(\w+)",
lambda m: m.group(1)
+ colors["cyan"]
+ "!"
+ colors["orange"]
+ m.group(2)
+ colors["yellow"],
title,
)
title = re.sub(
r"(^|[^@])@(\w+)",
lambda m: m.group(1)
+ colors["lightblue"]
+ "@"
+ m.group(2)
+ colors["yellow"],
title,
)
title = re.sub(
r"(^|[^#])#(\w+)",
lambda m: m.group(1)
+ colors["bg_gray"]
+ "#"
+ m.group(2)
+ colors["yellow"],
title,
)
title = re.sub(
r" -{1,2} ", lambda m: colors["red"] + " - " + colors["yellow"], title
)
title = re.sub(r"\|", lambda m: colors["red"] + "|" + colors["yellow"], title)
# Print the title
print(colors["yellow"] + title + colors["reset"])
# URL
url = f"https://twitch.tv/{stream['user_login']}"
print(colors["gray"] + url + colors["reset"])
# Separator line
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment