-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOctoKissUpload.py
executable file
·133 lines (111 loc) · 3.2 KB
/
OctoKissUpload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
"""
OctoKissUpload - Upload Gcode to Octoprint
Based on: https://github.com/quillford/OctoUpload
Todo
1: SSL support
"""
version = "0.01"
import sys
import os
import base64
import socket
import urllib
import urllib2
import mimetools
hostIP = '192.168.88.14'
octoPort = '5000'
apiKey = '7CC15816FBE7459XXXXXXXXXX'
sendLoc = 'local' # or 'sdcard'
gcodeExt = 'gcode'
sslBool = 'no'
printBool = 'no'
selectBool = 'yes'
timeout = 15
socket.setdefaulttimeout(timeout)
def prepare_file(infile, gcodeExt=gcodeExt):
#remove extension user may have used on the filename
outputName = infile.split(".")[0]
print outputName
#remove . user may have used on extension
if gcodeExt.find(".") != -1:
gcodeExt = gcodeExt.split(".")[1]
#print gcodeExt
#add extension user specifies
if gcodeExt == "g":
outputName = outputName + "." + gcodeExt
elif gcodeExt == "gco":
outputName = outputName + "." + gcodeExt
else:
outputName = outputName + ".gcode"
print "Ext: " + outputName
send_file(outputName)
def send_file(filename,
sslBool=sslBool,
sendLoc=sendLoc,
printBool=printBool,
selectBool=selectBool,
apiKey=apiKey,
hostIP=hostIP,
octoPort=octoPort):
#username = "spec"
#password = "password"
outputName = os.path.split(filename)[1]
#allows for SSL if user specifies
if sslBool == "yes":
protocol = "https://"
else:
protocol = "http://"
#sends the gcode to either sd or local
if sendLoc == "sdcard":
url = protocol + hostIP + ":" + octoPort + "/api/files/sdcard"
else:
url = protocol + hostIP + ":" + octoPort + "/api/files/local"
#makes sure user submits a valid option for selecting
if selectBool != ('yes' or 'no'):
selectBool = 'no'
print "Select: " + selectBool
#makes sure user submits a valid option for printing
if printBool != ('yes' or 'no'):
printBool = 'no'
print "Print: " + selectBool
filebody = open(filename, 'rb').read()
mimetype = 'application/octet-stream'
boundary = mimetools.choose_boundary()
content_type = 'multipart/form-data; boundary=%s' % boundary
body = []
body_boundary = '--' + boundary
body = [ body_boundary,
'Content-Disposition: form-data; name="file"; filename="%s"' % outputName,
'Content-Type: %s' % mimetype,
'',
filebody,
'--' + boundary,
'Content-Disposition: form-data; name="select"',
'',
selectBool,
'--' + boundary,
'Content-Disposition: form-data; name="print"',
'',
printBool,
]
body.append('--' + boundary + '--')
body.append('')
body = '\r\n'.join(body)
req = urllib2.Request(url)
# Uncomment below two lines for basic auth support. (Used in cases where haproxy is in front of octoprint, with basic auth enabled).
#base64string = base64.encodestring('%s:%s' % (username, password))
#req.add_header("Authorization", "Basic %s" % base64string)
req.add_header('User-agent', 'Cura AutoUploader Plugin')
req.add_header('Content-type', content_type)
req.add_header('Content-length', len(body))
req.add_header('X-Api-Key', apiKey)
req.add_data(body)
print "Uploading..."
print urllib2.urlopen(req).read()
print "Done"
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('usage: OctoKissUpload.py <filename>')
infilename = sys.argv[1]
prepare_file(infilename)