Thursday, May 5, 2011

Fun With Python: play tone when ur friend become online

=======play.py=========================

import pyaudio
import wave
import sys
def play():
chunk = 1024

x='C:/Documents and Settings/Dipankar/Desktop/Dropbox/IITR/Python Exp/PythonAudio/tone.wav'

wf = wave.open(x, 'rb')

p = pyaudio.PyAudio()

# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)

# read data
data = wf.readframes(chunk)

# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)

stream.close()
p.terminate()

==========================================
import xmpp
import sys
import time
import thread
import play
#this function for break the infinite loop-------or on log out--------
def StepOn(conn):
try:
conn.Process(1)
except KeyboardInterrupt:
return 0
return 1

def GoOn(conn):
while StepOn(conn):
pass
#---------------------------Sending SMS routine----------------------------------
def SendSMS(cl):
#for i in range(0,10):
cl.send(xmpp.Message( "dutta.dipankar08@gmail.com" ,"This is an Experimental WOrk..!!!!..My Gtalk Is now Automated...PLZ Config AI system" ))

##-------------------------------Reciving sms routine------------------------------------------------
def messageCB(cl,msg):
Sender=msg.getFrom()
print "Content: " + str(msg.getBody())
#print msg
#SendSMS(cl);
cl.send(xmpp.Message(Sender ,"his is an Experimental WOrk..!!!!..My Gtalk Is now Automated...PLZ Config AI system" ))
play.play()
##-------------------------------Presence sms routine------------------------------------------------
def presenceCB(conn,msg):
#print str(msg)
prs_type=msg.getType()
who=msg.getFrom()
status=msg.getStatus()
print msg.getType(),who,status
if prs_type == "subscribe":
conn.send(xmpp.Presence(to=who, typ = 'subscribed'))
#conn.send(xmpp.Presence(to=who, typ = 'subscribe'))


#----------update status1: TIMER---------------------------------------------
def updateStatus1(conn):
while(1):
localtime = time.asctime( time.localtime(time.time()) )
pres = xmpp.Presence()
pres.setStatus(str(localtime))
conn.send(pres)
time.sleep(1)
#-------------------ROTATE GOOGLE CHAT STATUS>>>..........................
#def updateStatus2(conn):
# Status= " My Gtalk Under Experiment ..Plz donot Ping now.. You may get Unexpected result.."
# while(1):
# pres = xmpp.Presence()
# pres.setStatus(Status)
# conn.send(pres)
# Status=Status[1:len(Status)]+Status[0:1]
# print Status
# time.sleep(.5)


def RECV_MSG(cl):
cl.RegisterHandler('message', messageCB)


#================================main program stat here===============
def main():
#userID = 'dutta.dipankar08@gmail.com'
userID = 'dutta.subhankar123@gmail.com'
password = 'xxxx'
ressource = 'Script'

jid = xmpp.protocol.JID(userID)
cl = xmpp.Client(jid.getDomain(), debug=[])

connection = cl.connect(('talk.google.com',5222))

if not connection:
sys.stderr.write('Could not connect\n')
else:
sys.stderr.write('Connected with %s\n' % connection)

auth = cl.auth(jid.getNode(), password, ressource)
if not auth:
sys.stderr.write("Could not authenticate\n")
else:
sys.stderr.write('Authenticate using %s\n' % auth)

cl.sendInitPresence(requestRoster=1)


# check connection
print cl.isConnected()

#send message--
#SendSMS(cl)

##reciving message.............
#RECV_MSG(cl)
cl.RegisterHandler('message', messageCB)


#---get presence CB........
cl.RegisterHandler('presence', presenceCB)


#------------change the status...----------DONE
try:
#thread.start_new_thread(updateStatus1,(cl,))
print 'hello'
except:
print " error is thread cration"

#--get contact list-----------------
#ros=cl.getRoster()
#for i in ros:
#print "a"

#run the main thread for ever...
GoOn(cl)

cl.disconnect()

main()

No comments:

Post a Comment