summaryrefslogtreecommitdiff
path: root/crystal
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk+git@omgwallhack.org>2022-03-14 08:08:56 -0700
committerJoe Rayhawk <jrayhawk+git@omgwallhack.org>2022-03-14 08:08:56 -0700
commitdf7e467d4c26f1108175d486bcc97e86251ffe60 (patch)
tree52d67b88df36531b62f86baf3a5b0062aa7933bf /crystal
parent7f2a7acf23d909d16551c75018d32bd5ccfea50e (diff)
downloadtwitchtools-df7e467d4c26f1108175d486bcc97e86251ffe60.tar.gz
twitchtools-df7e467d4c26f1108175d486bcc97e86251ffe60.zip
crystal/obs.cr: new obs-websocket dumper
Diffstat (limited to 'crystal')
-rw-r--r--crystal/obs.cr84
1 files changed, 84 insertions, 0 deletions
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\"}"
+# }
+#}
+#