public
Created — forked from xorrizon/diaspora-libnotify.rb

  • Download Gist
diaspora-libnotify.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#!/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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.