|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- fbnameid.lua: tools to convert names of FB users and groups to numeric ids.
-- This file:
-- http://angg.twu.net/fbcache/fbnameid.lua
-- http://angg.twu.net/fbcache/fbnameid.lua.html
-- (find-angg "fbcache/fbnameid.lua")
--
-- (find-fbcache "fbcache2.lua" "NameIds")
-- (find-fbcache "fbcache2.lua" "NameIds-tests")
-- (find-fbcache "fbcache2.lua" "user-id")
-- (find-fbcache "fbcache2.lua" "user-id-tests")
-- «.NameIds-class» (to "NameIds-class")
-- «.NameIds-tests» (to "NameIds-tests")
-- «.nameids» (to "nameids")
-- «.nameids_read» (to "nameids_read")
-- «.nameid_maybe» (to "nameid_maybe")
-- «.fbget_maybe» (to "fbget_maybe")
require "fbcache3" -- (find-fbcache "fbcache3.lua")
-- _ _ ___ _
-- | \ | | __ _ _ __ ___ ___|_ _|__| |___
-- | \| |/ _` | '_ ` _ \ / _ \| |/ _` / __|
-- | |\ | (_| | | | | | | __/| | (_| \__ \
-- |_| \_|\__,_|_| |_| |_|\___|___\__,_|___/
--
-- «NameIds-class» (to ".NameIds-class")
-- (find-angg "LUA/lua50init.lua" "SetL")
NameIds = Class {
type = "NameIds",
new = function () return NameIds {setl=SetL.new(), missing=SetL.new()} end,
__tostring = function (ni) return ni:tostring() end,
__index = {
n = function (ni) return ni.setl:n() end,
add = function (ni, name, id) ni.setl:add(name, id); return ni end,
nameid = function (ni, name) return ni.setl:val(name) end,
--
addmissing = function (ni, name, kind) ni.missing:add(name, kind); end,
get = function (ni, name, kind)
return ni:nameid(name) or ni:addmissing(name, kind)
end,
genmissing = function (ni) return ni.missing:gen() end,
--
tostring = function (ni)
local f = function (name) return format("%-30s %s", name, ni:nameid(name)) end
return mapconcat(f, ni.setl:ks(), "\n")
end,
--
readstring = function (ni, bigstr)
for _,line in ipairs(splitlines(bigstr)) do
local name,id = unpack(split(line))
if name and id then ni:add(name, id) end
end
return ni
end,
readfile = function (ni, fname)
return ni:readstring(ee_readfile(fname))
end,
writefile = function (ni, fname)
ee_writefile(fname, ni:tostring().."\n")
return ni
end,
},
}
-- «NameIds-tests» (to ".NameIds-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbnameid.lua"
nameids = NameIds.new():readstring [[
me 123
you 456
]]
= nameids
nameids = NameIds.new():readfile("nameids")
= nameids
= nameids.setl:n()
= nameids:n()
--]==]
-- «nameids» (to ".nameids")
-- «nameids_read» (to ".nameids_read")
-- Read and write the nameid table from/to disk.
-- (find-fbcache "nameids")
nameids_read0 = function ()
nameids = NameIds.new():readfile("~/fbcache/nameids")
end
nameids_read = function () if not nameids then nameids_read0() end end
nameids_write = function () nameids:writefile("~/fbcache/nameids") end
-- nameids_read()
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbnameid.lua"
nameids_read()
= nameids:n()
--]]
-- «nameid_maybe» (to ".nameid_maybe")
-- «fbget_maybe» (to ".fbget_maybe")
nameid = function (name, kind) return nameids:get(name, kind) end
nameid_maybe = function (name, kind) return nameids:get(name, kind or "?") or "?" end
fbget_maybe = function (query) if not query:match"%?" then return fbget(query) end end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbnameid.lua"
nameids_read()
nameid("foo", "user")
nameid("bar", "page")
nameid("boo", "user")
= table.concat(nameids.missing.list, "\n")
for name,kind in nameids:genmissing() do print(name, kind) end
--]]
-- Local Variables:
-- coding: raw-text-unix
-- End: