Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
#!/usr/bin/env lua5.1
-- This file:
-- http://angg.twu.net/fbcache/fbcache2.lua
-- http://angg.twu.net/fbcache/fbcache2.lua.html
--  (find-angg        "fbcache/fbcache2.lua")
--
-- See: (find-angg "LUA/lua50init.lua" "loadfbcache2")


-- 1. Introduction
-- ================
-- It is easier to understand how this library works if we start with
-- a simple usage case. So, here it is (in the first person!)... Every
-- time that I see an interesting post on Facebook I copy its
-- permalink, which is a URL like this, but often much longer,
--
--   https://www.facebook.com/cezar.migliorin/posts/573841232680302
--   https://www.facebook.com/bruno.cava.7/posts/10204411927997424
--
-- to a file where I keep all these URLs. When I want to find a post
-- again, e.g., to mention it in a discussion, I search my "local
-- cache of interesting Facebook posts" for the right keywords, and I
-- get the text of the post and a URL to where it was originally
-- posted, like the ones above.
--
-- This library builds a "local cache of interesting Facebook posts".
--
--
-- 2. How it works
-- ===============
-- The graph below explains all the main operations.
--
--   browser_url
--        |
--        v
--   browser_url_parts --> fbkind
--                     \      | (*)
--                      v     v
--                        graph_query    token
--                            |       \    |
--                            |        v   v
--                            |          json
--                            |            |
--                            |            v
--                            |        json_table --> output_html
--                            |            ^      \-> output_txt
--                            |            |
--                            v            v
--                      cache_fname <--> json_pp





-- «.requires»			(to "requires")
-- «.curl»			(to "curl")
-- «.token»			(to "token")
-- «.json_parse»		(to "json_parse")
-- «.fbpatcompile»		(to "fbpatcompile")
-- «.fbpatcompile-tests»	(to "fbpatcompile-tests")
-- «.NameIds»			(to "NameIds")
-- «.NameIds-tests»		(to "NameIds-tests")
-- «.Fbkind-class»		(to "Fbkind-class")
-- «.Fbkinds-class»		(to "Fbkinds-class")
-- «.fbkinds»			(to "fbkinds")
-- «.fbkinds-tests»		(to "fbkinds-tests")
-- «.sample-urls»		(to "sample-urls")
-- «.user-id»			(to "user-id")
-- «.user-id-test»		(to "user-id-test")
-- «.gumbo»			(to "gumbo")
-- «.command-line»		(to "command-line")



--                       _               
--  _ __ ___  __ _ _   _(_)_ __ ___  ___ 
-- | '__/ _ \/ _` | | | | | '__/ _ \/ __|
-- | | |  __/ (_| | |_| | | | |  __/\__ \
-- |_|  \___|\__, |\__,_|_|_|  \___||___/
--              |_|                      
--
-- «requires» (to ".requires")

require "posix"
require "lpeg"
require "re"
userocks()         -- (find-angg "LUA/lua50init.lua" "userocks")
pretty = require 'pl.pretty'     -- (find-es "lua5" "pl.pretty")
pp0 = function (o) return pretty.write(o) end
pp  = function (o) print(pretty.write(o)) end

-- (find-es "lua5" "pl.pretty-fix")
longquote = function (str)
    local T = {}
    local f = function (eqs) T[#eqs+1] = 1 end
    if str:gsub("%](=*)", f) then
      local eqs = string.rep("=", #T)
      return '['..eqs..'[\n'..str..']'..eqs..']'
    end
    return '[[\n'..str..']]'
  end

-- (find-es "lua5" "pl.text.wrap")
wrap = (require "pl.text").wrap
wraps = function (bigstr)
    local f = function (li) return table.concat(wrap(li), "\n").."\n" end
    return (bigstr:gsub("([^\n]+)", f))
  end

-- (find-angg "LUA/lua50init.lua" "ee_ls")
no_dots = function (L)
    for i=#L,1,-1 do if L[i]=="." or L[i]==".." then table.remove(L, i) end end
    return L
  end
ee_ls         = function (dir) return (posix.dir(ee_expand(dir))) end
ee_ls_no_dots = function (dir) return sorted(no_dots(ee_ls(dir))) end

cfmt   = function (fmt) return function (...) return format(fmt, ...) end end
ee_fmt = function (fmt) return cfmt(ee_expand(fmt)) end



--                  _ 
--   ___ _   _ _ __| |
--  / __| | | | '__| |
-- | (__| |_| | |  | |
--  \___|\__,_|_|  |_|
--                    
-- «curl» (to ".curl")
fbcurl0 = function (url) return getoutput(format("curl -s '%s'", url)) end
fbcurl1 = function (query)
    local c = query:match"%?" and "&" or "?"
    local url = "https://graph.facebook.com/v2.5/" ..
      query .. c .. "access_token=" .. token
    local shorturl = "$GRAPH25/" .. query .. c .. "$ATOKEN"
    return url, shorturl
  end
fbcurl = function (query)
    local url, shorturl = fbcurl1(query)
    local cmd = format("curl -s '%s'", url)
    print(shorturl)             -- verbose behavior
    return getoutput(cmd)
  end
fbcurl0parse = function (url)   return (json_parse(fbcurl0(url)))  end
fbcurlparse  = function (query) return (json_parse(fbcurl(query))) end
-- fbid_fetch_from_facebook = function (fbid) return fbcurl(fbid) end


-- «token» (to ".token")
-- (find-angg ".pythonrc.py")
-- https://developers.facebook.com/tools/explorer
token = "CAACEdEose0cBABpCSLpeWwpqgcNjZCGdt8x4VbunCQ9pdZBu1Jt9u4zfp2JVtcFPpPWLkVWjoSiSKwnZCZBTtRq2BY7gqy4RbsxC0ZCKiKUJg95owWR8DmRpZAjd1M8sbUtteoTWwqfsqjOpVaccwWbHhua8SFkR867gOQlDwigtn4spZCKeOs1KRZCrzQNhqLFNAhdT3m0wQgZDZD"


--    _                                                
--   (_)___  ___  _ __       _ __   __ _ _ __ ___  ___ 
--   | / __|/ _ \| '_ \     | '_ \ / _` | '__/ __|/ _ \
--   | \__ \ (_) | | | |    | |_) | (_| | |  \__ \  __/
--  _/ |___/\___/|_| |_|____| .__/ \__,_|_|  |___/\___|
-- |__/               |_____|_|                        
--
-- «json_parse» (to ".json_parse")
-- (find-fbcache "fbcache.lua" "json_parse")
-- http://www.json.org/

json_grammar0 = [=[
  objrest <- obj {} [%s]* {.*}
  obj     <- bool / num / str / table / list
  objp    <- obj colon obj
  table   <- ("{" (objp (comma objp)*)* "}") -> totable
  list    <- ("[" (obj  (comma obj )*)* "]") -> tolist
  colon   <- [%s]* ":" [%s]*
  comma   <- [%s]* "," [%s]*
  bool    <- ("true" / "false") -> tobool
  num     <- ("-"? [0-9.]+)      -> tonum

  str <- (ustr / str0)
   ustr        <- "u" str0
   str0        <- ("'" (stritem / {'"'})* -> concat "'") /
                  ('"' (stritem / {"'"})* -> concat '"')
   stritem     <- strcnormals / strcc / strcx / strcu / strcU / strcother
   strcnormals <- {[^'"\]+}
   strcc       <- "\"  {['"\/]}
   strcx       <- "\x" ({[%x][%x]}                       -> hextoc)
   strcu       <- "\u" ({[%x][%x][%x][%x]}                 -> utoc)
   strcU       <- "\U" ({[%x][%x][%x][%x][%x][%x][%x][%x]} -> Utoc)
   strcother   <- "\" . -> otherc
]=]
json_chars_b = {n="\n", r="\r", t="\t", b="\b", ["/"]="/"}
json_chars_u = {["2018"]='"', ["2019"]='"', ["201c"]='"', ["201d"]='"',
                ["2013"]="---", ["2022"]="*", }
json_chars_U = {}
json_defs = {
  tobool  = function (s) return s == "True" and true or false end,
  tonum   = function (s) return tonumber(s) end,
  tostr   = function (s) return s end,
  tolist  = function (...) return {...} end,
  totable = function (...)
      local L, T = {...}, {}
      for i=1,#L-1,2 do T[L[i]] = L[i+1] end
      return T
    end,
  hextoc  = function (hh) return string.char(tonumber(hh, 16)) end,
  utoc   = function  (h4)
      local n = tonumber(h4, 16)
      if n < 256 then return string.char(n) end
      return json_chars_u[h4] or "\\u"..h4
    end,
  Utoc   = function  (h8) return "\\U"..h8 end,
  concat  = function (...) return table.concat {...} end,
  otherc  = function (c)
      if json_chars_b[c] then return json_chars_b[c] end
      print("\\"..c)
      return "\\"..c
    end,
}
json_grammar = re.compile(json_grammar0, json_defs)
json_parse   = function (bigstr, pos) return json_grammar:match(bigstr, pos) end
json_test    = function (bigstr, pos) pp(json_parse(bigstr, pos)) end




--   __ _                 _       
--  / _| |__  _ __   __ _| |_ ___ 
-- | |_| '_ \| '_ \ / _` | __/ __|
-- |  _| |_) | |_) | (_| | |_\__ \
-- |_| |_.__/| .__/ \__,_|\__|___/
--           |_|                  
--
-- «fbpatcompile» (to ".fbpatcompile")
-- (find-es "lua-intro" "lpeg-quickref")

PatN = (lpeg.R("09")^1):C()
PatO = ((lpeg.R("09") + lpeg.S(" "))^1):C()
PatW = ((lpeg.R("09") + lpeg.R("AZ") + lpeg.R("az") + lpeg.S("."))^1):C()
PatR = (lpeg.P(1)^0):C()
PatR = lpeg.Cp():Cg("rpos") * (lpeg.P(1)^0):C()
fbpatcompile = function (pat, kindname, kindn)
    pat = fbpatcompile0(pat)
    local ppos   = lpeg.Cp():Cg("bpos")
    local pkind  = lpeg.Cc(kindname):Cg("kind")
    local pkindn = lpeg.Cc(kindn):Cg("kindn")
    return (ppos * pat * pkind * pkindn):Ct()
  end
fbpatcompile0 = function (pat) return expr(fbpatexpand(pat)) end
fbpatexpand = function (pat)
    local mkp       = function (s) return "Pat"..s end
    local mks       = function (s) return 'lpeg.P"'..s..'"' end
    local mka       = function (...) return table.concat({...}, " * ") end
    local Upcase    = lpeg.R("AZ")
    local NonUpcase = (1 - Upcase)^1
    local Parts     = ((NonUpcase/mks) * (Upcase/mkp))^1
    return lpeg.match(Parts/mka, pat)
  end

-- «fbpatcompile-tests» (to ".fbpatcompile-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
pat   = "/events/N/permalink/N/R"
pate  = fbpatexpand  (pat)
patc0 = fbpatcompile0(pat)
patc  = fbpatcompile (pat, "eventp")
uu   = "https://www.facebook.com/events/1397247553878845/permalink/1412951502308450/"
u    =                         "/events/1397247553878845/permalink/1412951502308450/"
= pate
--> lpeg.P"/events/" * PatN * lpeg.P"/permalink/" * PatN * lpeg.P"/" * PatR
= patc0:match(u)
--> 1397247553878845	1412951502308450
PP(patc:match(u))
--> {1="1397247553878845", 2="1412951502308450", 3="", "bpos"=1, "kind"="eventp", "rpos"=53}

patt = lpeg.P"https://www.facebook.com" * patc
PP(patt:match(uu))

--]]




--  _   _                      ___    _     
-- | \ | | __ _ _ __ ___   ___|_ _|__| |___ 
-- |  \| |/ _` | '_ ` _ \ / _ \| |/ _` / __|
-- | |\  | (_| | | | | | |  __/| | (_| \__ \
-- |_| \_|\__,_|_| |_| |_|\___|___\__,_|___/
--                                          
-- «NameIds» (to ".NameIds")
-- (find-angg "LUA/lua50init.lua" "SetL")
NameIds = Class {
  type = "NameIds",
  new  = function () return NameIds {setl=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,
    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
  },
}

nameids = NameIds.new()

-- «NameIds-tests» (to ".NameIds-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
nameids = NameIds.new():readstring [[
me 123
you 456
]]
= nameids

nameids = NameIds.new():readfile("nameids")
= nameids
= nameids.setl:n()

--]==]



--  _____ _     _    _           _        _               
-- |  ___| |__ | | _(_)_ __   __| |   ___| | __ _ ___ ___ 
-- | |_  | '_ \| |/ / | '_ \ / _` |  / __| |/ _` / __/ __|
-- |  _| | |_) |   <| | | | | (_| | | (__| | (_| \__ \__ \
-- |_|   |_.__/|_|\_\_|_| |_|\__,_|  \___|_|\__,_|___/___/
--                                                        
-- «Fbkind-class» (to ".Fbkind-class")

Fbkind = Class {
  type = "Fbkind",
  new  = function (kindn, kindname, pat, comment)
      local patc = fbpatcompile(pat, kindname, kindn)
      local fbk = Fbkind {kindn=kindn, kindname=kindname,
                          pat=pat, patc=patc, comment=comment}
      return fbk
    end,
  fromline = function (kindn, line)
      local spl = split(line)
      return Fbkind(kindn, spl[1], spl[2], spl[3])
    end,
  __tostring = function (fbk) return fbk:tostring() end,
  __index = {
    tostring = function (fbk)
        return format("%2s  %-12s  %-40s  %s",
                      fbk.kindn, fbk.kindname, fbk.pat, fbk.comment)
      end
  },
}


--  _____ _     _    _           _                _               
-- |  ___| |__ | | _(_)_ __   __| |___        ___| | __ _ ___ ___ 
-- | |_  | '_ \| |/ / | '_ \ / _` / __|_____ / __| |/ _` / __/ __|
-- |  _| | |_) |   <| | | | | (_| \__ \_____| (__| | (_| \__ \__ \
-- |_|   |_.__/|_|\_\_|_| |_|\__,_|___/      \___|_|\__,_|___/___/
--                                                                
-- «Fbkinds-class» (to ".Fbkinds-class")

Fbkinds = Class {
  type = "Fbkinds",
  new  = function () return Fbkinds {} end,
  from = function (bigstr) return Fbkinds.new():addlines(bigstr) end,
  __tostring = function (fbks) return fbks:tostring() end,
  __index = {
    add = function (fbks, kindname, pat, comment)
        local kindn = #fbks + 1
        local fbk = Fbkind.new(kindn, kindname, pat, comment)
        fbks[kindn]    = fbk
        fbks[kindname] = fbk
        fbks.pat = fbks:makepat()
      end,
    addline = function (fbks, line)
        local spl = split(line)
        fbks:add(spl[1], spl[2], spl[3])
        return fbks
      end,
    addlines = function (fbks, bigstr)
        for _,line in ipairs(splitlines(bigstr)) do fbks:addline(line) end
        return fbks
      end,
    --
    makepat0 = function (fbks)
        local pat = fbks[1].patc
        for i=2,#fbks do pat = pat + fbks[i].patc end
        return pat
      end,
    makepat = function (fbks)
        return lpeg.P"https://www.facebook.com" * fbks:makepat0()
      end,
    --
    match = function (fbks, url)
        return fbks.pat:match(url)
      end,
    tostring = function (fbks)
        local f = function (i) return fbks[i]:tostring() end
        return mapconcat(f, seq(1, #fbks), "\n")
      end,
  },
}


--   __ _     _    _           _     
--  / _| |__ | | _(_)_ __   __| |___ 
-- | |_| '_ \| |/ / | '_ \ / _` / __|
-- |  _| |_) |   <| | | | | (_| \__ \
-- |_| |_.__/|_|\_\_|_| |_|\__,_|___/
--                                   
-- «fbkinds» (to ".fbkinds")
-- (find-fbcache "fbcache.lua" "fbkinds")

fbkinds_bigstr = [[
  eventnpn   /events/N/permalink/N/R              21_ok
  eventnn    /events/N/N/R                        21_ok
  eventn     /events/N/R                          1_ok
  groupnpn   /groups/N/permalink/N/R              21_ok
  groupwpn   /groups/W/permalink/N/R              0_nothingworks
  groupnn    /groups/N/NR                         1_whydoesnt2work
  groupwn    /groups/W/NR                         2_ok
  groupn     /groups/NR                           1_ok
  groupw     /groups/WR                           0_how_to_convert_name->id?
  mediawannn /W/media_set?set=a.N.N.NR            0_nothingworks
  mediawnnn  /media/set/?set=W.N.N.NR             0_nothingworks
  mediawn    /media/set/?set=W.NR                 0_nothingworks
  noteswwn   /notes/W/W/NR                        0_3shouldworkbutdeprecated
  notesn     /notes/N/R                           0_1shouldworkbutdeprecated
  pageswn    /pages/W/NR                          2_ok
  storynn    /permalink.php?story_fbid=N&id=NR    12_ok
  photonn    /photo.php?fbid=N&set=t.NR           0_nothingworks
  photonnnn  /photo.php?fbid=N&set=a.N.N.NR       0_nothingworks
  photono    /photo.php?fbid=N&set=OR             1
  photot     /N/photos/t.N/N/R                    13
  photogm    /N/photos/gm.N/N/R                   123
  photona    /N/photos/a.N.N.N/N/R                1245
  photowa    /W/photos/a.N.N.N/N/R                0_untested
  photovs    /photo.php?v=N&set=OR                1
  photov     /photo.php?v=NR                      1
  postwn     /W/posts/NR                          2
  videon     /video.php?v=NR                      1
]]
fbkinds = Fbkinds.from(fbkinds_bigstr)

fbkinds_in_file = function (fname) return fbkinds_in_string(ee_readfile(fname)) end
fbkinds_in_string = function (bigstr)
    return cow(function ()
        for _,url in ipairs(split(bigstr)) do
          local fbk = fbkinds:match(url)
          if fbk then coy(fbk, url) end
        end
      end)
  end

allfbks = function () return fbkinds_in_file("~/fbcache/all-fb-urls.lst") end
allfbksk = function (kind)
    return cow(function ()
        for fbk,url in allfbks() do
          if fbk.kind == kind then coy(fbk, url) end
        end
      end)
  end

-- «fbkinds-tests» (to ".fbkinds-tests")
--[[
-- (find-lpegw3m "lpeg.html#f-match")
-- (find-es "lua5" "lpeg-quickref")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
= fbkinds
PP(keys(fbkinds))
PP(fbkinds[1])

pat0 = "/events/N/permalink/N/R"
uu   = "https://www.facebook.com/events/1397247553878845/permalink/1412951502308450/"
u    =                         "/events/1397247553878845/permalink/1412951502308450/"
PP(fbkinds[1].patc:match(u))
PP(fbkinds:makepat0():match(u))
PP(fbkinds:match(uu))

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
bigstr = ee_readfile "~/fbcache/all-fb-urls.lst"
for _,url in ipairs(split(bigstr)) do
  local p = fbkinds:match(url)
  if p then print(p.kind, url) else print("???", url) end
end

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
for fbk in fbkinds_in_file("~/fbcache/all-fb-urls.lst") do
  if fbk.kind:match("^group") then print(fbk[1]) end
end

for fbk in fbkinds_in_file("~/TODO") do
  if fbk.kind:match("^group") then print(fbk[1]) end
end

--]]



--  ____                        _        _   _ ____  _         
-- / ___|  __ _ _ __ ___  _ __ | | ___  | | | |  _ \| |    ___ 
-- \___ \ / _` | '_ ` _ \| '_ \| |/ _ \ | | | | |_) | |   / __|
--  ___) | (_| | | | | | | |_) | |  __/ | |_| |  _ <| |___\__ \
-- |____/ \__,_|_| |_| |_| .__/|_|\___|  \___/|_| \_\_____|___/
--                       |_|                                   
--
-- «sample-urls» (to ".sample-urls")
fbkinds_sample_urls_bigstr = [[
  eventnpn   https://www.facebook.com/events/1397247553878845/permalink/1412951502308450/
]]



--  _   _                 ___ ____      
-- | | | |___  ___ _ __  |_ _|  _ \ ___ 
-- | | | / __|/ _ \ '__|  | || | | / __|
-- | |_| \__ \  __/ |     | || |_| \__ \
--  \___/|___/\___|_|    |___|____/|___/
--
-- A hack to convert usernames to userids using wget and grep.
-- It doesn't always work.
-- See: (find-es "facebook" "fb-user-id")
-- «user-id» (to ".user-id")

fbwget0 = function (url)
    return 'cd /tmp/fb/ && wget -nc -U "$MOZILLA_USER_AGENT" "'..url..'"'
  end
fbwget1 = function (url)
    getoutput("mkdir -p /tmp/fb/")
    print(getoutput(fbwget0(url)))
  end
fbwget2 = function (page) fbwget1("https://www.facebook.com/"..page) end
fbwget3 = function (page)
    return "cat /tmp/fb/"..page.." 2>&1 | tr ' \"' '\\n\\n' | grep fb://profile/"
  end
fbwget4 = function (page) return getoutput(fbwget3(page)) end
fbwget5 = function (str) return str:match("fb://profile/(%d+)") end
fbwget6 = function (page) return fbwget5(fbwget4(page)) end

getuseridls = function () return ee_ls_no_dots("/tmp/fb/") end
getuserid0 = function (user) fbwget2(user) end
getuserid1 = function (user) return fbwget6(user) end

-- «user-id-test» (to ".user-id-test")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
user = "thadeu.penna"
= fbwget0(user)
= fbwget3(user)

= getoutput("rm -v /tmp/fb/"..user)
= fbwget2(user)
= fbwget6(user)
--> 1419700496

id = fbwget6(user)
o = fbcurlparse(id)
pp(o)
--> { name = "Thadeu Penna", id = "1419700496" }

--]]



--                        _           
--   __ _ _   _ _ __ ___ | |__   ___  
--  / _` | | | | '_ ` _ \| '_ \ / _ \ 
-- | (_| | |_| | | | | | | |_) | (_) |
--  \__, |\__,_|_| |_| |_|_.__/ \___/ 
--  |___/                             
--
-- «gumbo» (to ".gumbo")
geturltitle = function (url)
    gumbo = require "gumbo"
    local bigstr = getoutput(format("curl -s '%s'", url))
    local document = gumbo.parse(bigstr)
    local title = document.title
    return title
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
url = "http://www.brasildefato.com.br/node/33509"
= geturltitle(url)

--]]






--                                                _    _ _            
--   ___ ___  _ __ ___  _ __ ___   __ _ _ __   __| |  | (_)_ __   ___ 
--  / __/ _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |  | | | '_ \ / _ \
-- | (_| (_) | | | | | | | | | | | (_| | | | | (_| |  | | | | | |  __/
--  \___\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|  |_|_|_| |_|\___|
--                                                                    
-- «command-line» (to ".command-line")
-- Process command-line arguments, to run this as a script

local a,b = ...
-- PP(a, b)
if a then
  if     a == "-fbcurl"     then pp(fbcurlparse(b))
  elseif a == "-urlsofkind" then for fbk,url in allfbksk(b) do print(url) end
  elseif a == "-urltitle"   then print(geturltitle(b))
  else print("Unrecognized command")
  end
end

-- (find-sh "~/fbcache/fbcache2.lua -fbcurl 1412951502308450")
-- (find-fbcurl "1412951502308450")

--[[
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
~/fbcache/fbcache2.lua -urlsofkind photonn
~/fbcache/fbcache2.lua -urltitle http://www.brasildefato.com.br/node/33509 

--]]




--  _____ _       
-- | ____| |_ ___ 
-- |  _| | __/ __|
-- | |___| || (__ 
-- |_____|\__\___|
--                

--[[
-- (find-lpegw3m "lpeg.html#f-match")
-- (find-es "lua5" "lpeg-quickref")

-- Prepare a table of "name->id"s and save it to a file.
-- Experimental, incomplete, etc.
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "fbcache2.lua"
bigstr = ee_readfile "~/fbcache/all-fb-urls.lst"
s = SetL.new()
for _,url in ipairs(split(bigstr)) do
  local p = fbkinds:match(url)
  if p and p.kind == "postwn" then PP(p[1]) end
  if p and p.kind == "postwn" then s:add(p[1], "") end
end
= s

nameids = NameIds.new():readfile("nameids")
= nameids.setl:n()
= s - nameids.setl


for name in (s - nameids.setl):gen() do fbwget1(name) end

for _,name in ipairs(ee_ls_no_dots("/tmp/fb/")) do print(name, fbwget5(name)) end

= nameids.setl:n()
for _,name in ipairs(ee_ls_no_dots("/tmp/fb/")) do
  local id = fbwget5(name)
  if id then print(name, id) end
  if id then nameids:add(name, id) end
end
= nameids.setl:n()
= nameids




for name,p in s:gen() do
  PP(name, p)
end

for name,p in s:gen() do
  if not p.id then
    local o = fbcurlparse(name)
    if o and o.id then p.id = o.id end
    PP(p)
  end
end

for name,p in s:gen() do
  if not p.id then PP(p) end
end

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- (find-fline "/tmp/fb/")
-- (find-sh "rm -Rv /tmp/fb/")
getoutput("mkdir /tmp/fb/")
= getoutput('cd /tmp/fb/ && wget -U "$MOZILLA_USER_AGENT" https://www.facebook.com/thadeu.penna')

fbwget0 = function (url)
    getoutput("mkdir /tmp/fb/")
    print(getoutput('cd /tmp/fb/ && wget -U "$MOZILLA_USER_AGENT" "'..url..'"')
  end
fbwget = function (page) fbwget0("https://www.facebook.com/"..page) end

    

-- https://www.facebook.com/thadeu.penna
-- https://www.facebook.com/CopBlock
-- http://stackoverflow.com/questions/30886468/facebook-cannot-query-users-by-their-username-solution

name = "thadeu.penna"
o = fbcurlparse(name)
PP(o)


for name,p in s:gen() do
  if p.id then printf("%-30s  %s\n", p.name, p.id) end
end

--]]






-- Local Variables:
-- coding: raw-text-unix
-- End: