Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local dnt = false
- local data = {}
- local databaseLoc = "/.snipt-database"
- local function loadDatabase()
- if not(fs.exists(databaseLoc)) or fs.isDir(databaseLoc) then
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- local f = io.open(databaseLoc,"r")
- local database = textutils.unserialize(f:read("*a"))
- f:close()
- if database then
- if type(database[1]) == "boolean" then
- dnt = database[1]
- data = database[2]
- else
- print("Database corrupted, resetting")
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- else
- print("Database corrupted, resetting")
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- end
- local function betterGet(url)
- http.request(url)
- while true do
- local e, rUrl, rmsg = os.pullEvent()
- if (e == "http_success") and (rUrl == url) then
- if rmsg then
- local data = rmsg.readAll()
- rmsg.close()
- if data then
- return "success", data
- else
- sleep(1)
- http.request(url)
- end
- else
- sleep(1)
- http.request(url)
- end
- elseif (e == "http_failure") and (rUrl == url) then
- return "failure"
- end
- end
- end
- local function track(file,url)
- if not dnt then
- data[shell.resolve(file)] = url
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,data}))
- f:close()
- end
- end
- local function retrieveSnipt(snipt,file)
- loadDatabase()
- if (#snipt < 10) then
- local num = snipt:match("%d+")
- if num then
- local resp,data = betterGet("https://snipt.net/api/public/snipt/"..num.."/?format=xml")
- if resp == "success" then
- local url = data:match("<raw_url>([^<]+)</raw_url>")
- if not url then
- print("Failed to retrieve snipt! [6]")
- return false
- else
- resp,data = betterGet(url)
- if resp == "success" then
- if fs.exists(file) then
- write("File exists, overwrite [Y/n]? ")
- local input = read()
- if input:lower():find("n") then
- print("Cancelled!")
- return false
- else
- local f = io.open(file,"w")
- f:write(data)
- f:close()
- track(file,url)
- print("Snipt saved!")
- return true
- end
- else
- local f = io.open(file,"w")
- f:write(data)
- f:close()
- track(file,url)
- print("Snipt saved!")
- return true
- end
- else
- print("Failed to retrieve snipt! [2]")
- end
- end
- else
- print("Failed to retrieve snipt! [1]")
- return false
- end
- else
- print("Invalid Snipt ID!")
- end
- elseif (#snipt >= 32) and (#snipt < 100) then
- for word in string.gmatch(snipt,"/?([^/]+)/?") do
- if #word == 32 then
- local resp,data = betterGet("https://snipt.net/raw/"..word.."/")
- if resp == "success" then
- if fs.exists(file) then
- write("File exists, overwrite [Y/n]? ")
- local input = read()
- if input:lower():find("n") then
- print("Cancelled!")
- return false
- else
- local f = io.open(file,"w")
- f:write(data)
- f:close()
- track(file,"https://snipt.net/raw/"..word.."/")
- print("Snipt saved!")
- return true
- end
- else
- local f = io.open(file,"w")
- f:write(data)
- f:close()
- track(file,"https://snipt.net/raw/"..word.."/")
- print("Snipt saved!")
- return true
- end
- else
- print("Failed to retrive snipt! [3]")
- return false
- end
- end
- end
- print("Invalid Snipt ID!")
- return false
- else
- print("Invalid Snipt ID!")
- return false
- end
- end
- local function printUsage()
- print("Usage:")
- print("snipt <id> <filename>")
- print("snipt wget <url> <filename>")
- print("snipt <filename>")
- print("snipt dnt (Toggles tracking)")
- end
- if tArgs[1] == "dnt" then
- loadDatabase()
- if dnt then
- local f = io.open(databaseLoc,"w")
- dnt = false
- f:write(textutils.serialize({dnt,data}))
- f:close()
- print("Tracking turned on")
- else
- local f = io.open(databaseLoc,"w")
- dnt = true
- f:write(textutils.serialize({dnt,data}))
- f:close()
- print("Tracking turned off")
- end
- elseif (tArgs[1] == "wget") then
- if tArgs[2] and tArgs[3] then
- loadDatabase()
- local resp,data = betterGet(tArgs[2])
- if resp == "success" then
- if fs.exists(tArgs[3]) then
- write("File exists, overwrite [Y/n]? ")
- local input = read()
- if input:lower():find("n") then
- print("Cancelled!")
- return false
- else
- local f = io.open(tArgs[3],"w")
- f:write(data)
- f:close()
- track(tArgs[3],tArgs[2])
- print("Snipt saved!")
- return true
- end
- else
- local f = io.open(tArgs[3],"w")
- f:write(data)
- f:close()
- track(tArgs[3],tArgs[2])
- print("Snipt saved!")
- return true
- end
- else
- print("Failed to retrive snipt! [5]")
- return false
- end
- else
- printUsage()
- end
- elseif tArgs[1] and tArgs[2] then
- retrieveSnipt(tArgs[1],tArgs[2])
- elseif tArgs[1] then
- loadDatabase()
- if data[shell.resolve(tArgs[1])] then
- print("Retrieving:\n"..data[shell.resolve(tArgs[1])])
- resp,data = betterGet(data[shell.resolve(tArgs[1])])
- if resp == "success" then
- local f = io.open(tArgs[1],"w")
- f:write(data)
- f:close()
- print("Snipt saved!")
- return true
- else
- print("Failed to retrieve snipt! [4]")
- end
- else
- print("Could not find snipt URL in database!")
- printUsage()
- end
- else
- printUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement