Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same question: Send image and video :) #1710

Open
jacopomarcovaldi opened this issue Jul 26, 2016 · 5 comments
Open

Same question: Send image and video :) #1710

jacopomarcovaldi opened this issue Jul 26, 2016 · 5 comments

Comments

@jacopomarcovaldi
Copy link

Hi all,
i am trying to send an image or a video.
Ok there are hundreds of thread i know but i can't figure out how to do it :(

I had tried with yowsup-cli demos with -x parameters but i had received this message
demos: error: unrecognized arguments: -x [PHONENUMBER] [IMAGEPATH]
running command: sudo yowsup-cli demos -c [CONFIGFILE] -x [PHONENUMBER] [IMAGEPATH]

So i read this guide: https://github.com/tgalal/yowsup/wiki/Upload-and-send-Media and i figured out that i have to write a python script.
layer.py Class
`# -- coding: utf-8 --
import os, subprocess, time,re
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from yowsup.layers import YowLayer
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
from yowsup.layers.protocol_receipts.protocolentities import OutgoingReceiptProtocolEntity
from yowsup.layers.protocol_acks.protocolentities import OutgoingAckProtocolEntity
from yowsup.layers.protocol_media.protocolentities import *
from yowsup.layers.protocol_media.mediauploader import MediaUploader
from yowsup.layers.protocol_profiles.protocolentities import *
from subprocess import PIPE
from yowsup.layers.auth import YowAuthenticationProtocolLayer
from yowsup.layers import YowLayerEvent
from yowsup.layers.network import YowNetworkLayer
import sys, getopt
from yowsup.common import YowConstants
import datetime
import os
import logging
from yowsup.layers.protocol_receipts.protocolentities import *
from yowsup.layers.protocol_groups.protocolentities import *
from yowsup.layers.protocol_presence.protocolentities import *
from yowsup.layers.protocol_messages.protocolentities import *
from yowsup.layers.protocol_acks.protocolentities import *
from yowsup.layers.protocol_ib.protocolentities import *
from yowsup.layers.protocol_iq.protocolentities import *
from yowsup.layers.protocol_contacts.protocolentities import *
from yowsup.layers.protocol_chatstate.protocolentities import *
from yowsup.layers.protocol_privacy.protocolentities import *
from yowsup.layers.protocol_media.protocolentities import *
from yowsup.layers.protocol_media.mediauploader import MediaUploader
from yowsup.layers.protocol_profiles.protocolentities import *
class Bracco(YowInterfaceLayer):

funzioni per l'upload di immagini

    def doSendImage(self, filePath, url, to, ip = None, caption = None):                                                                                                                                  
            entity = ImageDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to, caption = caption)                                                                                   
            self.toLower(entity)                                                                                                                                                                          
    def doSendVideo(self, filePath, url, to, ip = None, caption = None):                                                                                                                                  
            entity = VideoDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, DownloadableMediaMessageProtocolEntity.MEDIA_TYPE_VIDEO, ip, to)                                             
            self.toLower(entity)                                                                                                                                                                          
    def doSendAudio(self, filePath, url, to, ip = None, caption = None):                                                                                                                                  
            entity = AudioDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to)                                                                                                      
            self.toLower(entity)
    def video_send(self, destJid, path, caption = None):                                                                                                                                                  
            jid = destJid                                                                                                                                                                                 
            print ("sending video to destJid=" + jid)                                                                                                                                                     
            entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_VIDEO, filePath=path)                                                                                         
            successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity, caption)                                                               
            errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)                                                                               
            self._sendIq(entity, successFn, errorFn)                                                                                                                                                      
    def image_send(self, destJid, path, caption = None):                                                                                                                                                  
            jid = destJid                                                                                                                                                                                 
            print ("sending image to destJid=" + jid)                                                                                                                                                     
            print ("path=" + path)                                                                                                                                                                        
            entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path)                                                                                         
            print ("path=" + path)                                                                                                                                                                        
            successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity, caption)                                                               
            print ("caption=" + caption)                                                                                                                                                                  
            errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)                                                                               
            #print ("errorFn=" + errorFn)                                                                                                                                                                 
            self._sendIq(entity, successFn, errorFn)                                                                                                                                                      
            print ("fine")                                                                                                                                                                                

    def onRequestUploadResult(self, jid, filePath, resultRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity, caption = None):                                                                   
            print("onRequestUploadResult-start")                                                                                                                                                          
            if requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_AUDIO:                                                                                                 
                    doSendFn = self.doSendAudio                                                                                                                                                           
            elif requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_VIDEO:                                                                                               
                    doSendFn = self.doSendVideo                                                                                                                                                           
            else:                                                                                                                                                                                         
                    doSendFn = self.doSendImage                                                                                                                                                           
            if resultRequestUploadIqProtocolEntity.isDuplicate():                                                                                                                                         
                    doSendFn(filePath, resultRequestUploadIqProtocolEntity.getUrl(), jid, resultRequestUploadIqProtocolEntity.getIp(), caption)                                                           
            else:                                                                                                                                                                                         
                    successFn = lambda filePath, jid, url: doSendFn(filePath, url, jid, resultRequestUploadIqProtocolEntity.getIp(), caption)                                                             
                    mediaUploader = MediaUploader(jid, self.getOwnJid(), filePath,                                                                                                                        
                                            resultRequestUploadIqProtocolEntity.getUrl(),                                                                                                                 
                                            resultRequestUploadIqProtocolEntity.getResumeOffset(),                                                                                                        
                                            successFn, self.onUploadError, self.onUploadProgress, async=False)                                                                                            
                    mediaUploader.start()                                                                                                                                                                 
                    print("onRequestUploadResult-end")                                                                                                                                                    
    def onRequestUploadError(self, jid, path, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):                                                                                         
            sys.stdout.write("Request upload for file %s for %s failed" % (path, jid))                                                                                                                    
    def onUploadSuccess(self, filePath, jid, url):                                                                                                                                                        
            print("onUploadSuccess-start")                                                                                                                                                                
            #convenience method to detect file/image attributes for sending, requires existence of 'pillow' library                                                                                       
            entity = VideoDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, None, to)                                                                                                    
            self.toLower(entity)                
    def image_send(self, destJid, path, caption = None):                                                                                                                                                  
            jid = destJid                                                                                                                                                                                 
            print ("sending image to destJid=" + jid)                                                                                                                                                     
            print ("path=" + path)                                                                                                                                                                        
            entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path)                                                                                         
            print ("path=" + path)                                                                                                                                                                        
            successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity, caption)                                                               
            print ("caption=" + caption)                                                                                                                                                                  
            errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)                                                                               
            #print ("errorFn=" + errorFn)                                                                                                                                                                 
            self._sendIq(entity, successFn, errorFn)                                                                                                                                                      
            print ("fine")                                                                                                                                                                                

    def onRequestUploadResult(self, jid, filePath, resultRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity, caption = None):                                                                   
            print("onRequestUploadResult-start")                                                                                                                                                          
            if requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_AUDIO:                                                                                                 
                    doSendFn = self.doSendAudio                                                                                                                                                           
            elif requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_VIDEO:                                                                                               
                    doSendFn = self.doSendVideo                                                                                                                                                           
            else:                                                                                                                                                                                         
                    doSendFn = self.doSendImage                                                                                                                                                           
            if resultRequestUploadIqProtocolEntity.isDuplicate():                                                                                                                                         
                    doSendFn(filePath, resultRequestUploadIqProtocolEntity.getUrl(), jid, resultRequestUploadIqProtocolEntity.getIp(), caption)                                                           
            else:                                                                                                                                                                                         
                    successFn = lambda filePath, jid, url: doSendFn(filePath, url, jid, resultRequestUploadIqProtocolEntity.getIp(), caption)                                                             
                    mediaUploader = MediaUploader(jid, self.getOwnJid(), filePath,                                                                                                                        
                                            resultRequestUploadIqProtocolEntity.getUrl(),                                                                                                                 
                                            resultRequestUploadIqProtocolEntity.getResumeOffset(),                                                                                                        
                                            successFn, self.onUploadError, self.onUploadProgress, async=False)                                                                                            
                    mediaUploader.start()                                                                                                                                                                 
                    print("onRequestUploadResult-end")                                                                                                                                                    
    def onRequestUploadError(self, jid, path, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):                                                                                         
            sys.stdout.write("Request upload for file %s for %s failed" % (path, jid))                                                                                                                    
    def onUploadSuccess(self, filePath, jid, url):                                                                                                                                                        
            print("onUploadSuccess-start")                                                                                                                                                                
            #convenience method to detect file/image attributes for sending, requires existence of 'pillow' library                                                                                       
            entity = VideoDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, None, to)                                                                                                    
            self.toLower(entity)                                                                                                                                                                          
    def onUploadError(self, filePath, jid, url):                                                                                                                                                          
            self.toLower(entity)                                                                                                                                                                          
    def onUploadError(self, filePath, jid, url):                                                                                                                                                          
            sys.stdout.write("Upload file %s to %s for %s failed!" % (filePath, url, jid))                                                                                                                
    def onUploadProgress(self, filePath, jid, url, progress):                                                                                                                                             
            sys.stdout.write("%s => %s, %d%% \r" % (os.path.basename(filePath), jid, progress))                                                                                                           
            sys.stdout.flush() `

run.py
`from yowsup.stacks import YowStackBuilder
from layer import bracco
from yowsup.layers.auth import AuthError
from yowsup.layers import YowLayerEvent
from yowsup.layers.network import YowNetworkLayer
from yowsup.env import YowsupEnv
import sys, getopt

credentials = ([PHONENUMBER], [PASSWORD]) # replace with your phone and password

CREDENTIALS = DemosArgParser._getCredentials()

if name== "main":
stackBuilder = YowStackBuilder()

    stack = stackBuilder\                                                                                                                                                                                 
            .pushDefaultLayers(True)\                                                                                                                                                                     
            .push(bracco)\                                                                                                                                                                                
            .build()                                                                                                                                                                                      

    stack.setCredentials(credentials)                                                                                                                                                                     

    stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal                                                                                                  
    oggetto = bracco()                                                                                                                                                                                    
    inputfile = ''                                                                                                                                                                                        
    try:                                                                                                                                                                                                  
            opts, args = getopt.getopt(sys.argv[1:],"hi:v:",["ifile="])                                                                                                                                   
    except getopt.GetoptError:                                                                                                                                                                            
            print 'test.py -i/-v <inputfile>'                                                                                                                                                             
            sys.exit(2)                                                                                                                                                                                   
    for opt, arg in opts:                                                                                                                                                                                 
            if opt == '-h':                                                                                                                                                                               
                    print 'test.py -i/-v <inputfile>'                                                                                                                                                     
                    sys.exit()                                                                                                                                                                            
            elif opt in ("-i", "--ifile", "-v", "--vfile"):                                                                                                                                               
                    inputfile = arg                                                                                                                                                                       
    print 'Input file is', inputfile                                                                                                                                                                      
    print 'opzione e', opt                                                                                                                                                                                
    if opt == '-i':                                                                                                                                                                                       
            oggetto.image_send([PHONENUMBER], inputfile, 'immagine rilevata')                                                                                                                         
    elif opt == '-v':                                                                                                                                                                                     
            oggetto.video_send([PHONENUMBER], inputfile, 'video rilevato')`

oggetto.image_send is called and executed (i see log on shell) but image is not send to the phone.
What am i missing?

thank you for your help.
Jacopo

@sowerkoku
Copy link

see here

@jacopomarcovaldi
Copy link
Author

hi sowerkoku,
thank you for your tip :)
I have read the thread but it's seems that you are talking about an echo client that send images when requested. I already have it, what i need now is a line command to send image and video passing it as parameters.
Maybe i didn't understand your thread :(
Can you help me?
thank you.
Jacopo

@sowerkoku
Copy link

would edit the yowsup-cli and add image and video. I think it's as easy.

@jacopomarcovaldi
Copy link
Author

I am sorry but as java developer i can't understand where to insert the right code.
Anybody can help me with an example or better with code?
thank you :)

@dargmuesli
Copy link

dargmuesli commented Sep 10, 2016

Image and video are currently not really supported by yowsup. See #1564.
Captions do not work for example.
Once I get it running, I can give more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants