def __init__(self,jid,message_id,url,mediaType_id,mediaId,account,resize=False): WADebug.attach(self); path = self.getSavePath(mediaType_id); filename = url.split('/')[-1] if path is None: raise Exception("Unknown media type") if not os.path.exists(path): os.makedirs(path) self.uploadHandler = MediaUploader(jid, account, self.onUploadSuccess, self.onError, self.onProgressUpdated) self.downloadHandler = MediaDownloader(self.onDownloadSuccess, self.onError, self.onProgressUpdated) self.url = url self._path = path+"/"+filename ext = os.path.splitext(filename)[1] self.downloadPath = Utilities.getUniqueFilename(path + "/" + self.getFilenamePrefix(mediaType_id) + ext) self.resize = resize self.mediaId = mediaId self.message_id = message_id self.jid = jid super(WAMediaHandler,self).__init__();
def onImageReceived(self, messageid, jid, preview, url, size, wantsReceipt, isBroadcast): '''Function called when an image is received. Args: messageid (str): a handle to the message. jid (str): the sender WhatsApp account (number + domain). url (str): the url which the image is stored in. wantsReceipt (bool): tells if sender expects reception notification. Not needed: preview, size, isBroadcast. ''' self.log( self.tr('<font color=orange>+{0}</font> sent an image.').format( self.phone[:self.phone.index('@')])) downloader = MediaDownloader( successClbk=lambda path: self.splashImage.emit(path), errorClbk=lambda: self.log('<font color=red>' + self.tr( 'Reception failed.') + '</font>')) downloader.download(url) if jid == self.phone: if wantsReceipt: self.methodsInterface.call('message_ack', (jid, messageid))
def onImageReceived(self, messageId, jid, preview, url, size, wantsReceipt, isBroadcast): print("Image received: Id:%s Jid:%s Url:%s size:%s" %(messageId, jid, url, size)) downloader = MediaDownloader(self.onDlsuccess, self.onDlerror, self.onDlprogress) downloader.download(url) if wantsReceipt and self.sendReceipts: self.methodsInterface.call("message_ack", (jid, messageId)) timeout = 10 t = 0; while t < timeout: time.sleep(0.5) t+=1
def onImageReceived(messageId, jid, preview, url, size, wantsReceipt, isBroadcast): print("Image received: Id:%s Jid:%s Url:%s size:%s" %(messageId, jid, url, size)) print preview downloader = MediaDownloader(onDlsuccess, onDlerror, onDlprogress) downloader.download(url) global bot if wantsReceipt and bot.sendReceipts: bot.methodsInterface.call("message_ack", (jid, messageId)) timeout = 10 t = 0; while t < timeout: time.sleep(0.5) t+=1
def onImageReceived(self, messageid, jid, preview, url, size, wantsReceipt, isBroadcast): """Function called when an image is received. Args: messageid (str): a handle to the message. jid (str): the sender WhatsApp account (number + domain). url (str): the url which the image is stored in. wantsReceipt (bool): tells if sender expects reception notification. Not needed: preview, size, isBroadcast. """ self.log(self.tr("<font color=orange>+{0}</font> sent an image.").format(self.phone[: self.phone.index("@")])) downloader = MediaDownloader( successClbk=lambda path: self.splashImage.emit(path), errorClbk=lambda: self.log("<font color=red>" + self.tr("Reception failed.") + "</font>"), ) downloader.download(url) if jid == self.phone: if wantsReceipt: self.methodsInterface.call("message_ack", (jid, messageid))
class WAMediaHandler(QObject): progressUpdated = QtCore.Signal(int,int) #%,progress,mediaid error = QtCore.Signal(str,int) success = QtCore.Signal(str,int,str,str, str) def __init__(self,jid,message_id,url,mediaType_id,mediaId,account,resize=False): WADebug.attach(self); path = self.getSavePath(mediaType_id); filename = url.split('/')[-1] if path is None: raise Exception("Unknown media type") if not os.path.exists(path): os.makedirs(path) self.uploadHandler = MediaUploader(jid, account, self.onUploadSuccess, self.onError, self.onProgressUpdated) self.downloadHandler = MediaDownloader(self.onDownloadSuccess, self.onError, self.onProgressUpdated) self.url = url self._path = path+"/"+filename ext = os.path.splitext(filename)[1] self.downloadPath = Utilities.getUniqueFilename(path + "/" + self.getFilenamePrefix(mediaType_id) + ext) self.resize = resize self.mediaId = mediaId self.message_id = message_id self.jid = jid super(WAMediaHandler,self).__init__(); def onError(self): self.error.emit(self.jid,self.message_id) def onUploadSuccess(self, url): #filename = os.path.basename(self._path) #filesize = os.path.getsize(self._path) #data = url + "," + filename + "," + str(filesize); self.success.emit(self.jid,self.message_id, self._path, "upload", url) def onDownloadSuccess(self, path): try: shutil.copyfile(path, self.downloadPath) os.remove(path) self.success.emit(self.jid, self.message_id, self.downloadPath, "download", "") except: print("Error occured at transfer %s"%sys.exc_info()[1]) self.error.emit(self.jid, self.message_id) def onProgressUpdated(self,progress): self.progressUpdated.emit(progress, self.mediaId); @async def pull(self): self.action = "download" self.downloadHandler.download(self.url) @async def push(self, uploadUrl): self.action = "upload" path = self.url.replace("file://","") filename = os.path.basename(path) filetype = mimetypes.guess_type(filename)[0] self._path = path self.uploadHandler.upload(path, uploadUrl) def getFilenamePrefix(self, mediatype_id): if mediatype_id == WAConstants.MEDIA_TYPE_IMAGE: return strftime("owhatsapp_image_%Y%m%d_%H%M%S", gmtime()) if mediatype_id == WAConstants.MEDIA_TYPE_AUDIO: return strftime("owhatsapp_audio_%Y%m%d_%H%M%S", gmtime()) if mediatype_id == WAConstants.MEDIA_TYPE_VIDEO: return strftime("owhatsapp_video_%Y%m%d_%H%M%S", gmtime()) if mediatype_id == WAConstants.MEDIA_TYPE_VCARD: return strftime("owhatsapp_vcard_%Y%m%d_%H%M%S", gmtime()) return "" def getSavePath(self,mediatype_id): if mediatype_id == WAConstants.MEDIA_TYPE_IMAGE: return WAConstants.IMAGE_PATH if mediatype_id == WAConstants.MEDIA_TYPE_AUDIO: return WAConstants.AUDIO_PATH if mediatype_id == WAConstants.MEDIA_TYPE_VIDEO: return WAConstants.VIDEO_PATH if mediatype_id == WAConstants.MEDIA_TYPE_VCARD: return WAConstants.VCARD_PATH return None