Advertisement
throwawayrobot

~/installer.lua

Feb 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. --[[
  2.   Minecraft OpenComputers application for updating the robbit' files.
  3.   Usage: pastebin run uxkfADHq https://pastebin.com/u/<username>
  4.   Prompts a menu to download and store the files listed on the specified url.
  5.   TODO: updater:
  6.         - prompt, menu, download, store. LOL
  7.         - merge repositories
  8.         - add conflict resolution
  9.         resource:
  10.         - add parsing of post/modification date
  11. ]]
  12.  
  13. local internet = require("internet")
  14. local shell = require("shell")
  15.  
  16. local updater = {
  17.   DEBUG = false;
  18. }
  19. function updater:log(...)
  20.   if self.DEBUG then print(...) end
  21. end
  22.  
  23. function updater:flush(resource)
  24.   repeat table.remove(resource, 1) until #resource == 0
  25. end
  26.  
  27. function updater:sync(resource, url)
  28.   resource.url = url
  29.  
  30.   local success, source = pcall(internet.request, resource.url, {t=os.time()})
  31.   if not success then return nil end
  32.  
  33.   for html in source do
  34.     if resource:search(html) then
  35.       return true
  36.     end
  37.   end
  38.  
  39.   return false
  40. end
  41.  
  42. function updater:run(path)
  43.   updater:log("Directory set to: " .. path)
  44.   for n, file in ipairs(Pastebin) do
  45.     print("["..n.."]: " .. file.path)
  46.   end
  47. end;
  48.  
  49. --[[ -- Repositories ------------------------------------------------------- ]]
  50. local pastebin = {
  51.   url = '';
  52.   buffer = '';
  53.   source_url = "https://pastebin.com/raw/";
  54. }
  55.  
  56. function pastebin:search(chunk)
  57.   local head, head_end = string.find(self.buffer, '<table class="maintable">', 0, true)
  58.   local tail, tail_end = string.find(self.buffer, '</table>', head_end, true)
  59.   if not head or not tail then
  60.     self.buffer = self.buffer .. string.gsub(chunk, "\t", "")
  61.     return false
  62.   end
  63.   self.buffer = string.sub(self.buffer, head_end, tail)
  64.  
  65.   updater:log("Indexing " .. self.url)
  66.   for line in string.gmatch(self.buffer, '[^\r\n]+') do
  67.     for id, name in string.gmatch(line, '[%.]*<a href="/(%w+)">(%g+)</a>[%.]*') do
  68.       updater:log(" ▪ [" .. id .. "]: " .. name)
  69.       table.insert(self, {
  70.         id=id;
  71.         url=self.source_url..id;
  72.         path=name;
  73.       })
  74.     end
  75.   end
  76.  
  77.   self.buffer = ""
  78.   return true
  79. end
  80.  
  81. --[[ -- Command-Line Interface --------------------------------------------- ]]
  82. local args, options = shell.parse(...)
  83. updater.DEBUG = true
  84. if updater:sync(pastebin, args[1] or "https://pastebin.com/u/throwawayrobot") then
  85.   updater:run(args[2] or '.')
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement