Skip to content

Instantly share code, notes, and snippets.

@TheLinx
Created November 13, 2010 16:20
Show Gist options
  • Save TheLinx/675461 to your computer and use it in GitHub Desktop.
Save TheLinx/675461 to your computer and use it in GitHub Desktop.
a (very ugly) lua script that utilizes notify-send and other stuff to get the fp-ticker to GNU/Linux
#!/usr/bin/env lua
require "socket"
http = require "socket.http"
require "ltn12"
require "json"
require "state"
conf = state.load "fp-notify"
if not conf or arg[1] == "--reconfigure" then
if not conf then
print("Seems like this is your first time launching fp-notify!")
end
conf = {}
print("Let's set things up!")
print("List the threads you want to follow! One ID per line. When you are done, press Ctrl+D")
conf.following = {}
while true do
local id = io.read("*l")
if not id then --[[ ctrl+d pressed ]] break end
conf.following[id] = true
end
io.write("Do you want to get alerts when mentioned? [Y/n] ") io.flush()
if (io.read("*l") or ""):lower() == "n" then
conf.mentionalerts = false
else
conf.mentionalerts = true
end
print("That's it! Let's get this party started!")
conf.lastpost = 0
end
function genTickerUrl(post)
return "http://www.facepunch.com/fp_ticker.php?aj=1&json=1&startpost=" .. post
end
function genAvatarUrl(id)
return "http://75.101.147.23/image.php?u=" .. id
end
function alert(title, message, picture)
return os.execute(("notify-send -i %q %q %q"):format(picture, title, message))
end
repeat
local body, code, headers = http.request(genTickerUrl(conf.lastpost))
if code == 200 then
body = json.decode(body)
for _, post in ipairs(body) do
if post.username then -- this is only nil if the post isn't avilable to you. like gmf posts.
if conf.following[post.threadid] or
(conf.mentionalerts and post.mentioned) then -- alert tiem!
-- first, let's download the user avatar.
if not io.open("/tmp/fp-notify-avatar-"..post.userid) then
http.request{
url = genAvatarUrl(post.userid),
sink = ltn12.sink.file(io.open("/tmp/fp-notify-avatar-"..post.userid, "w"))
}
end
-- now, let's alert
local title
if conf.following[post.threadid] then
title = "A thread you're following got a new post!"
elseif post.mentioned then
title = "You were mentioned in a thread!"
end
alert(title, ("Post #%s by %s in %s"):format(post.postid, post.username, post.threadname or post.postid), "/tmp/fp-notify-avatar-"..post.userid) -- sometimes the ticker makes post.threadname null. oh joy!
end
end
conf.lastpost = post.postid
end
else
print(body)
break
end
until os.execute("sleep 5") ~= 0 -- the os sleep is used because when it's cancelled, the while loop exits instead of the whole script. sure it's ugly, but so is your face.
print "\nExiting..."
state.store("fp-notify", conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment