Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# Required ubuntu packages: libnotify1
# required gems: json, libnotify from http://github.com/splattael/libnotify
# required file: web_socket.rb from http://github.com/gimite/web-socket-ruby/blob/master/lib/web_socket.rb
HOST="localhost" # The host where diaspora is running
WSPORT=8080 # Websocket port
USERID="xxxxxxxxxxxxxxxxxxxxxxx" # Your userid (the one you see in the adress bar when clicking edit profile)
REGISTERED_PATH="/" # Can be a path to a specific aspect or / for all
ICON_PATH=File.join(File.dirname(__FILE__), "icons")
require "web_socket"
require "rubygems"
require "json"
require "open-uri"
require "libnotify"
def local_icon_path(url)
Dir.mkdir(ICON_PATH) unless File.exists?(ICON_PATH)
filename = url.match(/.*\/(.+?)$/)[1]
filepath = File.expand_path File.join(ICON_PATH, filename)
unless File.exists?(filepath)
puts "downloading: #{url}"
open(url) do |uf|
File.open(filepath, "w") do |f|
f.write(uf.read)
end
end
end
filepath
end
def parse_msg(msg)
user, text = msg["html"].match(/<span class='from'>.+?<a href=.+?">(.+?)<\/a>(.+?)<\/span>/mi)[1..2]
icon_url = msg["html"].match(/<img.+?class="avatar" src="(.+?)([?].+?)?".+?\/>/i)[1]
unless icon_url =~ /^https?:\/\//
icon_url = "http://" + HOST + icon_url
end
text.strip!
type = msg["class"]
{:type => type, :user => user, :text => text, :icon_url => icon_url}
end
def send_notify(info, icon_path)
return unless info[:type]=="status_messages" || info[:type] =="comments"
n = Libnotify.new do |notify|
notify.summary = info[:user]
notify.body = info[:text]
notify.timeout = 1.5
notify.urgency = :normal # :low, :normal, :critical
notify.append = false # default true - append onto existing notification
notify.icon_path = icon_path
end
n.show!
end
client = WebSocket.new("ws://#{HOST}:#{WSPORT}/#{USERID}")
trap("INT") do
puts "Traped interrupt, going to exit"
client.close()
exit()
end
puts("Connected")
client.send(REGISTERED_PATH)
while data = client.receive() do
begin
msg = JSON.parse data
#puts "MSG (decoded):"
#puts msg["html"]
info = parse_msg(msg)
puts info.inspect
icon_path = local_icon_path(info[:icon_url])
send_notify(info, icon_path)
#puts
rescue JSON::ParserError
puts "ERROR: JSON parse error"
end
end
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment