Advertisement
LDShadowLord

MyDown

Dec 27th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "mydown pb <code> <filename>" )
  4.     print( "mydown tyc <code> <filename>" )
  5. end
  6.  
  7. local tArgs = { ... }
  8. if #tArgs < 2 then
  9.     printUsage()
  10.     return
  11. end
  12.  
  13. if not http then
  14.     print( "MyDown requires http API" )
  15.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  16.     return
  17. end
  18.  
  19. local sCommand = tArgs[1]
  20. if sCommand == "tyc" then
  21.     -- Download a file from tycoonier.co.uk
  22.     if #tArgs < 3 then
  23.         printUsage()
  24.         return
  25.     end
  26.  
  27.     -- Determine file to download
  28.     local sCode = tArgs[2]
  29.     local sFile = tArgs[3]
  30.     local sPath = shell.resolve( sFile )
  31.     if fs.exists( sPath ) then
  32.         print( "File already exists" )
  33.         return
  34.     end
  35.    
  36.     -- GET the contents from pastebin
  37.     write( "Connecting to tycoonier.co.uk... " )
  38.     local response = http.get(
  39.         "http://tycoonier.co.uk/dl/upload/"..textutils.urlEncode( sCode )..".txt"
  40.         )
  41.        
  42.     if response then
  43.         print( "Success." )
  44.        
  45.         local sResponse = response.readAll()
  46.         response.close()
  47.        
  48.         local file = fs.open( sPath, "w" )
  49.         file.write( sResponse )
  50.         file.close()
  51.        
  52.         print( "Downloaded as "..sFile )
  53.        
  54.     else
  55.         print( "Failed." )
  56.     end
  57.  
  58. elseif sCommand == "tyc73" then
  59.     -- Download a file from tycoonier.co.uk/dl
  60.     if #tArgs < 3 then
  61.         printUsage()
  62.         return
  63.     end
  64.  
  65.     -- Determine file to download
  66.     local sCode = tArgs[2]
  67.     local sFile = tArgs[3]
  68.     local sPath = shell.resolve( sFile )
  69.     if fs.exists( sPath ) then
  70.         print( "File already exists" )
  71.         return
  72.     end
  73.    
  74.     -- GET the contents from pastebin
  75.     write( "Connecting to tycoonier.co.uk... " )
  76.     local response = http.get(
  77.         "http://tycoonier.co.uk/dl/"..textutils.urlEncode( sCode )..".txt"
  78.         )
  79.        
  80.     if response then
  81.         print( "Success." )
  82.        
  83.         local sResponse = response.readAll()
  84.         response.close()
  85.        
  86.         local file = fs.open( sPath, "w" )
  87.         file.write( sResponse )
  88.         file.close()
  89.        
  90.         print( "Downloaded as "..sFile )
  91.        
  92.     else
  93.         print( "Failed." )
  94.     end
  95.  
  96. elseif sCommand == "pb" then
  97.     -- Download a file from Pastebin.com
  98.     if #tArgs < 3 then
  99.         printUsage()
  100.         return
  101.     end
  102.  
  103.     -- Determine file to download
  104.     local sCode = tArgs[2]
  105.     local sFile = tArgs[3]
  106.     local sPath = shell.resolve( sFile )
  107.     if fs.exists( sPath ) then
  108.         print( "File already exists" )
  109.         return
  110.     end
  111.    
  112.     -- GET the contents from pastebin
  113.     write( "Connecting to pastebin.com... " )
  114.     local response = http.get(
  115.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  116.         )
  117.        
  118.     if response then
  119.         print( "Success." )
  120.        
  121.         local sResponse = response.readAll()
  122.         response.close()
  123.        
  124.         local file = fs.open( sPath, "w" )
  125.         file.write( sResponse )
  126.         file.close()
  127.        
  128.         print( "Downloaded as "..sFile )
  129.        
  130.     else
  131.         print( "Failed." )
  132.     end
  133.  
  134. else
  135.     printUsage()
  136.     return
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement