t8rt0t

redditAPI.lua

Apr 28th, 2024 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.15 KB | None | 0 0
  1. local function generatePath(subreddit)
  2.     if type(subreddit) ~= "string" then subreddit = "*" end
  3.     return "https://archive.is/http:reddit.com/r/"..subreddit.."/comments/*"
  4. end
  5.  
  6. local function getLinks(path,archive_links,completed_paths,verbose)
  7.     local request = http.get(path)
  8.     if verbose then print(path) end
  9.     if not request then return archive_links end
  10.     local html = request.readAll()
  11.     request.close()
  12.     if type(archive_links) ~= "table" then archive_links = {} end
  13.     if type(completed_paths) ~= "table" then completed_paths = {} end
  14.     completed_paths[path] = true
  15.     for str in string.gmatch(html,"href=\"[%w%p]+\"") do
  16.         local link = str:sub(7,-2)
  17.         if string.match(link,"^https://archive%.is/%w%w%w%w%w$") then
  18.             table.insert(archive_links,link)
  19.         elseif string.match(link,"https://archive%.is/offset=%d%d") then
  20.             if not completed_paths[link] then
  21.                 getLinks(link,archive_links,completed_paths,verbose)
  22.             end
  23.         end
  24.     end
  25.     return archive_links
  26. end
  27.  
  28.  
  29.  
  30.  
  31. local links = getLinks(generatePath("politics"))
  32. print(textutils.serialise(links),#links)
Advertisement
Add Comment
Please, Sign In to add comment