Advertisement
Tag365

Download APIs

May 6th, 2015 (edited)
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1. --------------------------
  2. -- Download APIs script --
  3. --------------------------
  4. if not http then
  5. print("This is a script that allows the download of APIs.")
  6. return
  7. end
  8. local tAPIs = {}
  9. local topString = "-- This portion of the startup script was automatically added by the Download APIs script. --"
  10. local startupString = [[
  11. -- Load APIs --
  12. local apis = "apis"
  13. if fs.isDir(apis) then
  14. local folder = fs.list(apis)
  15. if #folder > 0 then
  16. print("Loading APIs...")
  17. for k, v in ipairs(folder) do
  18. os.loadAPI(apis.."/"..v)
  19. end
  20. end
  21. end
  22.  
  23. -- Set program aliases --
  24. local programs = "programs"
  25. if fs.isDir(programs) then
  26. local folder = fs.list(programs)
  27. if #folder > 0 then
  28. print("Aliasing programs...")
  29. for k, v in ipairs(folder) do
  30. shell.setAlias(apis.."/"..v)
  31. end
  32. end
  33. end
  34.  
  35. -- And now back to your startup script. --
  36. ]]
  37.  
  38. -- Set Up Variables --
  39. local scrX, scrY = term.getSize()
  40.  
  41. local menuTitleColor = colors.lightBlue
  42. local menuBGColor = colors.gray
  43. local menuBGSelectionColor = colors.blue
  44. local menuScrollColor = colors.white
  45. local menuBGScrollColor = colors.lightGray
  46.  
  47. function drawMenuFront(Title, ControlBarText)
  48. term.setCursorPos(1, 1)
  49. term.setBackgroundColor(menuTitleColor)
  50. term.clearLine()
  51. term.write(Title)
  52. ControlBarText = ControlBarText or "Select an option then press Enter."
  53. term.setCursorPos(1, scrY + 1 - math.ceil(#ControlBarText/scrX))
  54. term.setBackgroundColor(menuTitleColor)
  55. term.clearLine()
  56. term.write(ControlBarText)
  57. end
  58.  
  59. -- Draws a menu prompt.
  60. function drawMenu(Title, Options, Scroll, Selected)
  61. local y = 1
  62. for k=math.max(Scroll + 1, 1), math.min(Scroll + 1 + scrY - 2, #Options) do
  63. y = y + 1
  64. term.setCursorPos(1, y)
  65. if Selected == k then
  66. term.setBackgroundColor(menuBGSelectionColor)
  67. else
  68. term.setBackgroundColor(menuBGColor)
  69. end
  70. term.clearLine()
  71. if type(Options[k]) == "table" then
  72. term.write(Options[k]["Name"] or Options[k][1])
  73. else
  74. term.write(Options[k])
  75. end
  76. end
  77. if #Options > scrY - 2 then -- Draw a scroll bar
  78. term.setBackgroundColor(menuBGScrollColor)
  79. for k=2, scrY - 1 do
  80. term.setCursorPos(scrX, k)
  81. term.write(" ")
  82. end
  83. term.setBackgroundColor(menuScrollColor)
  84. local barHeight = (scrY - 2)/(#Options - (scrY - 2))
  85. local barPos = math.max((Scroll/(#Options - (scrY - 2)))*(scrY - 1 - barHeight), 1) + 1
  86. for k=barPos, barPos + barHeight do
  87. term.setCursorPos(scrX, k)
  88. term.write(" ")
  89. end
  90. end
  91. drawMenuFront(Title, ControlBar)
  92. end
  93.  
  94. -- Shows a menu prompt.
  95. function menuPrompt(Title, Options)
  96. term.setBackgroundColor(menuBGColor)
  97. term.clear()
  98. if not SuppressCancel then
  99. if tostring(Options[#Options]) ~= "Cancel" then
  100. Options[#Options + 1] = "Cancel"
  101. end
  102. end
  103. local Scroll, Selected = 0, 1
  104. while true do
  105. if #Options > scrY - 2 then
  106. Scroll = math.min(math.max(Selected - math.ceil((scrY - 2)*.5), 0), #Options - (scrY - 2))
  107. else
  108. Scroll = 0
  109. end
  110. drawMenu(Title, Options, Scroll, Selected)
  111. local event, key, clx, cly = os.pullEvent()
  112. if event == "key" then
  113. if key == keys.up then
  114. Selected = math.max(Selected - 1, 1)
  115. elseif key == keys.down then
  116. Selected = math.min(Selected + 1, #Options)
  117. elseif key == keys.pageUp then
  118. Selected = math.max(Selected - (height - 1), 1)
  119. elseif key == keys.pageDown then
  120. Selected = math.min(Selected + (height - 1), #Options)
  121. elseif key == keys.home then
  122. Selected = 1
  123. elseif key == keys["end"] then
  124. Selected = #Options
  125. elseif key == keys.f5 then
  126. -- Refresh
  127. elseif(key == keys.tab) then
  128. term.setCursorBlink(false)
  129. return false
  130. elseif key == (keys.enter or 28) then
  131. if Selected > #Options - 1 and not SuppressCancel then
  132. return false
  133. else
  134. return Selected
  135. end
  136. end
  137. elseif event == "mouse_click" then
  138. if cly > 1 and cly < scrY then
  139. local Clicked = Scroll + cly - 1
  140. if Clicked <= #Options then
  141. if Clicked == Selected then
  142. if Selected > #Options - 1 and not SuppressCancel then
  143. return false
  144. else
  145. return Selected
  146. end
  147. else
  148. Selected = Clicked
  149. end
  150. end
  151. end
  152. end
  153. end
  154. end
  155.  
  156. function testIfMainElement(str)
  157. if string.lower(str) == "description" then
  158. return true
  159. elseif string.lower(str) == "body" then
  160. return true
  161. elseif string.lower(str) == "message" then
  162. return true
  163. elseif string.lower(str) == "details" then
  164. return true
  165. elseif string.lower(str) == "code" then
  166. return true
  167. end
  168. return false
  169. end
  170.  
  171. function testIfVisibleElement(str)
  172. if string.lower(str) == "name" then
  173. return false
  174. elseif string.lower(str) == "title" then
  175. return false
  176. elseif string.lower(str) == "id" then
  177. return false
  178. elseif string.lower(str) == "turtle api" then
  179. return false
  180. elseif string.lower(str) == "command api" then
  181. return false
  182. end
  183. return true
  184. end
  185.  
  186. function drawDescriptionMenu(Title, ControlBarText, TableElements, Layout, MainElement)
  187. term.setBackgroundColor(menuBGColor)
  188. term.clear()
  189. local currentRow = 0
  190. for k, v in ipairs(Layout) do
  191. if not v["MainElement"] and v["Visible"] then -- If it is not the MainElement and it is visible
  192. currentRow = currentRow + 1
  193. term.setCursorPos(1, currentRow + 1)
  194. term.write((v["Name"] or v[1])..": ")
  195. term.setCursorPos(#(v[1]..": "), currentRow + 1)
  196. term.write(tostring(TableElements[v[1]]))
  197. end
  198. end
  199. currentRow = currentRow + 1
  200. term.setCursorPos(1, currentRow + 1)
  201. term.write(Layout[MainElement]["Name"] or Layout[MainElement][1])
  202. term.setCursorPos(1, currentRow + 2)
  203. print(tostring(TableElements[Layout[MainElement][1]]))
  204. drawMenuFront(Title, "Press Enter to confirm or Tab to return.")
  205. end
  206.  
  207. -- Draws a frame of a description menu.
  208. function descriptionMenuWindow(Title, ControlBarText, TableElements, Layout)
  209. local MainElement = 0
  210. if type(Layout) ~= "table" then
  211. Layout = {}
  212. for k, v in pairs(TableElements) do
  213. Layout[#Layout + 1] = {k}
  214. if testIfMainElement(k) then
  215. MainElement = #Layout
  216. Layout[#Layout]["MainElement"] = true
  217. else
  218. Layout[#Layout]["MainElement"] = false
  219. end
  220. Layout[#Layout]["Visible"] = testIfVisibleElement(Layout[#Layout][1])
  221. end
  222. if not MainElement then
  223. MainElement = #Layout
  224. Layout[#Layout]["MainElement"] = true
  225. end
  226. else
  227. for k, v in pairs(Layout) do
  228. if v[2] then
  229. MainElement = #Layout
  230. break
  231. end
  232. end
  233. end
  234. drawDescriptionMenu(Title, ControlBarText, TableElements, Layout, MainElement)
  235. end
  236.  
  237. function showAPIMenu(selectedAPI)
  238. descriptionMenuWindow(tAPIs[selectedAPI]["Name"], selectedAPI, tAPIs[selectedAPI], Layout)
  239. while true do
  240. local event, key = os.pullEvent("key")
  241. if(key == keys.tab) then
  242. term.setCursorBlink(false)
  243. return false
  244. elseif key == (keys.enter or 28) then
  245. return true
  246. end
  247. end
  248. end
  249.  
  250. function overwriteFileRaw(contents, destination)
  251. local file, err = fs.open(destination, "w")
  252. if file then
  253. file.write(contents)
  254. file.close()
  255. return true
  256. end
  257. return false, err
  258. end
  259.  
  260. function overwriteFile(contents, destination)
  261. local pcallSuccess, returnCondition, errorString = pcall(overwriteFileRaw, contents, destination)
  262. if pcallSuccess then
  263. return returnCondition, errorString
  264. end
  265. return false, returnCondition
  266. end
  267.  
  268. function pullFileRaw(url, header, post)
  269. if http then
  270. local handle, str
  271. if not post then
  272. handle, str = http.get(url, header), ""
  273. else
  274. handle, str = http.post(url, post, header), ""
  275. end
  276. if handle then
  277. str = handle.readAll()
  278. handle.close()
  279. return str
  280. end
  281. return false, str
  282. end
  283. return false, "Http API is not enabled"
  284. end
  285.  
  286. function pullFile(url, header, post)
  287. local pcallSuccess, returnCondition, errorString = pcall(pullFileRaw, url, header, post)
  288. if pcallSuccess then
  289. return returnCondition, errorString
  290. end
  291. return false, returnCondition
  292. end
  293. for k=1, 4 do
  294. tAPIs = textutils.unserialize(pullFile("https://pastebin.com/raw/Eare8WYE"))
  295. if tAPIs then
  296. break
  297. end
  298. end
  299. if not tAPIs then
  300. error("There was a problem while loading the API list.")
  301. end
  302.  
  303. function downloadFileRaw(url, destination, header, post)
  304. if http then
  305. local handle, str
  306. if not post then
  307. handle, str = http.get(url, header), ""
  308. else
  309. handle, str = http.post(url, post, header), ""
  310. end
  311. if handle then
  312. str = handle.readAll()
  313. handle.close()
  314. return overwriteFile(str, destination)
  315. end
  316. return str
  317. end
  318. return false, "Http API is not enabled"
  319. end
  320.  
  321. function downloadFile(url, destination, header, post)
  322. local pcallSuccess, returnCondition, errorString = pcall(downloadFileRaw, url, destination, header, post)
  323. if pcallSuccess then
  324. return returnCondition, errorString
  325. end
  326. return false, returnCondition
  327. end
  328.  
  329. function setUpStartup()
  330. local file, str = fs.open("startup", "r"), ""
  331. if file then
  332. str = file.readAll()
  333. file.close()
  334. end
  335. if not string.find(str, topString) then
  336. file = fs.open("startup", "w")
  337. if file then
  338. file.writeLine(topString.."\n")
  339. file.writeLine(startupString.."\n")
  340. file.writeLine(str)
  341. file.close()
  342. end
  343. print("Your startup script has been automatically changed.")
  344. end
  345. end
  346.  
  347. while true do
  348. local selectedAPI = menuPrompt("API list", tAPIs)
  349. if not selectedAPI then term.setBackgroundColor(colors.black) term.clear() return term.setCursorPos(1, 1) end
  350. if showAPIMenu(selectedAPI) then
  351. term.clear()
  352. term.setCursorPos(1, 1)
  353. --print("Save to where? To automatically load this API on startup leave this space empty.")
  354. local name = ""
  355. term.write("Downloading File...")
  356. local response, err
  357. if #name > 0 then
  358. response, err = downloadFile("http://pastebin.com/raw.php?i="..tAPIs[selectedAPI]["ID"], name)
  359. else
  360. fs.makeDir("apis")
  361. response, err = downloadFile("http://pastebin.com/raw.php?i="..tAPIs[selectedAPI]["ID"], "apis/"..(tAPIs[selectedAPI]["DefaultName"] or tAPIs[selectedAPI]["Name"]))
  362. setUpStartup()
  363. if tAPIs[selectedAPI]["SampleApplications"] then
  364. fs.makeDir("programs")
  365. for k, v in ipairs(tAPIs[selectedAPI]["SampleApplications"]) do
  366. downloadFile("http://pastebin.com/raw.php?i="..v[1], "programs/"..(tAPIs[selectedAPI]["DefaultName"] or tAPIs[selectedAPI]["Name"]))
  367. end
  368. end
  369. end
  370. if not response then
  371. print(err)
  372. print("Press any key to continue...")
  373. os.pullEvent("key")
  374. end
  375. end
  376. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement