Skip to content

Commit 2475339

Browse files
mumixamflashcode
authored andcommitted
twitch.py 1.1: changed hook_modifier from "irc_in_WHISPER" to "irc_in2_WHISPER"
changed hook_process_hashtable("url:") to hook_url use rawstrings to keep invalid escapes from throwing warnings
1 parent d4ea486 commit 2475339

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

python/twitch.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# SPDX-FileCopyrightText: 2014-2024 mumixam <[email protected]>
1+
# SPDX-FileCopyrightText: 2014-2025 mumixam <[email protected]>
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4-
#
54
# This program is free software; you can redistribute it and/or modify
65
# it under the terms of the GNU General Public License as published by
76
# the Free Software Foundation; either version 3 of the License, or
@@ -35,6 +34,10 @@
3534
# plugins.var.python.twitch.token (default: "")
3635
#
3736
# # History:
37+
# 2025-09-23,
38+
# v1.1: changed hook_modifier from "irc_in_WHISPER" to "irc_in2_WHISPER" -mumixam
39+
# changed hook_process_hashtable("url:") to hook_url -mumixam
40+
# use rawstrings to keep invalid escapes from throwing warnings -zer0def
3841
#
3942
# 2024-06-29, mumixam + stacyharper
4043
# v1.0: eval client_id and token expressions so that /secure can be used
@@ -69,7 +72,7 @@
6972

