Last active
November 14, 2017 18:14
-
-
Save streetgt/e76c709c492da260fa447d99782196db to your computer and use it in GitHub Desktop.
Twitch Group Chat - Inviter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Python bot for mass invite users to a twitch group channel | |
# You need oauth_token for use this script, also you need to be chat room owner to invite | |
import requests,sys | |
def fetch_channel_users(channel_name): | |
# URL for fetch the users from a given channel | |
url = 'https://tmi.twitch.tv/group/user/{0}/chatters'.format(channel_name) | |
# GET request | |
r = requests.get(url) | |
# We get the viewers as an array | |
users = r.json()["chatters"]["viewers"] | |
# If we don't fetch any user | |
if len(users) == 0: | |
return 0 | |
return users | |
def invite_users(session, users, oauth_token, channel_name): | |
# Check if any user is left in array | |
if len( users ) == 0: | |
print 'Twitch Group Chat - Inviter: Finished!' | |
return [] | |
# Pop a username from the users array | |
username = users.pop() | |
# Url from the API | |
url = 'https://chatdepot.twitch.tv/room_memberships?oauth_token={0}'.format(oauth_token) | |
# Payload body we need to send | |
payload = { | |
'username': username, | |
'irc_channel': channel_name | |
} | |
# Makes the post request | |
r = session.post(url, data=payload) | |
# If the request was made with success | |
if r.status_code == 200: | |
print 'Twitch Group Chat - Inviter: {0} invited with success!'.format(username) | |
return invite_users(session, users, oauth_token, invite_channel_name) | |
if __name__ == '__main__': | |
# Your chat API oauth token | |
oauth_token = '' | |
# Fetch channel name, ex: datguylirk | |
fetch_channel_name = '' | |
# Channel you want invite to, ex: _userexample_2323345 | |
# A group chat room stats with a "_" | |
invite_channel_name = '' | |
# Fetch the current online users in the chat | |
users = fetch_channel_users(fetch_channel_name) | |
# Check if exists users | |
if users == 0: | |
print "Twitch Group Chat - Inviter: No users online" | |
sys.exit(1) | |
# Creates a new session | |
session = requests.Session() | |
# Recurvise invite the fetched users | |
invite_users(session, users, oauth_token, invite_channel_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment