def main(): if not weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, 'exit_cb', ''): return weechat.WEECHAT_RC_ERROR # registration required for accessing config global NOTE_PREFIX global ERROR_PREFIX NOTE_PREFIX = weechat.color( weechat.config_color( weechat.config_get( 'weechat.color.chat_prefix_join'))) + weechat.config_string( weechat.config_get('weechat.look.prefix_join')) + '\t' ERROR_PREFIX = weechat.color( weechat.config_color( weechat.config_get( 'weechat.color.chat_prefix_error'))) + weechat.config_string( weechat.config_get('weechat.look.prefix_error')) + '\t' create_buffer() set_default_settings() log_debug('Test note', note=1) log_debug('Test error', error=1) if not get_setting('token'): show_auth_hint()
def fish_alert(buffer, message): mark = "%s%s%s\t" % ( weechat.color(weechat.config_color(fish_config_option["alert"])), weechat.config_string(fish_config_option["marker"]), weechat.color("chat")) weechat.prnt(buffer, "%s%s" % (mark, message))
def customize_join_cb_signal(data, signal, signal_data): weechat.prnt("","data: %s signal: %s signal_data: %s" % (data,signal,signal_data)) message = weechat.config_get_plugin('join_message') if message == '': return weechat.WEECHAT_RC_OK parsed = get_hashtable(signal_data) if parsed['nick'] == own_nick(signal.split(',', 1)[0]): return weechat.WEECHAT_RC_OK parsed['message'] = "" # dummy. no message for JOIN parsed['kicked_nick'] = '' # dummy. no KICK here message = create_output(message,parsed,'join') buf_pointer = weechat.buffer_search('irc',"%s.%s" % (signal.split(',', 1)[0],parsed['channel'])) prefix = weechat.config_string(weechat.config_get('weechat.look.prefix_join')) prefix_color = weechat.color(weechat.config_color(weechat.config_get('weechat.color.chat_prefix_join'))) message_tags = '' if weechat.config_get_plugin('no_log').lower() == 'on': message_tags = 'no_log' weechat.prnt_date_tags(buf_pointer,0,message_tags,'%s%s\t%s' % (prefix_color,prefix,message)) return weechat.WEECHAT_RC_OK
def customize_join_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin("join_message") if message == "": return string parsed = get_hashtable(string) if parsed["nick"] == own_nick(modifier_data): return string parsed["message"] = "" # dummy. no message for irc_JOIN parsed["kicked_nick"] = "" # dummy. no irc_KICK here message = create_output(message, parsed, "join") if OPTIONS["debug"] == "on": weechat.prnt("", string) weechat.prnt("", parsed["channel"]) weechat.prnt("", parsed["message"]) buffer_ptr = weechat.buffer_search("irc", "%s.%s" % (modifier_data, parsed["channel"])) prefix = weechat.config_string(weechat.config_get("weechat.look.prefix_join")) prefix_color = weechat.color(weechat.config_color(weechat.config_get("weechat.color.chat_prefix_join"))) prefix = substitute_colors(prefix) message_tags = "" if weechat.config_get_plugin("no_log").lower() == "on": message_tags = "no_log" weechat.prnt_date_tags(buffer_ptr, 0, message_tags, "%s%s\t%s" % (prefix_color, prefix, message)) return string
def customize_privmsg_cb(data, modifier, modifier_data, string): weechat.prnt("",data) weechat.prnt("",modifier) weechat.prnt("",modifier_data) weechat.prnt("",string) parsed = get_hashtable(string) message = parsed['message'].strip() # Filter out non-CTCP messages and non ACTION messages if not message or ord(message[0]) != 1 or not message[1:].startswith("ACTION"): return string text = message[8:-1] parsed['kicked_nick'] = '' # dummy. no irc_KICK here parsed['message'] = text message = create_output("${*blue}%N %M",parsed,'action') buffer_ptr = weechat.buffer_search('irc',"%s.%s" % (modifier_data,parsed['channel'])) prefix = weechat.config_string(weechat.config_get('weechat.look.prefix_action')) prefix_color = weechat.color(weechat.config_color(weechat.config_get('weechat.color.chat_prefix_action'))) prefix = substitute_colors(prefix) message_tags = '' if weechat.config_get_plugin('no_log').lower() == 'on': message_tags = 'no_log' weechat.prnt_date_tags(buffer_ptr,0,message_tags,'%s%s\t%s' % (prefix_color,prefix,message)) return string
def customize_join_cb_signal(data, signal, signal_data): weechat.prnt("", "data: %s signal: %s signal_data: %s" % (data, signal, signal_data)) message = weechat.config_get_plugin("join_message") if message == "": return weechat.WEECHAT_RC_OK parsed = get_hashtable(signal_data) if parsed["nick"] == own_nick(signal.split(",", 1)[0]): return weechat.WEECHAT_RC_OK parsed["message"] = "" # dummy. no message for JOIN parsed["kicked_nick"] = "" # dummy. no KICK here message = create_output(message, parsed, "join") buffer_ptr = weechat.buffer_search("irc", "%s.%s" % (signal.split(",", 1)[0], parsed["channel"])) prefix = weechat.config_string(weechat.config_get("weechat.look.prefix_join")) prefix_color = weechat.color(weechat.config_color(weechat.config_get("weechat.color.chat_prefix_join"))) message_tags = "" if weechat.config_get_plugin("no_log").lower() == "on": message_tags = "no_log" weechat.prnt_date_tags(buffer_ptr, 0, message_tags, "%s%s\t%s" % (prefix_color, prefix, message)) return weechat.WEECHAT_RC_OK
def customize_kick_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin("kick_message") if message == "": return string parsed = get_hashtable(string) try: parsed["kicked_nick"] = parsed["arguments"].split(" ", 1)[1] parsed["kicked_nick"] = parsed["kicked_nick"].split(" :", 1)[0] except: parsed["kicked_nick"] = "" message = create_output(message, parsed, "kick") if OPTIONS["debug"] == "on": weechat.prnt("", string) weechat.prnt("", parsed["channel"]) weechat.prnt("", parsed["message"]) buffer_ptr = weechat.buffer_search("irc", "%s.%s" % (modifier_data, parsed["channel"])) if not (buffer_ptr): return string prefix = weechat.config_string(weechat.config_get("weechat.look.prefix_quit")) prefix_color = weechat.color(weechat.config_color(weechat.config_get("weechat.color.chat_prefix_quit"))) message_tags = "" if weechat.config_get_plugin("no_log").lower() == "on": message_tags = "no_log" weechat.prnt_date_tags(buffer_ptr, 0, message_tags, "%s%s\t%s" % (prefix_color, prefix, message)) return string
def customize_part_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin('part_message') if message == '': return string parsed = get_hashtable(string) if parsed['nick'] == own_nick(modifier_data): return string parsed['kicked_nick'] = '' # dummy. no irc_KICK here message = create_output(message,parsed,'part') if OPTIONS['debug'] == 'on': weechat.prnt("","debug mode: irc_part") weechat.prnt("","string: %s" % string) weechat.prnt("",parsed['channel']) weechat.prnt("",parsed['message']) buf_pointer = weechat.buffer_search('irc',"%s.%s" % (modifier_data,parsed['channel'])) prefix = weechat.config_string(weechat.config_get('weechat.look.prefix_quit')) prefix_color = weechat.color(weechat.config_color(weechat.config_get('weechat.color.chat_prefix_quit'))) prefix = substitute_colors(prefix) message_tags = '' if weechat.config_get_plugin('no_log').lower() == 'on': message_tags = 'no_log' weechat.prnt_date_tags(buf_pointer,0,message_tags,'%s%s\t%s' % (prefix_color,prefix,message)) return string
def customize_kick_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin('kick_message') if message == '': return string parsed = get_hashtable(string) try: parsed['kicked_nick'] = parsed['arguments'].split(' ', 1)[1] parsed['kicked_nick'] = parsed['kicked_nick'].split(' :', 1)[0] except: parsed['kicked_nick'] = '' message = create_output(message,parsed,'kick') if OPTIONS['debug'] == 'on': weechat.prnt("",string) weechat.prnt("",parsed['channel']) weechat.prnt("",parsed['message']) buf_pointer = weechat.buffer_search('irc',"%s.%s" % (modifier_data,parsed['channel'])) prefix = weechat.config_string(weechat.config_get('weechat.look.prefix_quit')) prefix_color = weechat.color(weechat.config_color(weechat.config_get('weechat.color.chat_prefix_quit'))) message_tags = '' if weechat.config_get_plugin('no_log').lower() == 'on': message_tags = 'no_log' weechat.prnt_date_tags(buf_pointer,0,message_tags,'%s%s\t%s' % (prefix_color,prefix,message)) return string
def do_git_pull(): if (weechat.config_get_plugin("git_pull") == "false"): return path = weechat.config_get_plugin("git_compile_location") if path != "": f = os.popen("cd %s && git pull 2>&1" % path) stuff = f.readline() weechat.prnt("",weechat.prefix("action") + weechat.color(weechat.config_color(weechat.config_get("weechat.color.chat_nick_self"))) + SCRIPT_NAME + ":") weechat.prnt("",stuff) f.close()
def fish_announce_unencrypted(buffer, target): global fish_encryption_announced, fish_config_option if (not weechat.config_boolean(fish_config_option['announce']) or not fish_encryption_announced.get(target)): return fish_alert(buffer, "Messages to/from %s are %s*not*%s encrypted." % ( target, weechat.color(weechat.config_color(fish_config_option["alert"])), weechat.color("chat"))) del fish_encryption_announced[target]
def do_git_pull(): if (weechat.config_get_plugin("git_pull") == "false"): return path = weechat.config_get_plugin("git_compile_location") if path != "": f = os.popen("cd %s && git pull 2>&1" % path) stuff = f.readline() weechat.prnt( "", weechat.prefix("action") + weechat.color( weechat.config_color( weechat.config_get("weechat.color.chat_nick_self"))) + SCRIPT_NAME + ":") weechat.prnt("", stuff) f.close()
def show_spell_suggestion_item_cb(data, item, window): buffer = weechat.window_get_pointer(window, "buffer") if buffer == '': return '' if OPTIONS['replace_mode'].lower() == "on": if not weechat.buffer_get_string(buffer, 'localvar_inline_suggestions'): return '' tab_complete, position, aspell_suggest_items = weechat.buffer_get_string( buffer, 'localvar_inline_suggestions').split(':', 2) localvar_aspell_suggest = "dummy:%s" % aspell_suggest_items # return aspell_suggest_items else: tab_complete, position, aspell_suggest_item = get_position_and_suggest_item( buffer) localvar_aspell_suggest = get_localvar_aspell_suggest(buffer) # localvar_aspell_suggest = word,word2/wort,wort2 if localvar_aspell_suggest: misspelled_word, aspell_suggestions = localvar_aspell_suggest.split( ':') aspell_suggestions_orig = aspell_suggestions aspell_suggestions = aspell_suggestions.replace('/', ',') aspell_suggestion_list = aspell_suggestions.split(',') if not position: return '' if position == "-1": return aspell_suggestions_orig if int(position) < len(aspell_suggestion_list): reset_color = weechat.color('reset') color = weechat.color( weechat.config_color( weechat.config_get("%s.color.misspelled" % plugin_name))) new_word = aspell_suggestion_list[int(position)].replace( aspell_suggestion_list[int(position)], '%s%s%s' % (color, aspell_suggestion_list[int(position)], reset_color)) aspell_suggestion_list[int( position)] = new_word # replace word with colored word aspell_suggestions_orig = ','.join(map(str, aspell_suggestion_list)) else: return '' return aspell_suggestions_orig
def customize_quit_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin('quit_message') if message == '': return string parsed = get_hashtable(string) if parsed['nick'] == own_nick(modifier_data): return string parsed['kicked_nick'] = '' # dummy. no irc_KICK here message = create_output(message,parsed,'quit') if OPTIONS['debug'] == 'on': weechat.prnt("","debug mode: irc_quit") weechat.prnt("","string: %s" % string) weechat.prnt("",parsed['channel']) weechat.prnt("",parsed['message']) buf_pointer = weechat.buffer_search('irc',"%s.%s" % (modifier_data,parsed['channel'])) prefix = weechat.config_string(weechat.config_get('weechat.look.prefix_quit')) prefix_color = weechat.color(weechat.config_color(weechat.config_get('weechat.color.chat_prefix_quit'))) prefix = substitute_colors(prefix) message_tags = '' ptr_infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(ptr_infolist): ptr_buffer = weechat.infolist_pointer(ptr_infolist, "pointer") if weechat.buffer_get_string(ptr_buffer, 'plugin') != 'irc': continue localvar_type = weechat.buffer_get_string(ptr_buffer, 'localvar_type') localvar_server = weechat.buffer_get_string(ptr_buffer, 'localvar_server') if localvar_type != 'channel' or localvar_server != modifier_data: continue # localvar_channel = weechat.buffer_get_string(buffer, 'localvar_channel') if (weechat_nicklist_search_nick(ptr_buffer,parsed['nick'])): if weechat.config_get_plugin('no_log').lower() == 'on': message_tags = 'no_log' weechat.prnt_date_tags(ptr_buffer,0,message_tags,'%s%s\t%s' % (prefix_color,prefix,message)) weechat.infolist_free(ptr_infolist) return string
def customize_quit_cb(data, modifier, modifier_data, string): message = weechat.config_get_plugin("quit_message") if message == "": return string parsed = get_hashtable(string) if parsed["nick"] == own_nick(modifier_data): return string parsed["kicked_nick"] = "" # dummy. no irc_KICK here message = create_output(message, parsed, "quit") if OPTIONS["debug"] == "on": weechat.prnt("", "debug mode: irc_quit") weechat.prnt("", "string: %s" % string) weechat.prnt("", parsed["channel"]) weechat.prnt("", parsed["message"]) buffer_ptr = weechat.buffer_search("irc", "%s.%s" % (modifier_data, parsed["channel"])) prefix = weechat.config_string(weechat.config_get("weechat.look.prefix_quit")) prefix_color = weechat.color(weechat.config_color(weechat.config_get("weechat.color.chat_prefix_quit"))) prefix = substitute_colors(prefix) message_tags = "" ptr_infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(ptr_infolist): ptr_buffer = weechat.infolist_pointer(ptr_infolist, "pointer") if weechat.buffer_get_string(ptr_buffer, "plugin") != "irc": continue localvar_type = weechat.buffer_get_string(ptr_buffer, "localvar_type") localvar_server = weechat.buffer_get_string(ptr_buffer, "localvar_server") if localvar_type != "channel" or localvar_server != modifier_data: continue # localvar_channel = weechat.buffer_get_string(buffer, 'localvar_channel') if weechat_nicklist_search_nick(ptr_buffer, parsed["nick"]): if weechat.config_get_plugin("no_log").lower() == "on": message_tags = "no_log" weechat.prnt_date_tags(ptr_buffer, 0, message_tags, "%s%s\t%s" % (prefix_color, prefix, message)) weechat.infolist_free(ptr_infolist) return string
def show_spell_suggestion_item_cb (data, item, window): buffer = weechat.window_get_pointer(window,"buffer") if buffer == '': return '' if OPTIONS['replace_mode'].lower() == "on": if not weechat.buffer_get_string(buffer,'localvar_inline_suggestions'): return '' tab_complete,position,aspell_suggest_items = weechat.buffer_get_string(buffer,'localvar_inline_suggestions').split(':',2) localvar_aspell_suggest = "dummy:%s" % aspell_suggest_items # return aspell_suggest_items else: tab_complete,position,aspell_suggest_item = get_position_and_suggest_item(buffer) localvar_aspell_suggest = get_localvar_aspell_suggest(buffer) # localvar_aspell_suggest = word,word2/wort,wort2 if localvar_aspell_suggest: misspelled_word,aspell_suggestions = localvar_aspell_suggest.split(':') aspell_suggestions_orig = aspell_suggestions aspell_suggestions = aspell_suggestions.replace('/',',') aspell_suggestion_list = aspell_suggestions.split(',') if not position: return '' if position == "-1": return aspell_suggestions_orig if int(position) < len(aspell_suggestion_list): reset_color = weechat.color('reset') color = weechat.color(weechat.config_color(weechat.config_get("%s.color.misspelled" % plugin_name))) new_word = aspell_suggestion_list[int(position)].replace(aspell_suggestion_list[int(position)],'%s%s%s' % (color, aspell_suggestion_list[int(position)], reset_color)) aspell_suggestion_list[int(position)] = new_word # replace word with colored word aspell_suggestions_orig = ','.join(map(str, aspell_suggestion_list)) else: return '' return aspell_suggestions_orig
def config_color(option): """Get the color of a color config option.""" return weechat.color( weechat.config_color( weechat.config_get(config_prefix('color.%s' % option))))
def stream_api(data, command, rc, stdout, stderr): try: jsonDict = json.loads(stdout.strip()) except Exception as e: weechat.prnt(data, '%stwitch.py: error communicating with twitch api' % weechat.prefix('error')) if OPTIONS['debug']: weechat.prnt(data,'%stwitch.py: return code: %s' % (weechat.prefix('error'),rc)) weechat.prnt(data,'%stwitch.py: stdout: %s' % (weechat.prefix('error'),stdout)) weechat.prnt(data,'%stwitch.py: stderr: %s' % (weechat.prefix('error'),stderr)) weechat.prnt(data,'%stwitch.py: exception: %s' % (weechat.prefix('error'),e)) return weechat.WEECHAT_RC_OK currentbuf = weechat.current_buffer() title_fg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_fg"))) title_bg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_bg"))) pcolor = weechat.color('chat_prefix_network') ccolor = weechat.color('chat') red = weechat.color('red') blue = weechat.color('blue') green = weechat.color('green') ptime = time.strftime("%H:%M:%S") subs = weechat.buffer_get_string(data, 'localvar_subs') r9k = weechat.buffer_get_string(data, 'localvar_r9k') slow = weechat.buffer_get_string(data, 'localvar_slow') emote = weechat.buffer_get_string(data, 'localvar_emote') if 'status' in jsonDict.keys(): if jsonDict['status'] == 422: weechat.prnt(data, 'ERROR: The community has closed this channel due to terms of service violations.') if jsonDict['status'] == 404: weechat.prnt(data, 'ERROR: The page could not be found, or has been deleted by its owner.') return weechat.WEECHAT_RC_OK if not 'stream' in jsonDict.keys(): weechat.prnt(data, 'twitch.py: Error with twitch API (stream key missing from json)') return weechat.WEECHAT_RC_OK if not jsonDict['stream']: line = "STREAM: %sOFFLINE%s %sCHECKED AT: %s" % ( red, title_fg, blue, ptime) if subs: line += " %s[SUBS]" % title_fg if r9k: line += " %s[R9K]" % title_fg if slow: line += " %s[SLOW@%s]" % (title_fg, slow) if emote: line += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", line) else: currenttime = time.time() output = 'STREAM: %sLIVE%s' % (green, title_fg) if 'game' in jsonDict['stream']: if jsonDict['stream']['game']: game = gameshort(jsonDict['stream']['game']) output += ' %s with' % makeutf8(game) if 'viewers' in jsonDict['stream']: viewers = jsonDict['stream']['viewers'] output += ' %s viewers started' % viewers if 'created_at' in jsonDict['stream']: createtime = jsonDict['stream']['created_at'].replace('Z', 'GMT') starttime = timegm( time.strptime(createtime, '%Y-%m-%dT%H:%M:%S%Z')) dur = timedelta(seconds=currenttime - starttime) uptime = days_hours_minutes(dur) output += ' %s ago' % uptime if 'channel' in jsonDict['stream']: if 'followers' in jsonDict['stream']['channel']: followers = jsonDict['stream']['channel']['followers'] output += ' [%s followers]' % followers if 'status' in jsonDict['stream']['channel']: titleutf8=jsonDict['stream']['channel']['status'].replace('\n',' ').encode('utf8') titleascii=jsonDict['stream']['channel']['status'].encode('ascii','replace') if not isinstance(titleutf8, str): titleascii=str(titleascii,'utf8') titleutf8=str(titleutf8,'utf8') oldtitle = weechat.buffer_get_string(data, 'localvar_tstatus') if not oldtitle == titleascii: weechat.prnt(data, '%s--%s Title is "%s"' % (pcolor, ccolor, titleutf8)) weechat.buffer_set(data, 'localvar_set_tstatus', titleascii) if 'updated_at' in jsonDict['stream']['channel']: updateat = jsonDict['stream']['channel'][ 'updated_at'].replace('Z', 'GMT') updatetime = timegm( time.strptime(updateat, '%Y-%m-%dT%H:%M:%S%Z')) udur = timedelta(seconds=currenttime - updatetime) titleage = days_hours_minutes(udur) output += ' %s' % ptime if subs: output += " %s[SUBS]" % title_fg if r9k: output += " %s[R9K]" % title_fg if slow: output += " %s[SLOW@%s]" % (title_fg, slow) if emote: output += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", output) return weechat.WEECHAT_RC_OK
def _c(option, bgoption=False): # return a color character with numbers based on options if bgoption: return weechat.color(",".join((weechat.config_color(weechat.config_get(option)), weechat.config_color(weechat.config_get(bgoption))))) return weechat.color(weechat.config_color(weechat.config_get(option)))
def config_color(option): """Get the color of a color config option.""" return weechat.color(weechat.config_color(weechat.config_get( config_prefix('color.%s' % option))))
def stream_api(data, command, rc, stdout, stderr): try: jsonDict = json.loads(stdout.strip()) except Exception as e: weechat.prnt(data, '%stwitch.py: error communicating with twitch api' % weechat.prefix('error')) if OPTIONS['debug']: weechat.prnt(data,'%stwitch.py: return code: %s' % (weechat.prefix('error'),rc)) weechat.prnt(data,'%stwitch.py: stdout: %s' % (weechat.prefix('error'),stdout)) weechat.prnt(data,'%stwitch.py: stderr: %s' % (weechat.prefix('error'),stderr)) weechat.prnt(data,'%stwitch.py: exception: %s' % (weechat.prefix('error'),e)) return weechat.WEECHAT_RC_OK currentbuf = weechat.current_buffer() title_fg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_fg"))) title_bg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_bg"))) pcolor = weechat.color('chat_prefix_network') ccolor = weechat.color('chat') red = weechat.color('red') blue = weechat.color('blue') green = weechat.color('green') ptime = time.strftime("%H:%M:%S") subs = weechat.buffer_get_string(data, 'localvar_subs') r9k = weechat.buffer_get_string(data, 'localvar_r9k') slow = weechat.buffer_get_string(data, 'localvar_slow') emote = weechat.buffer_get_string(data, 'localvar_emote') if not 'data' in jsonDict.keys(): weechat.prnt(data, 'twitch.py: Error with twitch API (data key missing from json)') return weechat.WEECHAT_RC_OK if not jsonDict['data']: line = "STREAM: %sOFFLINE%s %sCHECKED AT: (%s)" % ( red, title_fg, blue, ptime) if subs: line += " %s[SUBS]" % title_fg if r9k: line += " %s[R9K]" % title_fg if slow: line += " %s[SLOW@%s]" % (title_fg, slow) if emote: line += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", line) else: currenttime = time.time() if len(jsonDict['data']) == 1: jsonDict['data'] = jsonDict['data'][0] output = 'STREAM: %sLIVE%s' % (green, title_fg) if 'game_id' in jsonDict['data']: if jsonDict['data']['game_id']: game = jsonDict['data']['game_id'] game_id = game if game in gameid_cache: game = gameid_cache[game] output += ' <%s> with' % game if 'viewer_count' in jsonDict['data']: viewers = jsonDict['data']['viewer_count'] output += ' %s viewers started' % viewers if 'started_at' in jsonDict['data']: createtime = jsonDict['data']['started_at'].replace('Z', 'GMT') starttime = timegm( time.strptime(createtime, '%Y-%m-%dT%H:%M:%S%Z')) dur = timedelta(seconds=currenttime - starttime) uptime = days_hours_minutes(dur) output += ' %s ago' % uptime if 'title' in jsonDict['data']: titleutf8=jsonDict['data']['title'].replace('\n',' ').encode('utf8') titleascii=jsonDict['data']['title'].encode('ascii','replace') if not isinstance(titleutf8, str): titleascii=str(titleascii,'utf8') titleutf8=str(titleutf8,'utf8') oldtitle = weechat.buffer_get_string(data, 'localvar_tstatus') if not oldtitle == titleascii: weechat.prnt(data, '%s--%s Title is "%s"' % (pcolor, ccolor, titleutf8)) weechat.buffer_set(data, 'localvar_set_tstatus', titleascii) output += ' (%s)' % ptime if subs: output += " %s[SUBS]" % title_fg if r9k: output += " %s[R9K]" % title_fg if slow: output += " %s[SLOW@%s]" % (title_fg, slow) if emote: output += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", output) if not game_id in gameid_cache: url = 'https://api.twitch.tv/helix/games?id=' + game_id weechat.hook_process_hashtable( "url:" + url, curlopt, 7 * 1000, "game_api", data) return weechat.WEECHAT_RC_OK
def stream_api(data, command, rc, stdout, stderr): try: jsonDict = json.loads(stdout.strip()) except Exception as e: weechat.prnt(data, 'TWITCH: Error with twitch API') return weechat.WEECHAT_RC_OK currentbuf = weechat.current_buffer() title_fg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_fg"))) title_bg = weechat.color( weechat.config_color(weechat.config_get("weechat.bar.title.color_bg"))) pcolor = weechat.color('chat_prefix_network') ccolor = weechat.color('chat') red = weechat.color('red') blue = weechat.color('blue') green = weechat.color('green') ptime = time.strftime("%H:%M:%S") subs = weechat.buffer_get_string(data, 'localvar_subs') r9k = weechat.buffer_get_string(data, 'localvar_r9k') slow = weechat.buffer_get_string(data, 'localvar_slow') emote = weechat.buffer_get_string(data, 'localvar_emote') if 'status' in jsonDict.keys(): if jsonDict['status'] == 422: weechat.prnt( data, 'ERROR: The community has closed this channel due to terms of service violations.' ) if jsonDict['status'] == 404: weechat.prnt( data, 'ERROR: The page could not be found, or has been deleted by its owner.' ) return weechat.WEECHAT_RC_OK if not 'stream' in jsonDict.keys(): weechat.prnt(data, 'TWITCH: Error with twitch API') return weechat.WEECHAT_RC_OK if not jsonDict['stream']: line = "STREAM: %sOFFLINE%s %sCHECKED AT: %s" % (red, title_fg, blue, ptime) if subs: line += " %s[SUBS]" % title_fg if r9k: line += " %s[R9K]" % title_fg if slow: line += " %s[SLOW@%s]" % (title_fg, slow) if emote: line += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", line) else: currenttime = time.time() output = 'STREAM: %sLIVE%s' % (green, title_fg) if 'game' in jsonDict['stream']: if jsonDict['stream']['game']: game = gameshort(jsonDict['stream']['game']).encode('utf8') output += ' %s with' % game if 'viewers' in jsonDict['stream']: viewers = jsonDict['stream']['viewers'] output += ' %s viewers started' % viewers if 'created_at' in jsonDict['stream']: createtime = jsonDict['stream']['created_at'].replace('Z', 'GMT') starttime = timegm(time.strptime(createtime, '%Y-%m-%dT%H:%M:%S%Z')) dur = timedelta(seconds=currenttime - starttime) uptime = days_hours_minutes(dur) output += ' %s ago' % uptime if 'channel' in jsonDict['stream']: if 'followers' in jsonDict['stream']['channel']: followers = jsonDict['stream']['channel']['followers'] output += ' [%s followers]' % followers if 'status' in jsonDict['stream']['channel']: titleutf8 = jsonDict['stream']['channel']['status'].encode( 'utf8') titleascii = jsonDict['stream']['channel']['status'].encode( 'ascii', 'replace') if not isinstance(titleutf8, str): titleascii = str(titleascii, 'utf8') titleutf8 = str(titleutf8, 'utf8') oldtitle = weechat.buffer_get_string(data, 'localvar_tstatus') if not oldtitle == titleascii: weechat.prnt( data, '%s--%s Title is "%s"' % (pcolor, ccolor, titleutf8)) weechat.buffer_set(data, 'localvar_set_tstatus', titleascii) if 'updated_at' in jsonDict['stream']['channel']: updateat = jsonDict['stream']['channel']['updated_at'].replace( 'Z', 'GMT') updatetime = timegm( time.strptime(updateat, '%Y-%m-%dT%H:%M:%S%Z')) udur = timedelta(seconds=currenttime - updatetime) titleage = days_hours_minutes(udur) output += ' %s' % ptime if subs: output += " %s[SUBS]" % title_fg if r9k: output += " %s[R9K]" % title_fg if slow: output += " %s[SLOW@%s]" % (title_fg, slow) if emote: output += " %s[EMOTE]" % title_fg weechat.buffer_set(data, "title", output) return weechat.WEECHAT_RC_OK