t8rt0t

reddit.lua

Jul 31st, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. -- local args = {...}
  2. -- local subreddit = "politics"
  3. -- if args and args[1] then subreddit = args[1] end
  4. -- local archive = http.get("https://archive.is/http://reddit.com/r/"..subreddit.."/comments/*")
  5. -- local data = archive.readAll()
  6. -- archive.close()
  7.  
  8. local function split(input,sep)
  9.     sep = sep or "%s"
  10.     local t = {}
  11.     for str in string.gmatch(input,"([^"..sep.."]+)") do
  12.         table.insert(t,str)
  13.     end
  14.     return t
  15. end
  16.  
  17. local function match(input,pattern)
  18.     local t = {}
  19.     for str in string.gmatch(input,pattern) do
  20.         table.insert(t,str)
  21.     end
  22.     return t
  23. end
  24.  
  25. local function getArchiveLinks(data)
  26.     return match(data, "\"https://archive.is/%w%w%w%w%w\"")
  27. end
  28.  
  29. -- local archiveLinks = getArchiveLinks(data)
  30. -- local link = archiveLinks[math.random(#archiveLinks)]
  31. -- --print(textutils.serialise(getArchiveLinks(data)))
  32. -- local pol = http.get(string.sub(link,2,-2))
  33. -- --local pol = http.get("https://archive.is/DFDQK")
  34. -- local polData = pol.readAll()
  35. -- pol.close()
  36. -- --local splitPolData = match(polData,"................................................................................................This is the definition.....................................")
  37. -- --local splitPolData = match(polData,"<form action=\"#\".-</form>")
  38. -- --local index = string.find(polData, "This is the definition")
  39. -- --local index = 319236
  40. -- --print(index)
  41. -- --local splitPolData = {string.sub(polData,index-2000,index-1400),
  42. -- --string.sub(polData,index-30,index+40)}
  43. -- local comments = match(polData,"<form action=\"#\".-</form>")
  44. -- local comment = comments[math.random(#comments)]
  45. -- splitComment = split(string.sub(comment,1,-28),">")
  46.  
  47. -- local message = splitComment[math.random(#splitComment)]
  48. -- local msg = {
  49. --     {text=message,color="red"}}
  50. -- msg = textutils.serialiseJSON(msg)
  51. -- --print(textutils.serialise(splitComment))
  52. -- --print(string.sub(comment,1,-28))
  53. -- --print(splitComment[#splitComment])
  54. -- print(message)
  55. -- chat.sendFormattedMessage(msg,"r/"..subreddit.." bot")
  56.  
  57.  
  58. local expect = require("cc.expect").expect
  59. return function(subreddit,offset)
  60.     expect(1,subreddit,"string")
  61.     expect(2,offset,"number","nil")
  62.     offset = math.floor((tonumber(offset) or 0)*20)
  63.     local archive = assert(http.get("https://archive.is/offset="..offset.."/http://reddit.com/r/"..subreddit.."/comments/*"),"no subreddit was found")
  64.     local data = archive.readAll()
  65.     archive.close()
  66.     local archiveLinks = getArchiveLinks(data)
  67.     local tComments = {}
  68.     local tSeenLinks = {}
  69.     for k,v in pairs(archiveLinks) do
  70.         if not tSeenLinks[v] then
  71.             print(v:sub(2,-2))
  72.             -- pcall(http.request,v:sub(2,-2),"GET")
  73.             local s,pol,_,pol2 = pcall(http.get,{url=v:sub(2,-2),timeout=5})
  74.             local site = pol or pol2
  75.             if s and pol then
  76.                 local polData = site.readAll()
  77.                 print(#polData,polData:sub(1,term.getSize()))
  78.                 site.close()
  79.                 local comments = match(polData,"<form action=\"#\"(.-)</form>")
  80.                 print(#comments)
  81.                 -- local comments = {}
  82.                 -- local str,bInCommentFlag = "",false
  83.                 -- local line = pol.readLine(true)
  84.                 -- while line do
  85.                 --     if b
  86.                 --     local i = line:find("<form action = \"#\"")
  87.                 --     while i do
  88.                 --     if i then
  89.                 --         str = str .. line:sub(i)                
  90.                 for _,comment in pairs(comments) do
  91.                     --print("comment",comment)
  92.                     --local splitComment = split(string.sub(comment,1,-28),">")
  93.                     local splitComment = match(comment,'<div style="font%-size:14px;[^"]*"%s*>(.-)</div>')
  94.                     for j,w in pairs(splitComment) do
  95.                         w = w:gsub("%b<>",""):gsub("%s*$",""):gsub("&quot",'"'):gsub("&amp","&"):gsub("&lt","<"):gsub("&gt",">")
  96.                         print(w)
  97.                             -- print("comment")
  98.                         table.insert(tComments,w)
  99.                         -- else
  100.                         --     print("not comment")
  101.                         -- end
  102.                     end
  103.                 end
  104.             else
  105.                 print("pol",site,pol,pol2)
  106.             end
  107.         end
  108.         tSeenLinks[v] = true
  109.         os.queueEvent("nosleep")
  110.         os.pullEvent()
  111.     end
  112.     -- local index = 1
  113.     -- while index <= #archiveLinks do
  114.     --     local e,p1,p2,p3 = os.pullEvent()
  115.     --     if e == "http_success" and tSeenLinks[p1] then
  116.     --         local polData = p2.readAll()
  117.     --         p2.close()
  118.     --         local comments = match(polData,"<form action=\"#\".-</form>")
  119.     --         for _,comment in pairs(comments) do
  120.     --             print("comment",comment)
  121.     --             local splitComment = split(string,sub(comment,1,-28),">")
  122.     --             for j,w in pairs(splitComment) do
  123.     --                 print(w)
  124.     --                 if true or #w > 20 then
  125.     --                     table.insert(tComments,w)
  126.     --                 end
  127.     --             end
  128.     --         end
  129.     --         index = index + 1
  130.     --     elseif e == "http_failure" and tSeenLinks[p1] then
  131.     --         print(p2,p3)
  132.     --         index = index + 1
  133.     --     end
  134.     -- end
  135.     return tComments
  136. end
  137.    
  138.  
  139. --print(textutils.serialise(match(polData,"%b\"\"")))
Advertisement
Add Comment
Please, Sign In to add comment