1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
6 import httplib as httpclient
8 import cStringIO as StringIO
10 class Youmagine(object):
11 def __init__(self, authToken):
15 self._authToken = authToken
19 self._hostReachable = True
32 ('Creative Commons - Share Alike', 'cc'),
33 ('Creative Commons - Attribution-NonCommerical-ShareAlike', 'ccnc'),
37 def getAuthorizationUrl(self):
40 def getCategories(self):
41 return map(lambda n: n[0], self._categories)
43 def getLicenses(self):
44 return map(lambda n: n[0], self._licenses)
46 def setAuthToken(self, token):
47 self._authToken = token
51 def getAuthToken(self):
52 return self._authToken
54 def isHostReachable(self):
55 return self._hostReachable
57 def viewUrlForDesign(self, id):
58 return 'https://%s/designs/%d' % (self._viewUrl, id)
60 def editUrlForDesign(self, id):
61 return 'https://%s/designs/%d/edit' % (self._viewUrl, id)
63 def isAuthorized(self):
64 if self._authToken is None:
66 if self._userName is None:
67 #No username yet, try to request the username to see if the authToken is valid.
68 result = self._request('GET', '/authorized_integrations/%s/whoami.json' % (self._authToken))
71 self._authToken = None
73 self._userName = result['screen_name']
74 self._userID = result['id']
77 def createDesign(self, name, description, category, license):
78 res = self._request('POST', '/designs.json', {'design[name]': name, 'design[excerpt]': description, 'design[design_category_id]': filter(lambda n: n[0] == category, self._categories)[0][1], 'design[license]': filter(lambda n: n[0] == license, self._licenses)[0][1]})
84 def publishDesign(self, id):
85 res = self._request('PUT', '/designs/%d/mark_as/publish.json' % (id), {'ignore': 'me'})
91 def createDocument(self, designId, name, contents):
92 res = self._request('POST', '/designs/%d/documents.json' % (designId), {'document[name]': name, 'document[description]': 'Uploaded from Cura'}, {'document[file]': (name, contents)})
98 def createImage(self, designId, name, contents):
99 res = self._request('POST', '/designs/%d/images.json' % (designId), {'image[name]': name, 'image[description]': 'Uploaded from Cura'}, {'image[file]': (name, contents)})
105 def listDesigns(self):
106 res = self._request('GET', '/users/%s/designs.json' % (self._userID))
109 def _request(self, method, url, postData = None, files = None):
111 if self._authToken is not None:
112 url += '?auth_token=%s' % (self._authToken)
113 error = 'Failed to connect to %s' % self._hostUrl
114 for n in xrange(0, retryCount):
115 if self._http is None:
116 self._http = httpclient.HTTPSConnection(self._hostUrl)
118 if files is not None:
119 boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
120 s = StringIO.StringIO()
121 for k, v in files.iteritems():
124 s.write('--%s\r\n' % (boundary))
125 s.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (k, filename))
126 s.write('Content-Type: application/octet-stream\r\n')
127 s.write('Content-Transfer-Encoding: binary\r\n')
129 s.write(fileContents)
132 for k, v in postData.iteritems():
133 s.write('--%s\r\n' % (boundary))
134 s.write('Content-Disposition: form-data; name="%s"\r\n' % (k))
138 s.write('--%s--\r\n' % (boundary))
141 self._http.request(method, url, StringIO.StringIO(s), {"Content-type": "multipart/form-data; boundary=%s" % (boundary), "Content-Length": len(s)})
142 elif postData is not None:
143 self._http.request(method, url, urllib.urlencode(postData), {"Content-type": "application/x-www-form-urlencoded"})
145 self._http.request(method, url)
150 response = self._http.getresponse()
151 responseText = response.read()
156 if responseText == '':
158 return json.loads(responseText)
160 print response.getheaders()
161 error = 'Failed to decode JSON response'
162 self._hostReachable = False
163 return {'error': error}
166 #Fake Youmagine class to test without internet
167 class FakeYoumagine(Youmagine):
168 def __init__(self, authToken):
169 super(FakeYoumagine, self).__init__(authToken)
170 self._authUrl = 'file:///C:/Models/output.html'
171 self._authToken = None
173 def isAuthorized(self):
174 if self._authToken is None:
176 if self._userName is None:
177 self._userName = 'FakeYoumagine'
181 def isHostReachable(self):
184 def createDesign(self, name, description, category, license):
187 def publishDesign(self, id):
190 def createDocument(self, designId, name, contents):
191 print "Create document: %s" % (name)
192 f = open("C:/models/%s" % (name), "wb")
197 def createImage(self, designId, name, contents):
198 print "Create image: %s" % (name)
199 f = open("C:/models/%s" % (name), "wb")
204 def listDesigns(self):
207 def _request(self, method, url, postData = None, files = None):
208 print "Err: Tried to do request: %s %s" % (method, url)
211 ym = Youmagine('j3rY9kQF62ptuZF7vqbR')
212 if not ym.isAuthorized():
213 print "Failed to authorize"
215 for design in ym.listDesigns():