Advertisement
LDDestroier

STD-GUI Jouhou List (nonfunctional)

Feb 16th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------------------------------ utils
  2. local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
  3. local function isArray(t)
  4.     local max = 0
  5.     for k,v in pairs(t) do
  6.         if type(k) ~= "number" then
  7.             return false
  8.         elseif k > max then
  9.             max = k
  10.         end
  11.     end
  12.     return max == #t
  13. end
  14. local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
  15. local function removeWhite(str)
  16.     while whites[str:sub(1, 1)] do
  17.         str = str:sub(2)
  18.     end
  19.     return str
  20. end
  21. local encodeCommon, arrEncoding, parseNumber, parseString, parseArray, parseMember, parseValue, parseObject, decode, decodeFromFile
  22.  
  23. ------------------------------------------------------------------ encoding
  24.  
  25. function encodeCommon(val, pretty, tabLevel, tTracking)
  26.     local str = ""
  27.     local function tab(s)
  28.         str = str .. ("\t"):rep(tabLevel) .. s
  29.     end
  30.     function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
  31.         str = str .. bracket
  32.         if pretty then
  33.             str = str .. "\n"
  34.             tabLevel = tabLevel + 1
  35.         end
  36.         for k,v in iterator(val) do
  37.             tab("")
  38.             loopFunc(k,v)
  39.             str = str .. ","
  40.             if pretty then str = str .. "\n" end
  41.         end
  42.         if pretty then
  43.             tabLevel = tabLevel - 1
  44.         end
  45.         if str:sub(-2) == ",\n" then
  46.             str = str:sub(1, -3) .. "\n"
  47.         elseif str:sub(-1) == "," then
  48.             str = str:sub(1, -2)
  49.         end
  50.         tab(closeBracket)
  51.     end
  52.     if type(val) == "table" then
  53.         assert(not tTracking[val], "Cannot encode a table holding itself recursively")
  54.         tTracking[val] = true
  55.         if isArray(val) then
  56.             arrEncoding(val, "[", "]", ipairs, function(k,v)
  57.                 str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
  58.             end)
  59.         else
  60.             arrEncoding(val, "{", "}", pairs, function(k,v)
  61.                 assert(type(k) == "string", "JSON object keys must be strings", 2)
  62.                 str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
  63.                 str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
  64.             end)
  65.         end
  66.     elseif type(val) == "string" then
  67.         str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
  68.     elseif type(val) == "number" or type(val) == "boolean" then
  69.         str = tostring(val)
  70.     else
  71.         error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
  72.     end
  73.     return str
  74. end
  75. local function encode(val)
  76.     return encodeCommon(val, false, 0, {})
  77. end
  78. local function encodePretty(val)
  79.     return encodeCommon(val, true, 0, {})
  80. end
  81.  
  82. ------------------------------------------------------------------ decoding
  83.  
  84. local decodeControls = {}
  85. for k,v in pairs(controls) do
  86.     decodeControls[v] = k
  87. end
  88. local function parseBoolean(str)
  89.     if str:sub(1, 4) == "true" then
  90.         return true, removeWhite(str:sub(5))
  91.     else
  92.         return false, removeWhite(str:sub(6))
  93.     end
  94. end
  95. local function parseNull(str)
  96.     return nil, removeWhite(str:sub(5))
  97. end
  98. local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
  99. function parseNumber(str)
  100.     local i = 1
  101.     while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
  102.         i = i + 1
  103.     end
  104.     local val = tonumber(str:sub(1, i - 1))
  105.     str = removeWhite(str:sub(i))
  106.     return val, str
  107. end
  108. function parseString(str)
  109.     str = str:sub(2)
  110.     local s = ""
  111.     while str:sub(1,1) ~= "\"" do
  112.         local next = str:sub(1,1)
  113.         str = str:sub(2)
  114.         assert(next ~= "\n", "Unclosed string")
  115.  
  116.         if next == "\\" then
  117.             local escape = str:sub(1,1)
  118.             str = str:sub(2)
  119.  
  120.             next = assert(decodeControls[next..escape], "Invalid escape character")
  121.         end
  122.         s = s .. next
  123.     end
  124.     return s, removeWhite(str:sub(2))
  125. end
  126. function parseArray(str)
  127.     str = removeWhite(str:sub(2))
  128.     local val = {}
  129.     local i = 1
  130.     while str:sub(1, 1) ~= "]" do
  131.         local v = nil
  132.         v, str = parseValue(str)
  133.         val[i] = v
  134.         i = i + 1
  135.         str = removeWhite(str)
  136.     end
  137.     str = removeWhite(str:sub(2))
  138.     return val, str
  139. end
  140. function parseValue(str)
  141.     local fchar = str:sub(1, 1)
  142.     if fchar == "{" then
  143.         return parseObject(str)
  144.     elseif fchar == "[" then
  145.         return parseArray(str)
  146.     elseif tonumber(fchar) ~= nil or numChars[fchar] then
  147.         return parseNumber(str)
  148.     elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
  149.         return parseBoolean(str)
  150.     elseif fchar == "\"" then
  151.         return parseString(str)
  152.     elseif str:sub(1, 4) == "null" then
  153.         return parseNull(str)
  154.     end
  155.     return nil
  156. end
  157. function parseMember(str)
  158.     local k = nil
  159.     k, str = parseValue(str)
  160.     local val = nil
  161.     val, str = parseValue(str)
  162.     return k, val, str
  163. end
  164. function parseObject(str)
  165.     str = removeWhite(str:sub(2))
  166.     local val = {}
  167.     while str:sub(1, 1) ~= "}" do
  168.         local k, v = nil, nil
  169.         k, v, str = parseMember(str)
  170.         val[k] = v
  171.         str = removeWhite(str)
  172.     end
  173.     str = removeWhite(str:sub(2))
  174.     return val, str
  175. end
  176. function decode(str)
  177.     str = removeWhite(str)
  178.     t = parseValue(str)
  179.     return t
  180. end
  181. function decodeFromFile(path)
  182.     local file = assert(fs.open(path, "r"))
  183.     local decoded = decode(file.readAll())
  184.     file.close()
  185.     return decoded
  186. end
  187.  
  188.  
  189. std.storeCatagoryNames = {}
  190. local boardIDs = {}
  191. local req = http.get("http://backend.jouhou.ml/api/Boards")
  192. if not req then
  193.     return false
  194. end
  195. req = decode(req.readAll())
  196. for a = 1, #req do
  197.     boardIDs[req[a].id] = a
  198.     std.storeCatagoryNames[#std.storeCatagoryNames+1] = req[a].name
  199. end
  200.  
  201. local req = http.get("http://backend.jouhou.ml/api/Posts")
  202. if not req then
  203.     return false
  204. end
  205. req = decode(req.readAll())
  206. std.storeURLs = {}
  207. local buffer
  208. for k,v in pairs(req) do
  209.     buffer = {
  210.         title = v.id,
  211.         url = {v.id},
  212.         creator = "n/a",
  213.         description = "Timestamp: "..v.creation,
  214.         catagory = boardIDs[v.id],
  215.         forumPost = "N/A ",
  216.         keywords = {tostring(v.content)},
  217.     }
  218.     std.storeURLs[v.code] = buffer
  219. end
  220.  
  221. std.contextualGet = function(appid)
  222.     local url = "http://backend.jouhou.ml/api/Posts/"
  223.     local out = http.get(url..appid)
  224.     if out then
  225.         out = decode(out.readAll())
  226.         return out.content
  227.     else
  228.         return false
  229.     end
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement