def main(): parser = argparse.ArgumentParser(description="Twitch notifications") parser.add_argument("-c", "--channel", default="#video", help="Slack channel") parser.add_argument( "-n", "--notifications", default="#ci", help="Slack channel notifications for errors or warnings") parser.add_argument("-i", "--interval", default=30, help="Update interval in seconds", type=int) parser.add_argument("-u", "--users", default="", help="Twitch users") args = parser.parse_args() remember_online_users = [] TWITCH_CLIENT_ID = os.environ["TWITCH_CLIENT_ID"] TWITCH_CLIENT_SECRET = os.environ["TWITCH_CLIENT_SECRET"] def bearer(): global twitch_access_token_expires_in global twitch_access_token if (twitch_access_token_expires_in < (60 * 15) and not twitch_access_token): connection = http.client.HTTPSConnection("id.twitch.tv") connection.request( "POST", "/oauth2/token?client_id=" + TWITCH_CLIENT_ID + "&client_secret=" + TWITCH_CLIENT_SECRET + "&grant_type=client_credentials") response = json.loads( connection.getresponse().read().decode('utf8')) twitch_access_token = response['access_token'] twitch_access_token_expires_in = int(response['expires_in']) return twitch_access_token while (True): twitch = Helix(client_id=TWITCH_CLIENT_ID, client_secret=TWITCH_CLIENT_SECRET, bearer_token=bearer()) slack = WebClient(token=os.environ["SLACK_API_KEY"]) try: for user in args.users.split(","): if twitch.user(user).is_live: print("TWITCH_LIVE: {}".format(user)) if user not in remember_online_users: remember_online_users.append(user) slack.chat_postMessage( channel=args.channel, attachments=[{ "text": "TWITCH_LIVE: https://www.twitch.tv/{}".format( user), "color": "#ff0000" }]) else: if user in remember_online_users: remember_online_users.remove(user) except Exception as exception: print(exception) slack.chat_postMessage( channel=args.notifications, attachments=[{ "text": "ERROR: reevefresh: Failed to fetch Twitch users status", "color": "#ff0000" }]) print("Online users: {}\n".format(",".join(remember_online_users))) time.sleep(args.interval)
def __get_user(self, api: Helix, nick_or_id: Union[str, int]) -> Optional[User]: return api.user(nick_or_id)
# --- API key --- helix = Helix(client_id=cfg.CLIENT_ID, bearer_token=cfg.BEARER_TOKEN, use_cache=True) # --- Argument parser --- parser = argparse.ArgumentParser(description="View VOD comments and search through them.") parser.add_argument("-u", "--user", help="desired user to view") parser.add_argument("-c", "--count", help="number of most recent vods to view", type=int, default=1) parser.add_argument("-s", "--search", help="term to search for in vods") parser.add_argument("-t", "--realtime", help="include real-world time comments were posted at", action="store_true") args = parser.parse_args() # --- Main stuff --- user = helix.user(args.user if args.user else input('User: '******'Number of VODs to search: ')) vods = user.videos(first=vodCount) searchTerm = args.search if args.search else input('Search for: ') showRealTime = args.realtime def viewVod(vod, searchTerm=searchTerm, showRealTime=showRealTime): print('-'*60) print(vod) print('-'*60) totalComments = 0 if searchTerm: totalComments = 0 for c in vod.comments: if re.search(searchTerm, c.message.body):
from importlib import import_module from os import O_PATH from rx.core.typing import Observer # 3rd party from twitch import Chat, Helix from twitch.chat import Message # local from .. import config from ..web import app from .commands import * message_handlers = [] # Twitch API client helix = Helix(config.CLIENT_ID, config.CLIENT_SECRET) user = helix.user(config.USERNAME) for mod in config.HANDLERS: module = import_module(f'..{mod}.bot', __name__) message_handlers.append(getattr(module, 'handle_message')) class MessageHandler(Observer): "Message handling Observer" def on_completed(_): pass def on_error(_): pass