Advertisement
PofMagicfingers

LPA

Feb 19th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. --LPA by: http://youtube.com/thepofplayer
  2. --This is a modified version of the PasteBin script to work directly with lpa's ThePofPlayer program files.
  3. --Based on TurtleScripts.com
  4. local tArgs = { ... }
  5.  
  6. local function termClear()
  7.     term.clear()
  8.     term.setCursorPos(1,1)
  9.     print( " LPA v1.1 BETA (#h8ZjhpU7)" )
  10.     print( "---------------" )
  11.     print("")
  12. end
  13.  
  14. local function printUsage()
  15.   term.clear()
  16.   term.setCursorPos(1,1)
  17.     print( " LPA v1.1 BETA (#h8ZjhpU7)" )
  18.     print( "---------------" )
  19.     print( "by: Pof Magicfingers" )
  20.     print( " " )
  21.     print( "Utilisation:" )
  22.     print( " == Recuperation ==" )
  23.     print( "  lpa (episode) (program)" )
  24.     print( " == Liste des programmes ==" )
  25.     print( "  lpa (episode)" )
  26.     print( " == Mise a jour LPA ==" )
  27.     print( "  lpa update" )
  28.     print( " " )
  29. end
  30.  
  31. function decode(resp)
  32.   resp = resp or ""
  33.   local table = {}
  34.   local index = 1
  35.  
  36.   i, j = string.find(resp, "/PofMagicfingers/lpa_computercraft/blob/[^/]+/")
  37.   while i ~= nil do
  38.     content = string.sub(resp, j+1, string.find(resp, "\"", j)-1)
  39.     if not string.match(content, ".spec$") and not string.match(content, ".md$") then
  40.         if string.match(content, ".lua$") then
  41.             content = string.gsub(content, ".lua$","")
  42.         end
  43.         table[index] = content
  44.         index = index + 1
  45.     end
  46.     i, j = string.find(resp, "/PofMagicfingers/lpa_computercraft/blob/[^/]+/", j)
  47.   end
  48.  
  49.   return table
  50. end
  51.  
  52. function printlist(resp, ep)
  53.   resp = resp or ""
  54.  
  55.   local table = decode(resp)
  56.   local printed = 0
  57.   local per_page = 4
  58.  
  59.   termClear()
  60.   term.setCursorPos(1,3)
  61.  
  62.   print("Episode "..ep..": Programmes (1/"..math.ceil(#table/per_page)..")")
  63.   print("============================")
  64.   print("")
  65.  
  66.   for i=1,#table do
  67.     if printed == per_page then
  68.       print("")
  69.       print("Suite: Appuyez sur une touche...")
  70.       os.pullEvent ("key")
  71.       printed = 0
  72.       termClear()
  73.       term.setCursorPos(1,3)
  74.       print("Episode "..ep..": Programmes ("..math.ceil(i/per_page).."/"..math.ceil(#table/per_page)..")")
  75.       print("============================")
  76.       print("")
  77.     end
  78.     print("   - "..table[i] )
  79.     printed = printed + 1
  80.   end
  81.   print("")
  82. end
  83.  
  84. local function getFile(episode, program)
  85.     termClear()
  86.  
  87.     if episode == "update" and program == "" then
  88.         episode = "master" ; program = "lpa"
  89.     end
  90.  
  91.     if episode == "" then
  92.         print( "Vous devez fournir un numero d'episode !" ) ; return
  93.     else
  94.         if type(tonumber(episode)) == "number" then
  95.             episode = "ep"..tostring(tonumber(episode))
  96.         end
  97.     end
  98.  
  99.     if program == "" then
  100.         termClear()
  101.         write( "Connexion... " )
  102.         local response = http.get("https://github.com/PofMagicfingers/lpa_computercraft/file-list/"..episode)
  103.         if response then
  104.             local sResponse = response.readAll()
  105.             response.close()
  106.             printlist(sResponse, episode)
  107.         else
  108.             print( "Echec." )
  109.             print( " " )
  110.             print( "Verifiez le point suivant :" )
  111.             print( "  - (episode) : est un nombre")
  112.             print("")
  113.             print( "      exemple : lpa 3 " )
  114.             print("")
  115.         end
  116.         return
  117.     end
  118.     termClear()
  119.     write( "Connexion... " )
  120.     local response = http.get("https://raw2.github.com/PofMagicfingers/lpa_computercraft/"..episode.."/"..program..".lua")
  121.     if not response then
  122.         response = http.get("https://raw2.github.com/PofMagicfingers/lpa_computercraft/"..episode.."/"..program)
  123.     end
  124.     if response then
  125.         print( "OK !" )
  126.         local sResponse = response.readAll()
  127.         response.close()
  128.        
  129.         local sPath = shell.resolve( program )
  130.  
  131.         local spec_response = http.get("https://raw2.github.com/PofMagicfingers/lpa_computercraft/"..episode.."/"..program..".spec")
  132.         if spec_response then
  133.             local tResponse = textutils.unserialize(spec_response.readAll())
  134.             spec_response.close()
  135.             if tResponse and tResponse["installPath"] then
  136.                 sPath = tResponse["installPath"] ; program = sPath
  137.             end
  138.         end
  139.  
  140.         sDirPath = string.match(sPath, "(.-)([^/]-)$")
  141.  
  142.         pcall(function() fs.makeDir(sDirPath) end)
  143.  
  144.         if (not fs.isDir( sDirPath ) and fs.exists( sDirPath )) then
  145.             print("Impossible de creer le dossier d'installation "..sDirPath)
  146.             print("Creez le dossier manuellement et reessayez.")
  147.             return
  148.         end
  149.  
  150.         if fs.exists( sPath ) then
  151.             if fs.isDir( sPath ) then
  152.                 print("Un dossier "..sPath.." existe deja, renommez le ou supprimez le pour pouvoir installer ce programme")
  153.                 return
  154.             else
  155.                 local override = ""
  156.                 printed = 0
  157.                 while override ~= "n" and override ~= "o" do
  158.                     if printed == 3 then
  159.                         termClear()
  160.                         printed = 0
  161.                     end
  162.                     print( "Le programme "..program.." existe !" )
  163.                     print( "Remplacer ? (o pour oui, n pour non)" )
  164.                     print( " " )
  165.                     event, override = os.pullEvent ("char")
  166.                     printed = printed + 1
  167.                 end
  168.                 if override == "n" then return end
  169.             end
  170.         end
  171.        
  172.         local file = fs.open( sPath, "w" )
  173.         file.write( sResponse )
  174.         file.close()
  175.         print( " " )
  176.         print( "Nouvelle commande : "..program )
  177.         print( "[===========================] 100%" )
  178.         print( " "..string.len(sResponse).." octets")
  179.         print( " " )
  180.         print( "Telechargement accompli." )
  181.     else
  182.         print( "Echec." )
  183.         print( " " )
  184.         print( "Verifiez les points suivant :" )
  185.         print( "  - (program) : majuscules importantes" )
  186.         print( "  - (episode) : est un nombre")
  187.         print( " ")
  188.         print( "exemple : lpa 3 programmeCool " )
  189.         print(" ")
  190.     end
  191. end
  192.  
  193. local gui_mode = false
  194. if #tArgs < 1 then
  195.     printUsage()
  196.     return
  197. end
  198. local episode = tArgs[1] or ""
  199. local program = tArgs[2] or ""
  200. getFile(episode, program)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement