Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- local args = {...}
- -- local subreddit = "politics"
- -- if args and args[1] then subreddit = args[1] end
- -- local archive = http.get("https://archive.is/http://reddit.com/r/"..subreddit.."/comments/*")
- -- local data = archive.readAll()
- -- archive.close()
- local function split(input,sep)
- sep = sep or "%s"
- local t = {}
- for str in string.gmatch(input,"([^"..sep.."]+)") do
- table.insert(t,str)
- end
- return t
- end
- local function match(input,pattern)
- local t = {}
- for str in string.gmatch(input,pattern) do
- table.insert(t,str)
- end
- return t
- end
- local function getArchiveLinks(data)
- return match(data, "\"https://archive.is/%w%w%w%w%w\"")
- end
- -- local archiveLinks = getArchiveLinks(data)
- -- local link = archiveLinks[math.random(#archiveLinks)]
- -- --print(textutils.serialise(getArchiveLinks(data)))
- -- local pol = http.get(string.sub(link,2,-2))
- -- --local pol = http.get("https://archive.is/DFDQK")
- -- local polData = pol.readAll()
- -- pol.close()
- -- --local splitPolData = match(polData,"................................................................................................This is the definition.....................................")
- -- --local splitPolData = match(polData,"<form action=\"#\".-</form>")
- -- --local index = string.find(polData, "This is the definition")
- -- --local index = 319236
- -- --print(index)
- -- --local splitPolData = {string.sub(polData,index-2000,index-1400),
- -- --string.sub(polData,index-30,index+40)}
- -- local comments = match(polData,"<form action=\"#\".-</form>")
- -- local comment = comments[math.random(#comments)]
- -- splitComment = split(string.sub(comment,1,-28),">")
- -- local message = splitComment[math.random(#splitComment)]
- -- local msg = {
- -- {text=message,color="red"}}
- -- msg = textutils.serialiseJSON(msg)
- -- --print(textutils.serialise(splitComment))
- -- --print(string.sub(comment,1,-28))
- -- --print(splitComment[#splitComment])
- -- print(message)
- -- chat.sendFormattedMessage(msg,"r/"..subreddit.." bot")
- local expect = require("cc.expect").expect
- return function(subreddit,offset)
- expect(1,subreddit,"string")
- expect(2,offset,"number","nil")
- offset = math.floor((tonumber(offset) or 0)*20)
- local archive = assert(http.get("https://archive.is/offset="..offset.."/http://reddit.com/r/"..subreddit.."/comments/*"),"no subreddit was found")
- local data = archive.readAll()
- archive.close()
- local archiveLinks = getArchiveLinks(data)
- local tComments = {}
- local tSeenLinks = {}
- for k,v in pairs(archiveLinks) do
- if not tSeenLinks[v] then
- print(v:sub(2,-2))
- -- pcall(http.request,v:sub(2,-2),"GET")
- local s,pol,_,pol2 = pcall(http.get,{url=v:sub(2,-2),timeout=5})
- local site = pol or pol2
- if s and pol then
- local polData = site.readAll()
- print(#polData,polData:sub(1,term.getSize()))
- site.close()
- local comments = match(polData,"<form action=\"#\"(.-)</form>")
- print(#comments)
- -- local comments = {}
- -- local str,bInCommentFlag = "",false
- -- local line = pol.readLine(true)
- -- while line do
- -- if b
- -- local i = line:find("<form action = \"#\"")
- -- while i do
- -- if i then
- -- str = str .. line:sub(i)
- for _,comment in pairs(comments) do
- --print("comment",comment)
- --local splitComment = split(string.sub(comment,1,-28),">")
- local splitComment = match(comment,'<div style="font%-size:14px;[^"]*"%s*>(.-)</div>')
- for j,w in pairs(splitComment) do
- w = w:gsub("%b<>",""):gsub("%s*$",""):gsub(""",'"'):gsub("&","&"):gsub("<","<"):gsub(">",">")
- print(w)
- -- print("comment")
- table.insert(tComments,w)
- -- else
- -- print("not comment")
- -- end
- end
- end
- else
- print("pol",site,pol,pol2)
- end
- end
- tSeenLinks[v] = true
- os.queueEvent("nosleep")
- os.pullEvent()
- end
- -- local index = 1
- -- while index <= #archiveLinks do
- -- local e,p1,p2,p3 = os.pullEvent()
- -- if e == "http_success" and tSeenLinks[p1] then
- -- local polData = p2.readAll()
- -- p2.close()
- -- local comments = match(polData,"<form action=\"#\".-</form>")
- -- for _,comment in pairs(comments) do
- -- print("comment",comment)
- -- local splitComment = split(string,sub(comment,1,-28),">")
- -- for j,w in pairs(splitComment) do
- -- print(w)
- -- if true or #w > 20 then
- -- table.insert(tComments,w)
- -- end
- -- end
- -- end
- -- index = index + 1
- -- elseif e == "http_failure" and tSeenLinks[p1] then
- -- print(p2,p3)
- -- index = index + 1
- -- end
- -- end
- return tComments
- end
- --print(textutils.serialise(match(polData,"%b\"\"")))
Advertisement
Add Comment
Please, Sign In to add comment