def SaveTo(self, saveToFile = ''): id = self.Save() if (id == None or id == ""): return False #Wait for it to be possibly ready sleep((3000 + self.request.options.delay) / 1000) #Wait for it to be ready. while(1): status = self.GetStatus(id) if not(status.Cached) and not(status.Processing): raise GrabzItException.GrabzItException("The capture did not complete with the error: " + status.Message, GrabzItException.GrabzItException.RENDERING_ERROR) break elif status.Cached: result = self.GetResult(id) if result == None: raise GrabzItException.GrabzItException("The capture could not be found on GrabzIt.", GrabzItException.GrabzItException.RENDERING_MISSING_SCREENSHOT) break if (saveToFile == None or saveToFile == ""): return result fo = open(saveToFile, "wb") fo.write(result) fo.close() break sleep(3) return True
def CheckResponseHeader(self, httpCode): if httpCode == 403: raise GrabzItException.GrabzItException( 'Potential DDOS Attack Detected. Please wait for your service to resume shortly. Also please slow the rate of requests you are sending to GrabzIt to ensure this does not happen in the future.', GrabzItException.GrabzItException.NETWORK_DDOS_ATTACK) elif httpCode >= 400: raise GrabzItException.GrabzItException( 'A network error occured when connecting to the GrabzIt servers.', GrabzItException.GrabzItException.NETWORK_GENERAL_ERROR)
def Save(self, callBackURL = ''): if (self.request == None ): raise GrabzItException.GrabzItException("No parameters have been set.", GrabzItException.GrabzItException.PARAMETER_MISSING_PARAMETERS) sig = self.CreateSignature(self.request.options._getSignatureString(self.applicationSecret, callBackURL, self.request._targetUrl())) obj = self._take(sig, callBackURL) if (obj == None or obj == ""): obj = self._take(sig, callBackURL) if (obj == None or obj == ""): raise GrabzItException.GrabzItException('An unknown network error occurred, please try calling this method again.', GrabzItException.GrabzItException.NETWORK_GENERAL_ERROR) return self.GetResultObject(obj, "ID");
def ReadHTMLFile(self, path): try: return open(path, 'rb').read() except: raise GrabzItException.GrabzItException( "File: " + path + " does not exist", GrabzItException.GrabzItException.FILE_NON_EXISTANT_PATH)
def AddWaterMark(self, identifier, path, xpos, ypos): files = [] try: files.append( ['watermark', os.path.basename(path), open(path, 'rb').read()]) except: raise GrabzItException.GrabzItException( "File: " + path + " does not exist", GrabzItException.GrabzItException.FILE_NON_EXISTANT_PATH) sig = self.CreateSignature( str(self.applicationSecret) + "|" + str(identifier) + "|" + str(xpos) + "|" + str(ypos)) fields = {} fields['key'] = self.applicationKey fields['identifier'] = identifier fields['xpos'] = str(xpos) fields['ypos'] = str(ypos) fields['sig'] = sig return self.IsSuccessful( self.HTTPPost("/services/addwatermark.ashx", fields, files))
def Save(self, callBackURL = ''): if (self.request == None ): raise GrabzItException.GrabzItException("No parameters have been set.", GrabzItException.GrabzItException.PARAMETER_MISSING_PARAMETERS) sig = self.CreateSignature(self.request.options._getSignatureString(self.applicationSecret, callBackURL, self.request._targetUrl())) if (self.request.isPost == False): return self.GetResultObject(self.HTTPGet(self.request.url + '?' + urlencode(self.request.options._getParameters(self.applicationKey, sig, callBackURL, 'url', self.request.data))), "ID") else: return self.GetResultObject(self.HTTPPost(self.request.url, self.request.options._getParameters(self.applicationKey, sig, callBackURL, 'html', self.request.data)), "ID")
def CheckForException(self, dom): if dom == None: return message = "" code = "" messageNodes = dom.getElementsByTagName("Message") codeNodes = dom.getElementsByTagName("Code") for messageNode in messageNodes: if messageNode.firstChild == None: break message = messageNode.firstChild.nodeValue break for codeNode in codeNodes: if codeNode.firstChild == None: break code = codeNode.firstChild.nodeValue break if len(message) > 0: raise GrabzItException.GrabzItException(message, code)