Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # encoding=utf8
- import logging
- import requests
- import socket
- import string
- import sys
- from time import sleep
- import lxml.html as html
- reload(sys)
- sys.setdefaultencoding('utf-8')
- HOST = "irc.chat.twitch.tv"
- NICK = "dj_denis97"
- PORT = 6667
- PASS = "" # Брать тут http://twitchapps.com/tmi/
- readbuffer = ""
- MODT = False
- game = "https://api.rtainc.co/twitch/channels/just4fun__/status?format=[1]"
- time = "https://api.rtainc.co/twitch/channels/just4fun__/uptime?format=%5B1%5D&units=2"
- # Подключаемся к IRC твича предавая ему данные, и подключаемся к указанному каналу
- s = socket.socket()
- s.connect((HOST, PORT))
- s.send("PASS " + PASS + "\r\n")
- s.send("NICK " + NICK + "\r\n")
- s.send("JOIN #just4fun__ \r\n")
- def get_mmr():
- headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
- r = requests.get('https://www.dotabuff.com/players/109462682', headers=headers)
- page = html.document_fromstring(r.text)
- return page.find_class('header-content-secondary')[0].text_content().split('h')[1].split('S')[0]
- def smart_time(h, m):
- result = 'Стрим идет '
- if h is not None:
- if h == 1:
- result += '1 час, '
- elif str(h)[0] == '2' or str(h)[0] == '3' or str(h) == '4':
- result = result + str(h) + ' часа, '
- else:
- result = result + str(h) + ' часов, '
- if str(m)[-1] == 1:
- result = result + str(m) + ' минуту.'
- elif str(m)[-1] in ['2', '3', '4']:
- result = result + str(m) + ' минуты.'
- elif m in [str(i) for i in range(5, 19)] or str(m)[-1] == '0' or str(m)[-1] in ['5', '6', '7', '8', '9']:
- result = result + str(m) + ' минут.'
- return result
- def get_game():
- url = 'https://api.twitch.tv/kraken/streams/just4fun__'
- header = {'Client-ID': 'b4dw5l0j92y6zvahdmwsury87akc5gp'}
- try:
- a = requests.get(url, headers=header).json()['stream']['game']
- except:
- a = requests.get(game).text
- return a
- # Method for sending a message
- def send_message(message):
- s.send("PRIVMSG #just4fun__ :" + message + "\r\n")
- print(message)
- while True:
- readbuffer = readbuffer + s.recv(1024)
- temp = string.split(readbuffer, "\n")
- readbuffer = temp.pop()
- for line in temp:
- # Проверка на сообщение PING от твича, проверка на афк
- if "PING" in line:
- logging.info(line)
- logging.info(line.split(" :")[1])
- logging.info(type(line))
- s.send("PONG %s\r\n" % line.split(" :")[1])
- else:
- # Разбиваем строку, для того что бы было проще с ней работать
- parts = string.split(line, ":")
- if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
- try:
- message = parts[2][:len(parts[2]) - 1]
- except:
- message = ""
- usernamesplit = string.split(parts[1], "!")
- username = usernamesplit[0]
- # Магия, не трогать
- if MODT:
- print username + ": " + message
- # Тут добавляем команды
- if message == "!игра" or message == "!game":
- sleep(0.3)
- send_message("Текущая игра : " + get_game())
- if message in ['!время', '!up', '!time', '!uptime']:
- r = requests.get(time) # Делаем запрос к API и получем продолжительность стрима
- if r.text == "just4fun__ isn't currently streaming":
- sleep(0.3)
- send_message("В данный момент стрим оффлайн OpieOP")
- else:
- if r.text.split(' ')[1] == 'minutes,' or r.text.split(' ')[1] == 'minute,':
- send_message(smart_time(None, r.text.split(' ')[0]))
- else:
- send_message(smart_time(r.text.split(' ')[0], r.text.split(' ')[2]))
- if message == '!mmr' or message == '!ммр':
- send_message('Текущий ММР ' + get_mmr() + ' поинтов')
- for l in parts:
- if "End of /NAMES list" in l:
- MODT = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement