Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Pastebin(user, password)
- local Pastebin = {}
- Pastebin.api_user_key = 0
- Pastebin.API_version = 0.8
- -- Internal function that parses the pastebin XML response.
- function Pastebin:parse(response)
- local objs = {}
- for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
- local obj = {}
- for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
- obj[key] = value
- end
- objs[#objs+1] = obj
- end
- return objs
- end
- -- Used to be a custom function, but I realized CC-Lua had one already.
- Pastebin.url_encode = textutils.urlEncode
- -- My Pastebin API key.
- Pastebin.api_dev_key = "c09548865c8dd0c2bf57d0d060b5e96a"
- -- Allowable inputs for paste creation options.
- Pastebin.paste_formats = {["4cs"]=true,["6502acme"]=true,["6502kickass"]=true,["6502tasm"]=true,abap=true,actionscript=true,actionscript3=true,ada=true,algol68=true,apache=true,applescript=true,apt_sources=true,arm=true,asm=true,asp=true,asymptote=true,autoconf=true,autohotkey=true,autoit=true,avisynth=true,awk=true,bascomavr=true,bash=true,basic4gl=true,bibtex=true,blitzbasic=true,bnf=true,boo=true,bf=true,c=true,c_mac=true,cil=true,csharp=true,cpp=true,["cpp-qt"]=true,c_loadrunner=true,caddcl=true,cadlisp=true,cfdg=true,chaiscript=true,clojure=true,klonec=true,klonecpp=true,cmake=true,cobol=true,coffeescript=true,cfm=true,css=true,cuesheet=true,d=true,dcl=true,dcpu16=true,dcs=true,delphi=true,oxygene=true,diff=true,div=true,dos=true,dot=true,e=true,ecmascript=true,eiffel=true,email=true,epc=true,erlang=true,fsharp=true,falcon=true,fo=true,f1=true,fortran=true,freebasic=true,freeswitch=true,gambas=true,gml=true,gdb=true,genero=true,genie=true,gettext=true,go=true,groovy=true,gwbasic=true,haskell=true,haxe=true,hicest=true,hq9plus=true,html4strict=true,html5=true,icon=true,idl=true,ini=true,inno=true,intercal=true,io=true,j=true,java=true,java5=true,javascript=true,jquery=true,kixtart=true,latex=true,ldif=true,lb=true,lsl2=true,lisp=true,llvm=true,locobasic=true,logtalk=true,lolcode=true,lotusformulas=true,lotusscript=true,lscript=true,lua=true,m68k=true,magiksf=true,make=true,mapbasic=true,matlab=true,mirc=true,mmix=true,modula2=true,modula3=true,["68000devpac"]=true,mpasm=true,mxml=true,mysql=true,nagios=true,newlisp=true,text=true,nsis=true,oberon2=true,objeck=true,objc=true,["ocaml-brief"]=true,ocaml=true,octave=true,pf=true,glsl=true,oobas=true,oracle11=true,oracle8=true,oz=true,parasail=true,parigp=true,pascal=true,pawn=true,pcre=true,per=true,perl=true,perl6=true,php=true,["php-brief"]=true,pic16=true,pike=true,pixelbender=true,plsql=true,postgresql=true,povray=true,powershell=true,powerbuilder=true,proftpd=true,progress=true,prolog=true,properties=true,providex=true,purebasic=true,pycon=true,python=true,pys60=true,q=true,qbasic=true,rsplus=true,rails=true,rebol=true,reg=true,rexx=true,robots=true,rpmspec=true,ruby=true,gnuplot=true,sas=true,scala=true,scheme=true,scilab=true,sdlbasic=true,smalltalk=true,smarty=true,spark=true,sparql=true,sql=true,stonescript=true,systemverilog=true,tsql=true,tcl=true,teraterm=true,thinbasic=true,typoscript=true,unicon=true,uscript=true,ups=true,urbi=true,vala=true,vbnet=true,vedit=true,verilog=true,vhdl=true,vim=true,visualprolog=true,vb=true,visualfoxpro=true,whitespace=true,whois=true,winbatch=true,xbasic=true,xml=true,xorg_conf=true,xpp=true,yaml=true,z80=true,zxbasic=true}
- Pastebin.paste_private_options = {[0]=true,[1]=true,[2]="Private"}
- Pastebin.paste_expire_date_options = {["N"]=true,["10M"]=true,["1H"]=true,["1D"]=true,["1W"]=true,["2W"]=true,["1M"]=true}
- -- Creates a new paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
- -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
- --
- -- Returns paste URL or error code.
- function Pastebin:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
- if not api_paste_expire_date then
- api_paste_expire_date = "N"
- end
- assert(type(api_paste_code) == "string", "Only strings can be uploaded as pastes! Use `tostring()` if necessary!")
- local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_option=paste", "&api_paste_code=", self.url_encode(api_paste_code)}
- if self.api_user_key then if type(self.api_user_key) == "string" then local len = #urlstr urlstr[len+1] = "&api_user_key=" urlstr[len+2] = self.url_encode(self.api_user_key) else io.stderr:write("Invalid user key!\n") end end
- if api_paste_name then if type(api_paste_name) == "string" then local len = #urlstr urlstr[len+1] = "&api_paste_name=" urlstr[len+2] = self.url_encode(api_paste_name) else io.stderr:write("Only strings can be used to name pastes! Use `tostring()` if necessary!") end end
- if api_paste_format then if self.paste_formats[api_paste_format] then local len = #urlstr urlstr[len+1] = "&api_paste_format=" urlstr[len+2] = self.url_encode(api_paste_format) else io.stderr:write("Invalid paste format!") end end
- if api_paste_private then local paste_private_long = self.paste_private_options[api_paste_private] if paste_private_long then if paste_private_long == "Private" and not self.api_user_key then io.stderr:write("User key necessary for private pastes!") else local len = #urlstr urlstr[len+1] = "&api_paste_private=" urlstr[len+2] = self.url_encode(api_paste_private) end else io.stderr:write("Invalid privacy level") end end
- if api_paste_expire_date then if self.paste_expire_date_options[api_paste_expire_date] then local len = #urlstr urlstr[len+1] = "&api_paste_expire_date=" urlstr[len+2] = api_paste_expire_date else io.stderr:write("Invalid paste expiration date!") end end
- return http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
- end
- -- Authenticates with Pastebin.
- -- Example: local agente382 = pastebin.login("AgentE382", "thisisnotmypassword")
- --
- -- Returns user key for use with other API functions.
- function Pastebin:loginToPastebin(api_user_name, api_user_password)
- assert(type(api_user_name) == "string" and type(api_user_password) == "string", "Both arguments are required and must be strings!")
- return http.post("http://pastebin.com/api/api_login.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_name="..self.url_encode(api_user_name).."&api_user_password="..self.url_encode(api_user_password)).readAll()
- end
- -- Deletes a paste. Uses user key from login(). api_paste_key must be a string containing the end of the paste's URL.
- -- Example: local response = pastebin.delete("Rxe673BJ")
- --
- -- Returns "Paste Removed" or error message.
- function Pastebin:delete(api_paste_key)
- assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
- return http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=delete").readAll()
- end
- -- Fetches a paste from Pastebin.
- -- Example: local rc4 = pastebin.get("Rxe673BJ")
- --
- -- Returns requested paste as a string or an error message.
- function Pastebin:get(api_paste_key)
- assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
- local response = http.get("http://pastebin.com/raw.php?i="..self.url_encode(api_paste_key))
- return response and response.readAll()
- end
- -- Fetches a private paste from Pastebin. Paste must belong to user logged in with api_user_key
- -- Example: local test = pastebin.getprivate("fhLGZm3i")
- --
- -- Returns requested paste as a string or an error message.
- function Pastebin:getprivate(api_paste_key)
- assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
- return http.post("http://pastebin.com/api/api_raw.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=show_paste").readAll()
- end
- -- Lists data about all of a user's pastes. Uses user key from login(). Note that this makes it impossible to get data about random people's pastes.
- -- Example: local allpastes = pastebin.list(1000)
- --
- -- Returns a list of data about all pastes associated with a user's account, using the table format described at the end of the document.
- function Pastebin:list(api_results_limit)
- assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
- local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_user_key=", self.url_encode(self.api_user_key), "&api_option=list"}
- if api_results_limit then if type(api_results_limit) == "number" then if api_results_limit > 1000 then api_results_limit = 1000 elseif api_results_limit < 1 then api_results_limit = 1 end local len = #urlstr urlstr[len+1] = "&api_results_limit=" urlstr[len+2] = self.url_encode(api_results_limit) else io.stderr:write("Results limit must be a number!\n") end end
- local response = http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
- if string.find(response,"<") then
- return self:parse(response)
- else
- return response
- end
- end
- -- Lists data about the 18 currently trending pastes.
- -- Example: local trendingpastes = pastebin.trending()
- --
- -- Returns a list of data about the 18 currently trending pastes, using the table format described at the end of this document.
- function Pastebin:trending()
- local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_option=trends").readAll()
- if string.find(response,"<") then
- return self:parse(response)
- else
- return response
- end
- end
- -- Lists all of a user's settings. Uses user key from login().
- -- Example: local settings = pastebin.settings()
- --
- -- Returns a list of the user's settings, using the table format described at the end of this document.
- function Pastebin:settings()
- assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
- local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_option=userdetails").readAll()
- if string.find(response,"<") then
- return unpack(self:parse(response))
- else
- return response
- end
- end
- -- Edit a paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
- -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
- --
- -- Returns paste URL or error code.
- function Pastebin:edit(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
- local list = self:list(1000)
- for a, b in pairs(list) do
- if b and b["title"] == api_paste_name then
- self:delete(b["key"])
- local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
- return message
- end
- end
- local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
- return message
- end
- local function passwordInput(isFirstTime)
- if isFirstTime == true then
- term.clear()
- print("+-----------------------------------------------+")
- print("| You are using this software the first time on |")
- print("| this computer. This software needs your |")
- print("| Pastebin username and password |")
- print("+-----------------------------------------------+")
- write("username:")
- local user = io.read()
- term.clear()
- print("+-----------------------------------------------+")
- print("| You are using this software the first time on |")
- print("| this computer. This software needs your |")
- print("| Pastebin username and password |")
- print("+-----------------------------------------------+")
- write("password:")
- local password = io.read()
- term.clear()
- return user, password
- else
- term.clear()
- print("+-------------------------------+")
- print("| Username or password is wrong |")
- print("| Try again |")
- print("+-------------------------------+")
- write("username:")
- local user = io.read()
- term.clear()
- print("+-------------------------------+")
- print("| Username or password is wrong |")
- print("| Try again |")
- print("+-------------------------------+")
- write("password:")
- local password = io.read()
- term.clear()
- return user, password
- end
- end
- local function readUserDataFromFile()
- if fs.exists("MCS_data.txt") then
- local file = fs.open("MCS_data.txt", "r")
- local user = file.readLine()
- local password = file.readLine()
- file.close()
- return user, password
- end
- return false
- end
- local function writeUserDataToFile(user, password)
- local file = fs.open("MCS_data.txt", "w")
- file.writeLine(user)
- file.writeLine(password)
- file.close()
- end
- function Pastebin:login(user, password)
- local isFirstTime = true
- if not user or not password then
- user, password = readUserDataFromFile()
- end
- while true do
- if user and password then
- self.api_user_key = self:loginToPastebin(user, password)
- if self.api_user_key ~= "Bad API request, invalid login" then
- writeUserDataToFile(user, password)
- term.clear()
- return
- end
- end
- user, password = passwordInput(isFirstTime)
- isFirstTime = false
- end
- end
- Pastebin:login(user, password)
- return Pastebin
- end
- function Settings()
- local settings = {}
- settings.settings = {}
- settings.settings.isActive = false
- settings.settings.updateTime = 30
- settings.settings.minimumNumber = 8000
- settings.settings.maximumNumber = 10000
- settings.settings.enableMinMaxNumber = false
- function settings:loadSettings()
- if fs.exists("MCS_settings.txt") then
- local file = fs.open("MCS_settings.txt", "r")
- local settings = file.readAll()
- file.close()
- if settings ~= nil then
- local settingsTable = textutils.unserialize(settings)
- self:set(settingsTable)
- return true
- end
- end
- return false
- end
- function settings:writeSettings()
- local file = fs.open("MCS_settings.txt", "w")
- file.write(textutils.serialize(self.settings))
- file.close()
- end
- function settings:init()
- if self:loadSettings() == false then
- self.settings.isActive = true
- self:writeSettings()
- end
- end
- function settings:set(settingsTable)
- if settingsTable then
- if settingsTable.isActive then
- self.settings.isActive = settingsTable.isActive
- end
- if settingsTable.updateTime then
- self.settings.updateTime = settingsTable.updateTime
- end
- if settingsTable.maximumNumber then
- self.settings.maximumNumber = settingsTable.maximumNumber
- end
- if settingsTable.minimumNumber then
- self.settings.minimumNumber = settingsTable.minimumNumber
- end
- if settingsTable.enableMinMaxNumber ~= nil then
- self.settings.enableMinMaxNumber = settingsTable.enableMinMaxNumber
- end
- self:writeSettings()
- end
- end
- function settings:setActive(isActive)
- self.settings.isActive = isActive
- self:writeSettings()
- end
- function settings:getIsActive()
- return self.settings.isActive
- end
- function settings:getSettings()
- return self.settings
- end
- function settings:getUpdateTime()
- return self.settings.updateTime
- end
- function settings:getMaximumNumber()
- return self.settings.maximumNumber
- end
- function settings:getMinimumNumber()
- return self.settings.minimumNumber
- end
- function settings:getEnableMinMaxNumber()
- return self.settings.enableMinMaxNumber
- end
- function settings:print()
- term.clear()
- print("isActive: ".. tostring(self:getIsActive()))
- print("enableMinMaxNumber: ".. tostring(self:getEnableMinMaxNumber()))
- print("updateTime: ".. tostring(self:getUpdateTime()))
- if self:getEnableMinMaxNumber() then
- print("minimumNumber: ".. tostring(self:getMinimumNumber()))
- print("maximumNumber: ".. tostring(self:getMaximumNumber()))
- end
- end
- settings:init()
- return settings
- end
- settings = Settings()
- function updateToPastebin()
- local pastebin = Pastebin()
- local pasteNameOut = "MCS:Out:CobblestoneFarm:"..os.getComputerID()
- local pasteNameIn = "MCS:In:CobblestoneFarm:"..os.getComputerID()
- local pasteNameMeSystem = "MCS:Out:ME-System:"
- local statusOld = not settings:getIsActive()
- local status = {}
- local cobblestoneItemCount = 0
- local settingsData = {}
- while true do
- settings:print()
- status.isActive = settings:getIsActive()
- if status.isActive ~= statusOld then
- statusOld = status.isActive
- settingsData = settings:getSettings()
- if settingsData then
- local information = textutils.serialize(settingsData)
- if information then
- local message = pastebin:edit(information, pasteNameOut, nil, nil, "N")
- print(message)
- end
- end
- end
- sleep(settings:getUpdateTime())
- local list = pastebin:list(1000)
- for a, b in pairs(list) do
- if b["title"] == pasteNameIn then
- local statusString = pastebin:get(b["key"])
- pastebin:delete(b["key"])
- if statusString then
- status = textutils.unserialize(statusString)
- if status then
- settings:set(status)
- end
- end
- end
- if settings:getEnableMinMaxNumber() then
- local result = string.sub(b["title"], 1, string.len(pasteNameMeSystem))
- if result == pasteNameMeSystem then
- local items = pastebin:get(b["key"])
- if items then
- local itemList = textutils.unserialize(items)
- for a, b in pairs(itemList) do
- if b["itemId"] == "minecraft:cobblestone" then
- cobblestoneItemCount = b["count"]
- if cobblestoneItemCount > settings:getMaximumNumber() then
- settings:setActive(false)
- end
- if cobblestoneItemCount < settings:getMinimumNumber() then
- settings:setActive(true)
- end
- end
- end
- end
- end
- end
- end
- end
- end
- function work()
- turtle.select(1)
- local slot = 1
- while true do
- local isActive = settings:getIsActive()
- if isActive == true then
- turtle.dig()
- turtle.digDown()
- local freeSpaceInSlot = turtle.getItemSpace(slot)
- if turtle.getItemSpace(slot) == 0 then
- slot = slot + 1
- turtle.select(slot)
- if slot > 4 then
- print("empty everything")
- for emptySlot = 16, 1, -1 do
- turtle.select(emptySlot)
- turtle.dropUp()
- end
- slot = 1
- turtle.select(slot)
- end
- end
- else
- sleep(1)
- end
- end
- end
- parallel.waitForAny(updateToPastebin, work)
Add Comment
Please, Sign In to add comment