forked from Poorchop/hexchat-scripts
/
twitch-title.py
130 lines (108 loc) · 3.73 KB
/
twitch-title.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import requests
import sys
import threading
import hexchat
__module_name__ = "Twitch Title"
__module_author__ = "PDog"
__module_version__ = "0.1"
__module_description__ = "Display stream status and description for TwitchTV streams"
# TODO: Clean up thread handling <PDog>
# TODO: Figure out why get_current_status() sometimes doesn't print updated status <PDog>
t = None
class StreamParser:
def __init__(self, channel):
self.url = "https://api.twitch.tv/kraken/streams?"
self.channel = channel
self.twitch_chans = []
self.status = ""
self.display_name = ""
self.game = ""
self.title = ""
def set_topic(self):
"""
Set the channel topic (no formatting) and print the topic locally with formatting
"""
msg = "\00318{0}\00399 - {1} | Now playing: \00318{2}\00399 | {3}"\
.format(self.display_name, self.status, self.game, self.title)
hexchat.prnt(msg)
# HexChat doesn't support hiding characters in the topic bar (Windows), so strip the formatting until it's fixed
if sys.platform == "win32":
msg = hexchat.strip(msg, -1, 3)
if hexchat.get_info("topic") != msg:
hexchat.command("RECV :Topic!Topic@twitch.tv TOPIC #{0} :{1}".format(self.channel, msg))
def get_twitch_channels(self):
"""
Get a list of open TwitchTV channels and store them in self.twitch_chans
"""
self.twitch_chans = []
for chan in hexchat.get_list("channels"):
if chan.type == 2 and chan.context.get_info("host") == "irc.twitch.tv":
self.twitch_chans.append(chan.channel)
def update_status(self):
"""
Check the status of open channels
"""
if self.twitch_chans:
for chan in self.twitch_chans:
self.channel = chan[1:]
self.get_stream_info()
self.set_topic()
else:
pass
def get_stream_info(self):
"""
Get the stream information
"""
params = {"channel": self.channel}
r = requests.get(self.url, params=params)
data = r.json()
self.display_name = self.channel
self.game = ""
self.title = "\035Stream is offline\017"
if not data["streams"]:
self.status = "\00320\002OFFLINE\002\00399"
else:
self.status = "\00319\002LIVE\002\00399"
self.display_name = data["streams"][0]["channel"]["display_name"]
self.game = data["streams"][0]["channel"]["game"]
self.title = data["streams"][0]["channel"]["status"]
def is_twitch():
server = hexchat.get_info("host")
if server and "twitch.tv" in server:
return True
else:
return False
def get_current_status():
"""
Update the stream status every 10 minutes
"""
global t
parser = StreamParser(channel=None)
parser.get_twitch_channels()
parser.update_status()
t = threading.Timer(600, get_current_status)
t.daemon = True
t.start()
def join_cb(word, word_eol, userdata):
"""
Set the topic immediately after joining a channel
"""
if is_twitch():
channel = hexchat.get_info("channel")[1:]
parser = StreamParser(channel=channel)
parser.get_stream_info()
parser.set_topic()
return hexchat.EAT_NONE
def unload_cb(userdata):
"""
These appear to be necessary to prevent HexChat from crashing
on quit while a thread is active in Python
"""
global t
t.cancel()
t.join()
get_current_status()
hexchat.hook_print("Open Context", join_cb)
hexchat.hook_unload(unload_cb)
hexchat.prnt(__module_name__ + " version " + __module_version__ + " loaded")