Posted 26 February 2014 - 02:14 AM
I created a 'wiki-port'. It searches the real wiki using the MediaWiki api and then tries to cleanup most of the formated json (jsonfm). The result? You can search almost anything without having to minimize and maximize your game.
My code can be adapted to be used with other wikis. The techinique used to parse other information…
It is not ready by any means, I'm still missing the disambiguation pages and handeling the HUGE amount to text it spits. But i'd like to share it anyway so that you guys can use it to do what ever you want to ^^.
My code can be adapted to be used with other wikis. The techinique used to parse other information…
It is not ready by any means, I'm still missing the disambiguation pages and handeling the HUGE amount to text it spits. But i'd like to share it anyway so that you guys can use it to do what ever you want to ^^.
--This method opens the wiki api and retrieves the desired page. It will spit null in case of no page for that item
function searchWiki(term)
--Find the page with the API
h = http.get("http://ftbwiki.org/api.php?action=query&prop=revisions&rvprop=content&format=jsonfm&titles="..term)
--Store all lines
lines = h.readAll()
--Close the handle
h.close()
--If there is no page for that item, return nil
if string.find(lines, ""missing"") then
return nil
end
return lines
end
--This is a util split funcion
function split(str , delim, maxNb)
if string.find(str, delim) == nil then
return str
end
if maxNb == nil or maxNb < 1 then
maxNb = 0
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gmatch(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
if nb ~= maxNb then
result[nb+1] = string.sub(str, lastPos)
end
return result
end
--This function splits the page (json) with the ':' delimiter
--Then it searches for the title and page body, returning both in a table
--like this {"title" = title, "body" = body}
function processJsonFm(lines)
--print(">Json processing started")
local lineTable = split(lines,":")
--print("TableSize: " .. #lineTable)
local titleTag = -1
local bodyTag = -1
local count = 1
local returnTable = {}
for k,v in pairs(lineTable) do
--print("line: " .. v)
if string.find(v,"&quot;title&quot;") then
--print("Found title!")
titleTag = count + 1
end
if string.find(v,"*&quot;") then
--print("Found body!")
bodyTag = count + 1
end
count = count + 1
sleep(0.1)
end
--print("<Json processing ended")
returnTable["title"] = lineTable[titleTag]
returnTable["body"] = lineTable[bodyTag]
return returnTable
end
function cleanupTitle(text)
--print("cleaning: " .. text)
local newMem = text..""
local start = string.find(newMem,"&quot;")
--print("Start quot: " .. start)
local cut1 = string.sub(text,start+6,#newMem)
--print("clean cut1: " .. cut1)
local newMem2 = cut1..""
local ends = string.find(newMem2, "&quot;")
local finalcut = string.sub(newMem2, 0, ends-1)
-- print("clean cutfinal: " .. finalcut)
return finalcut
end
function cleanupBody(text)
local newMem = text..""
newMem = string.gsub(newMem, "&quot;", "")
newMem = string.gsub(newMem, "{{Item", "")
newMem = string.gsub(newMem, "}}", "")
newMem = string.gsub(newMem, "\\u00a0", " ")
newMem = string.gsub(newMem, "{{item", "")
local breakLines = split(newMem,"\\n")
table.remove(breakLines,#breakLines)
table.remove(breakLines,#breakLines)
--print("Splited: " .. #breakLines .. " line breaks.")
return breakLines
end
-----PROGRAM-----
local run = true
local showMenu = true
local monitor = peripheral.wrap("top")
if monitor ~= nil then
monitor.setTextScale(0.5)
end
while run do
local q = ""
--THE MENU--
if showMenu then
term.setCursorPos(1,1)
term.clearLine()
term.write("FTB Wiki: ")
q = io.read()
end
--KEY HANDLER--
if #q <= 1 then
local sEvent, param = os.pullEvent("key")
--Check for q press
if sEvent == "key" then
--User pressed q
if param == 16 then
showMenu = true
end
--User pressed w
if param == 17 then
--scrolling up
end
--User pressed s
if param == 31 then
--scrolling down
end
end
end
--WIKI SEARCH
if #q > 1 then
local query = string.gsub(q, " ","_")
local page = searchWiki(query)
if page == nil then
print("*--------------------------------*")
print("| Page not found! |")
print("*--------------------------------*")
else
term.redirect(monitor)
term.clear()
local res = processJsonFm(page)
print("+--------------------------------+")
print("|" .. cleanupTitle(res["title"]))
print("+--------------------------------+")
--TODO : SCROLL
local bb = cleanupBody(res["body"])
for k,v in pairs(bb) do
print(v)
sleep(0.1)
end
term.restore()
end
q = ""
end
end