Advertisement
Tag365

Internet API

May 6th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. -- Variables --
  2. pageCache = {}
  3. -- File sizes.
  4. kilobytes = 1024
  5. megabytes = 1024*1024
  6. gigabytes = 1024*1024*1024
  7. terabytes = 1024*1024*1024*1024
  8. KB = kilobytes
  9. MB = megabytes
  10. GB = gigabytes
  11. TB = terabytes
  12.  
  13. -- Configuration --
  14. defaultBrowserLocation = "browser"
  15. cacheLocation = "internetCache"
  16. cacheEnabled = fs.getFreeSpace("") > 1*MB
  17. maxPageCacheSize = 20*MB
  18.  
  19. local oget, opost, orequest
  20.  
  21. if http then
  22. if not http.Modified then
  23. oget = http.get
  24. opost = http.post
  25. orequest = http.request
  26. end
  27. end
  28.  
  29. function serializeCacheLocation(url, postData, headers)
  30. return cacheLocation.."/"..tostring(string.sub(url or "none", 6, -1).."/"..(string.sub(postData or "none", 1, 20) or "none").."/"..(string.sub(textutils.serialize(headers or {}) or "none", 1, 20) or "none"))
  31. end
  32.  
  33. -- This function adds a page to the page cache.
  34. function addToCache(url, postData, headers, handle)
  35. local findStr = serializeCacheLocation(url, postData, headers)
  36. fs.makeDir(cacheLocation)
  37. fs.makeDir(cacheLocation.."/"..string.sub(url or "none", 6, -1))
  38. fs.makeDir(cacheLocation.."/"..string.sub(url or "none", 6, -1).."/"..(string.sub(postData or "none", 1, 20) or "none"))
  39. local file = fs.open(findStr, "w")
  40. if file then
  41. file.writeLine(handle.getResponseCode() or "100")
  42. file.write(handle.readAll())
  43. file.close()
  44. return true
  45. end
  46. end
  47.  
  48.  
  49. function pullFromCache(url, postData, headers)
  50. local findStr = serializeCacheLocation(url, postData, headers)
  51. local file = fs.open(findStr, "r")
  52. if file then
  53. --os.queueEvent( "http_success", url, file )
  54. file.responseCode = tonumber(file.readLine())
  55. file.getResponseCode = function()
  56. return file.responseCode
  57. end
  58. return file
  59. end
  60. return false
  61. end
  62.  
  63. -- Override the final http functions with ones that pull files from the cache.
  64. if http then
  65. if oget and cacheEnabled then
  66. function request(url, postData, headers)
  67. local url, file = pullFromCache(url, postData, headers)
  68. if file then
  69. os.queueEvent( "http_success", url, file )
  70. end
  71. orequest(url or "", postData, headers)
  72. end
  73. function get(url, headers)
  74. if not string.find(url, "pastebin.com") then
  75. local url1, file = pullFromCache(url, nil, headers)
  76. if file then
  77. return file
  78. end
  79. end
  80. local handle, err = oget(url, headers)
  81. if handle then
  82. addToCache(url, nil, headers, handle)
  83. end
  84. return pullFromCache(url, nil, headers)
  85. end
  86. function post(url, postData, headers)
  87. local url1, file = pullFromCache(url, postData, headers)
  88. if file then
  89. return file
  90. end
  91. local handle, err = opost(url, postData, headers)
  92. if handle then
  93. pcall(addToCache, url, postData, headers, handle)
  94. end
  95. return handle, err
  96. end
  97. http.get = get
  98. http.post = post
  99. http.request = request
  100. end
  101. end
  102.  
  103. function resetCache()
  104. for k, v in ipairs(fs.list(cacheLocation)) do
  105. fs.delete(cacheLocation.."/"..v)
  106. end
  107. end
  108.  
  109. function overwriteFileRaw(contents, destination)
  110. local file, err = fs.open(destination, "w")
  111. if file then
  112. file.write(contents)
  113. file.close()
  114. return true
  115. end
  116. return false, err
  117. end
  118.  
  119. -- Overwrites a file with the contents you give as the first argument.
  120. function overwriteFile(contents, destination)
  121. local pcallSuccess, returnCondition, errorString = pcall(overwriteFileRaw, contents, destination)
  122. if pcallSuccess then
  123. return returnCondition, errorString
  124. end
  125. return false, returnCondition
  126. end
  127.  
  128. function pullFileRaw(url, header, post)
  129. if http then
  130. local handle, str
  131. if not post then
  132. handle, str = http.get(url, header), ""
  133. else
  134. handle, str = http.post(url, post, header), ""
  135. end
  136. if handle then
  137. str = handle.readAll()
  138. handle.close()
  139. end
  140. return str
  141. end
  142. return false, "Http API is not enabled"
  143. end
  144.  
  145. -- Downloads a file from the internet.
  146. -- The url is the file to download.
  147. function pullFile(url, header, post)
  148. local pcallSuccess, returnCondition, errorString = pcall(pullFileRaw, url, header, post)
  149. if pcallSuccess then
  150. return returnCondition, errorString
  151. end
  152. return false, returnCondition
  153. end
  154.  
  155.  
  156. -- Downloads a file from the internet.
  157. -- The url is the file to download, and the destination is what file will be overwritten.
  158. function downloadFileRaw(url, destination, header, post)
  159. if http then
  160. local handle, str
  161. if not post then
  162. handle, str = http.get(url, header), ""
  163. else
  164. handle, str = http.post(url, post, header), ""
  165. end
  166. if handle then
  167. str = handle.readAll()
  168. handle.close()
  169. return overwriteFile(str, destination)
  170. end
  171. return str
  172. end
  173. return false, "Http API is not enabled"
  174. end
  175.  
  176. -- Downloads a file from the internet.
  177. -- The url is the file to download, and the destination is what file will be overwritten.
  178. function downloadFile(url, destination, header, post)
  179. local pcallSuccess, returnCondition, errorString = pcall(downloadFileRaw, url, destination, header, post)
  180. if pcallSuccess then
  181. return returnCondition, errorString
  182. end
  183. return false, returnCondition
  184. end
  185.  
  186. -- Opens the default web browser.
  187. function openInternetBrowser(url)
  188. if not fs.exists(defaultBrowserLocation) then
  189. shell.run("pastebin get VDUGPdiA "..defaultBrowserLocation)
  190. end
  191. shell.run(defaultBrowserLocation, url)
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement