def __init__(self): """Initialize the bot. Initialize logging and all clients. Get reference to event loop. Load stream and role id files. """ self.logger = self.init_logger() self.loop = asyncio.get_event_loop() # initialize clients for discord, twitch, database self.discord = discord.Client(loop=self.loop) self.loop.run_until_complete( self.discord.login(constants.DISCORD_TOKEN)) self.twitch = twitch.TwitchClient(client_id=constants.TWITCH_ID) self.db = dataset.connect(constants.DB_NAME) self.table = self.db[constants.TABLE_NAME] stream_ids = self.get_db_streams() +\ self.load_file(constants.STREAM_IDS_FILE) self.stream_ids_map = {stream_id: None for stream_id in stream_ids} """dict of str to str or None: map of stream ids to discord display names or None if no discord account is linked to that stream. """ #: list of str: list of role ids that members must have one or more of self.role_ids = self.load_file(constants.ROLE_IDS_FILE) self.logger.debug('INITIALIZED')
def timestamp_and_dl(id_of_vod, type_of_vod, filename): print("Downloading " + filename) ydl_opts = { 'format': 'best', 'fixup': 'never', 'outtmpl': filename, } if type_of_vod == "twitch": twitch_client = twitch.TwitchClient( client_id='a57grsx9fi8ripztxn8zbxhnvek4cp') vodinf = twitch_client.videos.get_by_id(id_of_vod) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([vodinf.get('url')]) return vodinf.get('created_at').timestamp() elif type_of_vod == "youtube": url = "https://youtube.com/watch?v=" + id_of_vod with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) broadcast_time = youtube.get_broadcast(id_of_vod).timestamp() if time.daylight == 0: processed_time = time.timezone + broadcast_time else: processed_time = +time.altzone + broadcast_time return processed_time else: return None
async def call(self, channel, **kwargs): if not self._twitchClient: self._twitchClient = twitch.TwitchClient(self.config) #cache our client so we don't have to authenticate every time we access the twitch API game = self._twitchClient.getChannelGame(channel) #Uses our twitch integration to figure out the game that is being played if game: await self.respond(self.msg.author.mention+" is streaming **"+game+"** at https://www.twitch.tv/"+channel) #guessing the channel link usually works pretty well else: #This should never happen now that we are checking the channel page instead of the stream. If it does, the error is in Twitches API so our error message is not helpful anyway. await self.respond("That channel appears to be offline right now. Try again in a minute.") #Since the API can take a few minutes to update we tell the user to be patient instead of just telling them off
def timestamp_and_dl(id_of_vod, type_of_vod, filename): print("Downloading " + filename) ydl_opts = { 'format': 'best', 'fixup': 'never', 'outtmpl': filename, } if type_of_vod == "twitch": twitch_client = twitch.TwitchClient(client_id='a57grsx9fi8ripztxn8zbxhnvek4cp') vodinf = twitch_client.videos.get_by_id(id_of_vod) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([vodinf.get('url')]) return vodinf.get('created_at').timestamp() else: return 0
def init(self): self.worker = gevent.spawn(self.poll_loop) self.hosted = None # None means unknown. config.channel means no host. otherwise, name of currently hosted self.api = twitch.TwitchClient()
import twitch, twitter, sys import auth client = twitch.TwitchClient(auth.twitch_client_id) channel = client.channels.get_by_id(auth.channel_id) status = channel.status """ channel members: mature status broadcaster_language broadcaster_software display_name game language id name created_at updated_at partner logo video_banner profile_banner profile_banner_background_color url views followers broadcaster_type description private_video privacy_options_enabled """
import twitch import urllib.request import yaml import datetime from time import sleep config = None with open('config.yml') as stream: try: config = yaml.safe_load(stream) except yaml.YAMLError as e: print("Error parsing yaml config file: {}".format(e)) client_id = config['client_id'] client = twitch.TwitchClient(config['client_id']) clips = client.clips channel = 'stroopc' basepath = '/mnt/e/src/adobe/premiere/twitch/twitch_clips' # hash of vod_id's to their creation date. Saves looking up the date of the same # vod several times if there are several clips from the same vod vod_infos = {} def dl_progress(count, block_size, total_size): percent = int(count * block_size * 100 / total_size) sys.stdout.write("\r...%d%%" % percent) sys.stdout.flush()
def create_client(client_id, client_secret): client = twitch.TwitchClient(client_id=client_id) return client
import twitch import youtube_dl import tbapi import time as libtime import subprocess import os twitch_client_id = 'a57grsx9fi8ripztxn8zbxhnvek4cp' twitch_client = twitch.TwitchClient(client_id=twitch_client_id) def get_video_info(id): vodinf = twitch_client.videos.get_by_id(id) created_at = vodinf.get('created_at') # print('created at', created_at) length_sec = vodinf.get('length') print('length', length_sec) return { 'start_time': created_at.timestamp(), 'length_sec': length_sec, 'end_time': created_at.timestamp() + length_sec } def download_twitch(id, outfile): import os exists = os.path.isfile(outfile) if exists: print("File exists " + outfile) else: print("Downloading " + outfile)
def init(self): self.api = twitch.TwitchClient(oauth=self.config.oauth, client_id=self.config.client_id)
#https://python-twitch-client.readthedocs.io/en/latest/v5/users.html import configparser import twitch from time import sleep config = configparser.ConfigParser() config.read('config.ini') print(config['credentials']['user']) print(config['credentials']['client_id']) print(config['credentials']['oauth_token']) client = twitch.TwitchClient(client_id = config['credentials']['client_id']) user = client.users.translate_usernames_to_ids(config['credentials']['user']) channel = client.channels.get_by_id(44322889) print(client.channels.check_subscription_by_user(44322889,user.id)) print(channel.id) print(channel.name) print(channel.display_name) while true: sleep(1)
def __init__(self, client_id, uid, channel_id): self.uid = uid self.channel_id = channel_id self._user_follows_cache = None self._client = twitch.TwitchClient(client_id=client_id)