Lua cheat sheet
Lua cheat sheet

Lua cheat sheet

Helper methods for Internet access

web.lua
local fs = require("fs")
local lfs = require "lfs"
local str = require("str")

local function request(url)
    if string.find(url, "https://", 1, true)==1 then
        return require("ssl.https").request(url)
    else
        return require("socket.http").request(url)
    end
end

local function cached_request(url, seconds)
    if (seconds==nil) then seconds=3600 end
    local cached_filename = fs.get_temp_dir("http_cache") .. str.encode_url(url) .. ".html"
    local handle, content
    if fs.file_exists(cached_filename) and lfs.attributes(cached_filename).modification > os.time()-seconds then
        handle = io.open(cached_filename)
        content = handle:read("*all")
    else
        content = request(url)
        handle = io.open(cached_filename,"wb")
        handle:write(content)
    end
    io.close(handle)
    return content
end

local function query(keywords, cache_time)
    local result_page = cached_request("https://duckduckgo.com/html/?q=" .. keywords, cache_time);
    local results = {}
    for url in string.gmatch(result_page, 'uddg=(.-)"') do
      local decoded = str.decode_url(url)
      if (results[#results]~=decoded) then
        table.insert(results, decoded)
      end
    end
    return results
end

local function query_links_to(url, cache_time)
    local result_page = cached_request("https://www.gigablast.com/search?dr=0&link=" .. url, cache_time);
    local results = {}
    for url in string.gmatch(result_page, 'uddg=(.-)"') do
      local decoded = str.decode_url(url)
      if (results[#results]~=decoded) then
        table.insert(results, decoded)
      end
    end
    return results
end

local function mail(sender, recipient, subject, text)
	local cmd = "ssmtp " .. recipient
	local f = io.popen(cmd, "w")
	f:write("Subject: " .. subject .. "\n" .. text)
	f:close()
end

return
{
    request = request,
    cached_request = cached_request,
    query = query,
	mail = mail,
	query_links_to = query_links_to
}

Sample usage

local web = require("web")

function print_table(t)
  for k,v in pairs(t) do
    print(k,v)
  end
end

results = web.query("Lua site:en.wikipedia.org")

print_table(results)