📜 ⬆️ ⬇️

Skype + Python = Skype4Py

In this article I would like to tell you about my acquaintance with the wonderful extension for Skype API - Skype4Py. Using it, you can actually connect to Skype and, having written a couple of lines of code, to accomplish what the developers did not.

About two months ago I found a project called moc-tray on google code. He was a program written in gtk2-perl, the meaning of which is to hang in the tray and provide a context menu for accessing the main functionality of the excellent console player mocp . I decided to indulge and make the display of the current song as a status in Skype.
No sooner said than done.
The string in the barley program to run in the background of the Python script status master is done. Oh, and the script itself:

 #!/usr/bin/env python # Skymoc. # # Python script to show current song title in skype status import time import commands import Skype4Py #  ,       # ASCII decoding error: ordinal not in range(128) #   -    en_US.UTF-8 reload(sys) sys.setdefaultencoding('utf-8') # Callback     def OnAttach(status): print "API Attachment status: %s" % skype.Convert.AttachmentStatusToText(status) attached = False #   if status == Skype4Py.apiAttachAvailable: while not attached: try: #     Public API allowed programs -  skype.Attach() attached = True #   - ,  ,     except: pass # Callback    def OnUserStatus(status): print "Current status: %s" % status skype = Skype4Py.Skype() #   skype.OnAttachmentStatus = OnAttach skype.OnUserStatus = OnUserStatus print 'Connecting to skype..' #  skype.Attach(Wait=False) profile = skype.CurrentUserProfile mocpSongTitle = '' while True: #  5  ,     time.sleep(5) (stat, currTitle) = commands.getstatusoutput('mocp -Q %title') if currTitle != mocpSongTitle: if currTitle != '': mocpSongTitle = currTitle profile.MoodText = mocpSongTitle else: profile.MoodText = ':-)' 

')
As you can see, it's pretty simple. Once it took a bit more. There was football, I didn’t want to watch the match at home, and at the same time I had to receive an important chat message. Since the Skype client has not yet been invented for my phone, it was decided to send a message by mail (there is a benefit on the phone E-mail client). Everything was decided in 20 minutes by installing a callback on the incoming message, in which the sender and the text are sent to me by mail:

 #   def OnNotify(var): print "OnNotify: %s" % var def OnMessageStatus(chat, status): # ? ! if status == 'RECEIVED': # DynDns.org, port forwarding -     ! prompt = os.popen('echo "%s" | mail -s "%s" %s' % (chat.Body, "%s via Skype" % chat.Sender.FullName, 'example@gmail.com'), 'w') sent = prompt.close() print "Sent status: %s" % sent skype = Skype4Py.Skype() skype.OnAttachmentStatus = OnAttach skype.OnNotify = OnNotify # Callback    skype.OnMessageStatus = OnMessageStatus 


Well, I launched two Skype clients, wrote from one to another - and a message in the mail. True, that evening, so I did not wait for that message. But the next day they wrote me an email :) I hope this letter will now change my life.

Skype4Py documentation: http://skype4py.sourceforge.net/doc/html/

Good luck!

Source: https://habr.com/ru/post/98735/


All Articles