Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DownloadFile
- -- Version 1.0
- -- By JPiolho
- -- Feel free to copy and edit as long you credit the original author
- -- More Info @ http://bit.ly/zoxn6V
- local function AdviseUsage()
- print("Usage: downloadfile <provider> [code] <target>")
- end
- local function ErrorDownloading()
- print("There was an error while downloading! Aborting")
- end
- local tArgs = {...}
- if #tArgs == 0 then
- AdviseUsage()
- return
- end
- local provider = tArgs[1]
- local code, target
- if provider == "ideone" or provider == "pastebin" or provider == "tinypaste" or provider == "codepad" or provider == "pastebay" then
- if #tArgs ~= 3 then
- AdviseUsage()
- return
- end
- code = tArgs[2]
- target = tArgs[3]
- else
- target = tArgs[2]
- end
- if fs.exists(target) then
- -- Dont allow folders
- if fs.isDir(target) then
- print("A folder with that name already exists")
- return
- end
- -- Dont allow readonlys
- if fs.isReadOnly(target) then
- print("You cannot write to the target file")
- return
- end
- print("That file already exists. Overwrite? (y/n)")
- local answer = read()
- if answer ~= "y" then
- return
- end
- end
- local finalData = ""
- if provider == "ideone" then
- local r = http.get("http://ideone.com/plain/" .. code)
- if r == nil then ErrorDownloading() return end
- finalData = r:readAll()
- elseif provider == "pastebin" then
- local r = http.get("http://pastebin.com/raw.php?i=" .. code)
- if r == nil then ErrorDownloading() return end
- finalData = r:readAll()
- elseif provider == "pastebay" then
- local r = http.get("http://www.pastebay.net/pastebay.php?dl=" .. code)
- if r == nil then ErrorDownloading() return end
- finalData = r:readAll()
- elseif provider == "codepad" then
- local r = http.get("http://codepad.org/" .. code .. "/raw.")
- if r == nil then ErrorDownloading() return end
- finalData = r:readAll()
- elseif provider == "tinypaste" then
- local r = http.get("http://tinypaste.com/api/get.xml?id=" .. code)
- if r == nil then ErrorDownloading() return end
- local xml = r:readAll()
- local m = string.gmatch(xml,"<result>.*<paste>(.*)</paste>.*</result>")
- for result in m do
- finalData = finalData .. result
- end
- finalData = string.gsub(finalData," ","")
- finalData = string.gsub(finalData,"&","&")
- finalData = string.gsub(finalData,"<","<")
- finalData = string.gsub(finalData,">",">")
- finalData = string.gsub(finalData,""","\"")
- finalData = string.gsub(finalData,"'","'")
- else
- local r = http.get(provider)
- if r == nil then ErrorDownloading() return end
- finalData = r:readAll()
- end
- if fs.exists(target) then
- fs.delete(target)
- end
- local f = io.open(target,"w")
- f:write(finalData)
- f:close()
- print("File downloaded")
Add Comment
Please, Sign In to add comment