7073
SCRIPT_NAME = "twitch"
7174
SCRIPT_AUTHOR = "mumixam"
72-
SCRIPT_VERSION = "1.0.1"
75+
SCRIPT_VERSION = "1.1"
7376
SCRIPT_LICENSE = "GPL3"
7477
SCRIPT_DESC = "twitch.tv Chat Integration"
7578
OPTIONS={
@@ -89,6 +92,7 @@
8992
import time
9093
import string
9194
import ast
95+
import re
9296

9397
curlopt = {
9498
"httpheader": "\n".join([
@@ -127,8 +131,7 @@ def twitch_main(data, buffer, args):
127131
if not (server in OPTIONS['servers'].split() and type == 'channel'):
128132
return weechat.WEECHAT_RC_OK
129133
url = 'https://api.twitch.tv/helix/streams?user_login=' + username
130-
weechat.hook_process_hashtable(
131-
"url:" + url, curlopt, 7 * 1000, "stream_api", buffer)
134+
weechat.hook_url(url, curlopt, 7 * 1000, "stream_api", buffer)
132135
return weechat.WEECHAT_RC_OK
133136

134137

@@ -138,17 +141,15 @@ def makeutf8(data):
138141
data=str(data,'utf8')
139142
return data
140143

141-
142-
def stream_api(data, command, rc, stdout, stderr):
144+
def stream_api(data, url, options, output):
143145
try:
144-
jsonDict = json.loads(stdout.strip())
146+
jsonDict = json.loads(output['output'].strip())
145147
except Exception as e:
146148
weechat.prnt(data, '%stwitch.py: error communicating with twitch api' % weechat.prefix('error'))
147149
if OPTIONS['debug']:
148-
weechat.prnt(data,'%stwitch.py: return code: %s' % (weechat.prefix('error'),rc))
149-
weechat.prnt(data,'%stwitch.py: stdout: %s' % (weechat.prefix('error'),stdout))
150-
weechat.prnt(data,'%stwitch.py: stderr: %s' % (weechat.prefix('error'),stderr))
151-
weechat.prnt(data,'%stwitch.py: exception: %s' % (weechat.prefix('error'),e))
150+
weechat.prnt(data,'%stwitch.py: response code: %s' % (weechat.prefix('error'),output['response_code']))
151+
weechat.prnt(data,'%stwitch.py: headers: %s' % (weechat.prefix('error'),output['headers']))
152+
weechat.prnt(data,'%stwitch.py: output: %s' % (weechat.prefix('error'),output['output']))
152153
return weechat.WEECHAT_RC_OK
153154
currentbuf = weechat.current_buffer()
154155
title_fg = weechat.color(
@@ -168,7 +169,7 @@ def stream_api(data, command, rc, stdout, stderr):
168169
if not 'data' in jsonDict.keys():
169170
weechat.prnt(data, 'twitch.py: Error with twitch API (data key missing from json)')
170171
if OPTIONS['debug']:
171-
weechat.prnt(data, 'twitch.py: %s' % stdout.strip())
172+
weechat.prnt(data, 'twitch.py: %s' % output['output'].strip())
172173
return weechat.WEECHAT_RC_OK
173174
if not jsonDict['data']:
174175
line = "STREAM: %sOFFLINE%s %sCHECKED AT: (%s)" % (
@@ -232,23 +233,20 @@ def stream_api(data, command, rc, stdout, stderr):
232233
weechat.buffer_set(data, "title", output)
233234
if game_id is not None and not game_id in gameid_cache:
234235
url = 'https://api.twitch.tv/helix/games?id=' + game_id
235-
weechat.hook_process_hashtable(
236-
"url:" + url, curlopt, 7 * 1000, "game_api", data)
236+
weechat.hook_url(url, curlopt, 7 * 1000, "game_api", data)
237237

238238
return weechat.WEECHAT_RC_OK
239239

240240

241-
242-
def game_api(data, command, rc, stdout, stderr):
241+
def game_api(data, url, options, output):
243242
try:
244-
jsonDict = json.loads(stdout.strip())
243+
jsonDict = json.loads(output['output'].strip())
245244
except Exception as e:
246245
weechat.prnt(data, '%stwitch.py: error communicating with twitch api' % weechat.prefix('error'))
247246
if OPTIONS['debug']:
248-
weechat.prnt(data,'%stwitch.py: return code: %s' % (weechat.prefix('error'),rc))
249-
weechat.prnt(data,'%stwitch.py: stdout: %s' % (weechat.prefix('error'),stdout))
250-
weechat.prnt(data,'%stwitch.py: stderr: %s' % (weechat.prefix('error'),stderr))
251-
weechat.prnt(data,'%stwitch.py: exception: %s' % (weechat.prefix('error'),e))
247+
weechat.prnt(data,'%stwitch.py: response code: %s' % (weechat.prefix('error'),output['response_code']))
248+
weechat.prnt(data,'%stwitch.py: headers: %s' % (weechat.prefix('error'),output['headers']))
249+
weechat.prnt(data,'%stwitch.py: output: %s' % (weechat.prefix('error'),output['output']))
252250
return weechat.WEECHAT_RC_OK
253251

254252
if 'data' in jsonDict.keys():
@@ -265,17 +263,15 @@ def game_api(data, command, rc, stdout, stderr):
265263
return weechat.WEECHAT_RC_OK
266264

267265

268-
269-
def channel_api(data, command, rc, stdout, stderr):
266+
def channel_api(data, url, options, output):
270267
try:
271-
jsonDict = json.loads(stdout.strip())
268+
jsonDict = json.loads(output['output'].strip())
272269
except Exception as e:
273270
weechat.prnt(data, '%stwitch.py: error communicating with twitch api' % weechat.prefix('error'))
274271
if OPTIONS['debug']:
275-
weechat.prnt(data['buffer'],'%stwitch.py: return code: %s' % (weechat.prefix('error'),rc))
276-
weechat.prnt(data['buffer'],'%stwitch.py: stdout: %s' % (weechat.prefix('error'),stdout))
277-
weechat.prnt(data['buffer'],'%stwitch.py: stderr: %s' % (weechat.prefix('error'),stderr))
278-
weechat.prnt(data['buffer'],'%stwitch.py: exception: %s' % (weechat.prefix('error'),e))
272+
weechat.prnt(data,'%stwitch.py: response code: %s' % (weechat.prefix('error'),output['response_code']))
273+
weechat.prnt(data,'%stwitch.py: headers: %s' % (weechat.prefix('error'),output['headers']))
274+
weechat.prnt(data,'%stwitch.py: output: %s' % (weechat.prefix('error'),output['output']))
279275
return weechat.WEECHAT_RC_OK
280276
currentbuf = weechat.current_buffer()
281277
pcolor = weechat.color('chat_prefix_network')
@@ -298,8 +294,7 @@ def channel_api(data, command, rc, stdout, stderr):
298294
pcolor, pformat, dcolor, ncolor, name, dcolor, ccolor, ul, rul, followers)
299295
weechat.prnt(data, makeutf8(output))
300296
url = 'https://api.twitch.tv/helix/users/follows?from_id=' + uid
301-
url_hook = weechat.hook_process_hashtable(
302-
"url:" + url, curlopt, 7 * 1000, "channel_api", data)
297+
url_hook = weechat.hook_url(url, curlopt, 7 * 1000, "channel_api", data)
303298
return weechat.WEECHAT_RC_OK
304299
if 'from_id' in command:
305300
following = jsonDict['total']
@@ -325,8 +320,7 @@ def channel_api(data, command, rc, stdout, stderr):
325320
pcolor, pformat, dcolor, ncolor, name, dcolor, ccolor, ul, rul, status)
326321
weechat.prnt(data, makeutf8(output))
327322
url = 'https://api.twitch.tv/helix/users/follows?to_id=' + uid
328-
url_hook = weechat.hook_process_hashtable(
329-
"url:" + url, curlopt, 7 * 1000, "channel_api", data)
323+
url_hook = weechat.hook_url(url, curlopt, 7 * 1000, "channel_api", data)
330324

331325
else:
332326
weechat.prnt(data, 'Error: No Such User')
@@ -353,15 +347,15 @@ def twitch_clearchat(data, modifier, modifier_data, string):
353347
if user:
354348
if 'ban-duration' in tags:
355349
if 'ban-reason' in tags and tags['ban-reason']:
356-
bn=tags['ban-reason'].replace('\s',' ')
350+
bn=re.sub(r'\s',' ', tags['ban-reason'])
357351
weechat.prnt(buffer,"%s--%s %s has been timed out for %s seconds %sReason%s: %s" %
358352
(pcolor, ccolor, user, tags['ban-duration'], ul, rul, bn))
359353
else:
360354
weechat.prnt(buffer,"%s--%s %s has been timed out for %s seconds" %
361355
(pcolor, ccolor, user, tags['ban-duration']))
362356
elif 'ban-reason' in tags:
363357
if tags['ban-reason']:
364-
bn=tags['ban-reason'].replace('\s',' ')
358+
bn=re.sub(r'\s', ' ', tags['ban-reason'])
365359
weechat.prnt(buffer,"%s--%s %s has been banned %sReason%s: %s" %
366360
(pcolor, ccolor, user, ul, rul,bn))
367361
else:
@@ -458,7 +452,7 @@ def twitch_usernotice(data, modifier, server, string):
458452
"irc", "%s.%s" % (server, mp['channel']))
459453
if mp['tags']:
460454
tags = dict([s.split('=',1) for s in mp['tags'].split(';')])
461-
msg = tags['system-msg'].replace('\s',' ')
455+
msg = re.sub(r'\\s', ' ', tags['system-msg'])
462456
if mp['text']:
463457
msg += ' [Comment] '+mp['text']
464458
weechat.prnt(buffer, '%s--%s %s' % (pcolor, ccolor, msg))
@@ -522,8 +516,7 @@ def twitch_whois(data, modifier, server_name, string):
522516
currentbuf = weechat.current_buffer()
523517
url = 'https://api.twitch.tv/kraken/users?login=' + username
524518
params='&api_version=5'
525-
url_hook = weechat.hook_process_hashtable(
526-
"url:" + url+params, curlopt, 7 * 1000, "channel_api", currentbuf)
519+
url_hook = weechat.hook_url(url+params, curlopt, 7 * 1000, "channel_api", currentbuf)
527520
return ""
528521

529522

@@ -682,7 +675,7 @@ def config_change(pointer, name, value):
682675
weechat.hook_modifier("irc_in2_HOSTTARGET", "twitch_suppress", "")
683676
weechat.hook_modifier("irc_in2_ROOMSTATE", "twitch_roomstate", "")
684677
weechat.hook_modifier("irc_in2_USERNOTICE", "twitch_usernotice", "")
685-
weechat.hook_modifier("irc_in_WHISPER", "twitch_whisper", "")
678+
weechat.hook_modifier("irc_in2_WHISPER", "twitch_whisper", "")
686679
weechat.hook_modifier("irc_out_PRIVMSG", "twitch_privmsg", "")
687680
weechat.hook_modifier("irc_out_WHOIS", "twitch_whois", "")
688681
weechat.hook_modifier("irc_in2_PRIVMSG", "twitch_in_privmsg", "")

0 commit comments

Comments
 (0)