diff options
author | bie <bie@blekksprut.net> | 2017-07-02 16:19:59 +0200 |
---|---|---|
committer | bie <bie@blekksprut.net> | 2017-07-02 16:19:59 +0200 |
commit | f1d4dfce67c37626ef0bb993c801dacf4f63849a (patch) | |
tree | 91109049cdef491b9ce96c02d0c4ae14f07b8383 /lupin.lua | |
parent | 098fe27e2df8d3519117df48ec7b2a3308556672 (diff) | |
download | lupin-f1d4dfce67c37626ef0bb993c801dacf4f63849a.tar.xz |
bedre cache-håndtering
Diffstat (limited to 'lupin.lua')
-rwxr-xr-x | lupin.lua | 50 |
1 files changed, 31 insertions, 19 deletions
@@ -8,14 +8,36 @@ require 'corz' CACHE = "/home/bie/tmp.db" cache = tokyocabinet.bdbnew() +cache:open(CACHE, cache.OWRITER + cache.OREADER) + +function userurl(basename, basecache, home, fallback) + local path = ("/home/%s/%s"):format(home, basename) + local cache = ("/srv/www/users/%s"):format(basecache) + if not posix.stat(path) then return fallback end + if not posix.stat(cache) then + posix.unlink(cache) + posix.link(path, cache, true) + end + return ("https://intern.radionova.no/users/%s"):format(basecache) +end + +function userfile(basename, home, fallback) + local path = ("/home/%s/%s"):format(home, basename) + local ok, data = pcall(function() + return io.open(path):read("*l") + end) + if ok then + return data:gsub("[\r\n]*", "") + else + return fallback + end +end function oembed(endpoint, id) - cache:open(CACHE, cache.OWRITER + cache.OREADER) local url = endpoint .. id local cached = cache:get(id) if cached then local data = json.decode(cached) - cache:close() return data.html end @@ -25,9 +47,8 @@ function oembed(endpoint, id) end}:perform() if result:getinfo(http.INFO_RESPONSE_CODE) == 200 then cache:put(id, body) - cache:close() local data = json.decode(body) - return data.html + return "<div class=embed>" .. data.html .. "</div>" else return ("<b>Klarte ikke embedde %s :(</b>"):format(url) end @@ -54,7 +75,7 @@ function twitch(id) end function instagram(id) - return oembed("https://api.instagram.com/oembed?url=", id) + return oembed("https://api.instagram.com/oembed/?url=", id) end function flickr(id) @@ -319,23 +340,14 @@ function lupin.transform(path, format, env, mode) if path ~= "-" then local stat = posix.stat(path) local pwd = posix.getpasswd(stat.uid) + data.author = pwd.name + data.authorname = userfile("navn", data.author, data.author) + data.group = posix.getgroup(stat.gid).name - -- if data.published then - -- data.date = os.date("%A %d. %B %Y, kl. %H.%M", data.published) - -- else - -- local root = "/srv/www/happiest.place/tears/artikler" - -- y, m, d = file:match(root .. "/(.*)/(.*)/(.*)/(.*)") - -- data.date = ("%s/%s/%s"):format(y, m, d) - -- end - -- if posix.stat(pwd.dir .. "/portrait.jpg") then - if posix.stat(("/srv/www/users/%s.jpg"):format(data.author)) then - data.portrait = ("/users/%s.jpg"):format(data.author) - end + data.groupname = userfile("navn", data.group, data.group) - if posix.stat(pwd.dir .. "/.name") then - data.author = io.open(pwd.dir .. "/.name"):read("*l") - end + data.portrait = userurl("selfie.jpg", data.author .. ".jpg", data.author) end if format == "later" then |