From df7e467d4c26f1108175d486bcc97e86251ffe60 Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Mon, 14 Mar 2022 08:08:56 -0700 Subject: crystal/obs.cr: new obs-websocket dumper --- crystal/obs.cr | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 crystal/obs.cr (limited to 'crystal/obs.cr') diff --git a/crystal/obs.cr b/crystal/obs.cr new file mode 100644 index 0000000..31cba77 --- /dev/null +++ b/crystal/obs.cr @@ -0,0 +1,84 @@ +require "http/web_socket" +require "json" +require "pretty_print" + +settings = Hash(String, String).new + +settings["home"] = Path.home.to_s +settings["access_token"] = File.read( settings["home"] + "/.config/twitch/access_token" ).chomp +settings["client_id"] = File.read( settings["home"] + "/.config/twitch/client_id" ).chomp +settings["channel"] = File.read( settings["home"] + "/.config/twitch/channel" ).chomp +settings["channel_id"] = File.read( settings["home"] + "/.config/twitch/channel_id" ).chomp + +def pubsub_send ( settings, pubsub : HTTP::WebSocket, msg ) + pubsub.send( msg ) + puts( ("SENT : " + msg).gsub(/#{settings["access_token"]}/,"NOPE").gsub(/#{settings["client_id"]}/, "NOPE") ) +end + +def ircmsg( home : String, channel : String, msg : String) + if msg !~ /(\r|\n)/ + sock = Socket.unix +# sock.connect Socket::UNIXAddress.new( home + "/.irssi/twitch-socket".to_s, type: Socket::Type::DGRAM ) + sock.connect Socket::UNIXAddress.new( home + "/.irssi/twitch-socket".to_s ) + sock.puts( "#" + channel + " " + msg ) + sock.close + end +end + +def effectsmsg( msg : String ) + sock = Socket.unix + sock.connect Socket::UNIXAddress.new( "/etc/twitch/effects-socket" ) + sock.puts( msg ) + sock.close +rescue ex + STDERR.puts( ex ) +end + +msg_ping = { + "type" => "PING" +}.to_json.to_s + + +struct Nil + def as_s? + self + end +end + + +spawn do + loop do + begin + obs_pubsub = HTTP::WebSocket.new( URI.parse( "ws://127.0.0.1:4444/" ), HTTP::Headers{"Cookie" => "SESSIONID=1234"} ) + obs_pubsub.on_message do | message | + print( "RECEIVED: ") + json = JSON.parse( message ) + print( json.to_pretty_json ) + print( "\n") + case json["update-type"]? + when "SwitchScenes" + ircmsg( settings["home"], settings["channel"], "| obs: switched scene to " + ( json["scene-name"]?.as_s? || "unknown" ) ) + end + end + obs_pubsub.run + rescue ex + ircmsg( settings["home"], settings["channel"], "obs.cr: " + ex.to_s.gsub(/\r|\n/, ' ') ) + puts ex + obs_pubsub && obs_pubsub.close + end + sleep 10 + next + end +end + +loop do + sleep 10 +end +#RECEIVED: { +# "type": "MESSAGE", +# "data": { +# "topic": "channel-subscribe-events-v1.59895482", +# "message": "{\"benefit_end_month\":0,\"user_name\":\"orangesherbet\",\"display_name\":\"orangesherbet\",\"channel_name\":\"bungmonkey\",\"user_id\":\"36184711\",\"channel_id\":\"59895482\",\"time\":\"2020-05-06T06:55:31.362125005Z\",\"sub_message\":{\"message\":\"seqDag bungmoButt bungmoFart\",\"emotes\":[{\"start\":0,\"end\":5,\"id\":\"496082\"},{\"start\":7,\"end\":16,\"id\":\"300780134\"},{\"start\":18,\"end\":27,\"id\":\"301501910\"}]},\"sub_plan\":\"1000\",\"sub_plan_name\":\"5\",\"months\":0,\"cumulative_months\":21,\"context\":\"resub\"}" +# } +#} +# -- cgit v1.2.3