Skip to content

Instantly share code, notes, and snippets.

@mdusher
Created January 21, 2015 06:06
Show Gist options
  • Save mdusher/1f11d30b9dd23ed202d6 to your computer and use it in GitHub Desktop.
Save mdusher/1f11d30b9dd23ed202d6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Scans directory every at an interval for files, reads all files and creates a pushbullet list notification
# Files must have at least 2 lines, the first line is the title used for the notification (groups by this) and the second the message
# Multiple lines can be added to one file
#
require 'eventmachine'
require 'faraday'
require 'daemons'
require 'json'
conf = {
apikey: "oh shit not my api key",
pushdir: "/not/my/push/directory/",
apiurl: "https://api.pushbullet.com",
apipush: "/v2/pushes",
interval: 300
}
def init(conf)
puts "initialising digest poller [dir=#{conf[:pushdir]},interval=#{conf[:interval]}]"
scanDir(conf)
EventMachine.run do
timer = EventMachine::PeriodicTimer.new(conf[:interval]) do
scanDir(conf)
end
Process.daemon
end
end
def scanDir(conf)
pushes = Hash.new
Dir.foreach(conf[:pushdir]) do |item|
next if item =~ /^\.\.?$/
break if (item.nil?)
item = File.join(conf[:pushdir],item)
lines = IO.read(item).split("\n");
if (lines.length > 1)
lines.each do |line|
if (line != lines.first)
pushes[lines.first] = Array.new if (!pushes.has_key?(lines.first))
pushes[lines.first] << line
end
end
end
File.delete(item)
end
if (pushes.size > 0)
pushes.each do |key,value|
update = pushUpdateList(conf, key, value)
if (update.nil?)
pushList(conf, key, value)
end
end
end
end
def pushList(conf, title, items)
conn = Faraday.new(url: conf[:apiurl])
conn.basic_auth(conf[:apikey],'')
body = {type: "list", title: title, items: items}.to_json
r = conn.post(conf[:apipush],body,{'Content-Type' => 'application/json'})
end
def pushUpdateList(conf, title, items)
conn = Faraday.new(url: conf[:apiurl])
conn.basic_auth(conf[:apikey],'')
r = conn.get(conf[:apipush], {"modified_after" => 1421654733})
r = JSON.parse(r.body)
r = r["pushes"]
r.each do |i,v|
if (i.has_key?("title"))
id = i["iden"]
if (i["title"] == title)
items.each do |l|
i["items"] << {"checked"=>false, "text"=>l}
end
body = { dismissed: false, items: i["items"] }.to_json
conn.post("#{conf[:apipush]}/#{id}", body, {'Content-Type' => 'application/json'})
return true
end
end
end
return nil
end
init(conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment