def get_access_token(): # url = "?grant_type=refresh_token&refresh_token=" + config.CLIENT_REFRESH_TOKEN + "&client_id=" + config.CLIENT_ID + "&client_secret=" + config.CLIENT_SECRET url = "https://id.twitch.tv/oauth2/token" data = urllib.parse.urlencode({ 'grant_type': 'refresh_token', 'refresh_token': config.CLIENT_REFRESH_TOKEN, 'client_id': config.CLIENT_ID, 'client_secret': config.CLIENT_SECRET }) data = data.encode('utf-8') # utility.print_toscreen(url) req = urllib.request.Request(url) try: response = urllib.request.urlopen(req, data=data) except (HTTPError, URLError) as err: debug.output_error(debug.lineno() + " - " + "HTTP Error: " + str(err)) utility.restart() data = json.load(response) global access_token access_token = data["access_token"] # utility.print_toscreen(data) # utility.print_toscreen(data["access_token"]) return data["access_token"]
def is_stream_live(channel_id): url = "https://api.twitch.tv/helix/streams?user_id=" + channel_id # utility.print_toscreen(url) req = urllib.request.Request(url, headers={ "Client-ID": config.CLIENT_ID, "Authorization": "Bearer " + access_token, }, data=None) response = urllib.request.urlopen(req) data = json.load(response) # utility.print_toscreen(data) try: result = data["data"][0]["type"] # utility.print_toscreen(resulqt) except IndexError as err: debug.output_error(debug.lineno() + " - " + "Index Error" + str(err)) # utility.print_toscreen("false") return False # utility.print_toscreen("true") return True
def create_clip(channel_id): url = "https://api.twitch.tv/helix/clips" data = urllib.parse.urlencode({ 'has_delay': 'false', 'broadcaster_id': channel_id, }) data = data.encode('utf-8') req = urllib.request.Request(url, headers={ "Client-ID": config.CLIENT_ID, "Authorization": "Bearer " + access_token, }, data=data) try: response = urllib.request.urlopen(req, data) except (HTTPError, URLError) as err: debug.output_error("HTTP Error: " + str(err) + debug.lineno()) return 0 data = json.load(response) # utility.print_toscreen(str(data)) return data["data"][0]["id"]
def is_there_clip(clip_id): url = "https://api.twitch.tv/helix/clips?id=" + clip_id req = urllib.request.Request(url, headers={ "Client-ID": config.CLIENT_ID, "Authorization": "Bearer " + access_token, }, data=None) try: response = urllib.request.urlopen(req) except (HTTPError, URLError) as err: debug.output_error(debug.lineno() + " - " + "HTTP Error: " + str(err)) utility.restart() data = json.load(response) # utility.print_toscreen(data) try: result = data["data"][0]["id"] except IndexError as err: debug.output_error(debug.lineno() + " - " + "Index Error: " + str(err)) return False # utility.print_toscreen("true") return True
def get_channel_id(channel_name): url = "https://api.twitch.tv/kraken/users/?api_version=5&client_id=" + config.CLIENT_ID + "&login="******"access_token"]) result = "0" try: result = data["users"][0]["_id"] except IndexError as err: debug.output_error(debug.lineno() + " - " + " Bad channel name: " + str(err)) return result return result
def bot_loop(): time.sleep(1) utility.print_toscreen("Starting Bot Loop") while connected: response = "" try: response = s.recv(1024).decode("utf-8") except Exception as e: debug.output_error(debug.lineno() + " - " + str(e)) utility.restart() # PING-PONG if re.search("PING :tmi.twitch.tv",response): try: s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) # utility.print_toscreen("Pong") # debug.output_debug ("PONG") utility.print_usertoscreen("server", "twitch", "ping") except IOError as e: debug.output_error(debug.lineno() + " - " + "PONG error " + str(e)) utility.restart() find_all_twitch = re.findall(TWITCH_MSG, response) for found in find_all_twitch: username = "" channel = "" message = "" try: message = response[response.find(found) + len(found):] start = re.search(TWITCH_MSG, message) if start: message = message[0:start.start()] else: message = message if re.search("PRIVMSG",found): channel = re.search(r"#\w+", found).group(0) username = re.search(r"\w+", found).group(0) utility.print_usertoscreen(channel, username, message.rstrip()) else: utility.print_usertoscreen("server", "twitch", message.strip()) message = message.lower().rstrip() except Exception as e: debug.output_error(debug.lineno() + " - " + str(e)) # clip | !clip if message == "!clip" or message == "clip" or message == "clip it": channel_id = CHAT_NAMES_TO_ID[channel] debug.output_debug(channel + " | " + username + ": " + message) clip_thread = threading.Timer(5, create_clip, args=[channel, channel_id, username]) clip_thread.start() # !Hey # if message == "!hey" or message == "hi" or message == "hey" or message == "hello" or message == "heyguys": # utility.chat(s, channel, "Hey " + username + ", Welcome to the stream!") # utility.print_toscreen(CHAT_NAMES_TO_ID[channel]) # !help if message == "!help": utility.chat(s, channel, "Hi, I'm the clipping bot. type \"clip\" or \"!clip\" in chat, I'll clip the last 25 sec and post the link.") if re.search(config.TWITCH_NICK, message): debug.output_debug(channel + " | " + username + ": " + message)
for channel_name in config.CHANNEL_NAMES: time.sleep(0.2) # Getting channels id from channels names channel_id = twitch.get_channel_id(channel_name) channel_name = "#" + channel_name CHAT_NAMES_TO_ID[channel_name] = str(channel_id) s.send("JOIN {}\r\n".format(channel_name).encode("utf-8")) utility.print_toscreen(channel_name + " : " + channel_id) connected = True # Socket succefully connected except Exception as e: debug.output_error(debug.lineno() + " - " + str(e)) connected = False # Socket failed to connect utility.restart() # BOT LOOP # -------- def bot_loop(): time.sleep(1) utility.print_toscreen("Starting Bot Loop") while connected: response = ""
def get_latest_bitcoin_price(): global last_price bold = "" price = 0 str_price = "" result_dict = dict() try: response = requests.get(BITCOIN_API_URL) response_json = response.json() price = response_json["last"] price = price.split(".")[0] open_price = response_json["open"] open_price = open_price.split(".")[0] result_dict["open_price"] = open_price high_price = response_json["high"] high_price = high_price.split(".")[0] result_dict["high_price"] = high_price low_price = response_json["low"] low_price = low_price.split(".")[0] result_dict["low_price"] = low_price volume = response_json["volume"] volume = volume.split(".")[0] result_dict["volume"] = volume str_price = format(int(price), ',d') result_dict["str_price"] = str_price price = price.translate({ord(i): None for i in ','}) price = int(price) result_dict["price"] = price except Exception as e: debug.output_error(e) if price > 0: diff = abs(price - last_price) if last_price > price: sign = "-" else: sign = "+" result_dict["sign"] = sign result_dict["diff"] = format(int(diff), ',d') date = datetime.utcnow() result_dict["date"] = date.strftime('%H:%M') last_price = price output_str = bold + str_price + " ( " + sign + " " + str(diff) + "\t)" # print(output_str) debug.output_debug(output_str) return result_dict