· 4 years ago · Apr 05, 2021, 01:44 PM
1
2local function printUsage()
3 print( "Usages:" )
4 print( "pastebin put <filename>" )
5 print( "pastebin get <code> <filename>" )
6end
7
8local tArgs = { ... }
9if #tArgs < 2 then
10 printUsage()
11 return
12end
13
14if not http then
15 print( "Pastebin requires http API" )
16 print( "Set enableAPI_http to true in ComputerCraft.cfg" )
17 return
18end
19
20local sCommand = tArgs[1]
21if sCommand == "put" then
22 -- Upload a file to pastebin.com
23 -- Determine file to upload
24 local sFile = tArgs[2]
25 local sPath = shell.resolve( sFile )
26 if not fs.exists( sPath ) or fs.isDir( sPath ) then
27 print( "No such file" )
28 return
29 end
30
31 -- Read in the file
32 local sName = fs.getName( sPath )
33 local file = fs.open( sPath, "r" )
34 local sText = file.readAll()
35 file.close()
36
37 -- POST the contents to pastebin
38 write( "Connecting to pastebin.com... " )
39 local key = "0ec2eb25b6166c0c27a394ae118ad829"
40 local response = http.post(
41 "http://pastebin.com/api/api_post.php",
42 "api_option=paste&"..
43 "api_dev_key="..key.."&"..
44 "api_paste_format=lua&"..
45 "api_paste_name="..textutils.urlEncode(sName).."&"..
46 "api_paste_code="..textutils.urlEncode(sText)
47 )
48
49 if response then
50 print( "Success." )
51
52 local sResponse = response.readAll()
53 response.close()
54 print("rola")
55 print(sResponse)
56 local sCode = string.match( sResponse, "[^/]+$" )
57 print( "Uploaded as "..sResponse )
58 print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
59
60 else
61 print( "Failed." )
62 end
63
64elseif sCommand == "get" then
65 -- Download a file from pastebin.com
66 if #tArgs < 3 then
67 printUsage()
68 return
69 end
70
71 -- Determine file to download
72 local sCode = tArgs[2]
73 local sFile = tArgs[3]
74 local sPath = shell.resolve( sFile )
75 if fs.exists( sPath ) then
76 print( "File already exists" )
77 return
78 end
79
80 -- GET the contents from pastebin
81 write( "Connecting to pastebin.com... " )
82 local response = http.get("https://pastebin.com/raw/"..textutils.urlEncode(sCode))
83 if response then
84 print( "Success." )
85
86 local sResponse = response.readAll()
87 response.close()
88
89 local file = fs.open( sPath, "w" )
90 file.write( sResponse )
91 file.close()
92
93 print( "Downloaded as "..sFile )
94
95 else
96 print( "Failed." )
97 end
98
99else
100 printUsage()
101 return
102end