Kooky xkcd

From awesome
Jump to: navigation, search

Kooky's xkcd

Just a little script to download a random xkcd comic that can be displayed in a naughty popup

Function code

You will need the lua socket library http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/

 local http = require("socket.http")
 local ltn12 = require("ltn12")
 local io = io
 local string = string
 module("xkcd")
 local tempfile
 function initialize(filename)
   tempfile = filename
 end
 function comic()
   local body
   local code
   local header
   local sStart
   local sEnd
   local img
   body, code, header = http.request("http://dynamic.xkcd.com/comic/random/")
   sStart, sEnd = string.find(body, "<img src=[\"'](.-)[\"']")
   if sEnd ~= nil then
     img = string.match(body, "<img src=[\"'](.-)[\"']", sEnd)
   else
     img = string.match(body, "<img src=[\"'](.-)[\"']")
   end
   http.request{ 
     url = img, 
     sink = ltn12.sink.file(io.open(tempfile, "wb"))
   }
 end

Usage

To use the module in awesome just require it in your rc.lua and set the filename where it should save the image

 require("xkcd")
 xkcd.settings = {}
 xkcd.settings.filename = os.getenv("HOME").."/.config/awesome/xkcd.png"
 xkcd.initialize(xkcd.settings.filename)
 xkcd.comic()
 naughty.notify({ 
   title = "xkcd",
   text = "Random xkcd comic", 
   hover_timeout = 3, 
   timeout = 10,
   ontop = false,
   icon = xkcd.settings.filename, 
   icon_size = 256
 })
Personal tools