forked from adeebshihadeh/OctoUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctoUpload.py
More file actions
148 lines (126 loc) · 4.17 KB
/
OctoUpload.py
File metadata and controls
148 lines (126 loc) · 4.17 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Name: OctoUpload
#Info: Uploads gcode to OctoPrint server after slicing
#Depend: GCode
#Type: postprocess
#Param: uploadBool(string:yes) Upload gcode after slice
#Param: hostIP(string:octopi.local) IP Address
#Param: octoPort(string:80) Port
#Param: apiKey(string:) API Key
#Param: outputName(string:output) Output Filename
#Param: sendLoc(string:local) OctoPrint Location (local or sdcard)
#Param: gcodeExt(string:gcode) GCode Extension
#Param: sslBool(string:no) SSL (yes/no)
#Param: selectBool(string:yes) Select once uploaded (yes/no)
#Param: printBool(string:no) Print after upload (yes/no)
# todo
# 1. Automatically figure out a default filename based on first STL loaded
# 2. Create a checkmark for SSL vs Non-SSL
# 3. Optional Settings (ideally a expand/hidey thing) for octoPort, outputName (Optional because See #1), and basic auth username/password
version = "0.1"
import base64
import socket
import urllib
import urllib2
import mimetools
import sys
timeout = 15
socket.setdefaulttimeout(timeout)
print uploadBool
print hostIP
print octoPort
print apiKey
print outputName
print sendLoc
print gcodeExt
print sslBool
print selectBool
#skip upload if not enabled
if uploadBool != "yes":
print "Not uploading so exiting"
sys.exit()
#remove extension user may have used on the filename
outputName = outputName.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
username = "spec"
password = "password"
#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
if "darwin" == sys.platform:
# Monkey path socket.sendall to handle EAGAIN (Errno 35) on mac.
# (https://github.com/shazow/urllib3/issues/63)
import socket
import time
def socket_socket_sendall(self, data):
while len(data) > 0:
try:
bytes_sent = self.send(data)
data = data[bytes_sent:]
except socket.error, e:
if str(e) == "[Errno 35] Resource temporarily unavailable":
time.sleep(0.1)
else:
raise e
socket._socketobject.sendall = socket_socket_sendall
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"