diff options
Diffstat (limited to 'examples/set_avatar.py')
-rwxr-xr-x | examples/set_avatar.py | 154 |
1 files changed, 61 insertions, 93 deletions
diff --git a/examples/set_avatar.py b/examples/set_avatar.py index 08e0b664..f9641b1e 100755 --- a/examples/set_avatar.py +++ b/examples/set_avatar.py @@ -1,50 +1,39 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ - SleekXMPP: The Sleek XMPP Library + Slixmpp: The Slick XMPP Library Copyright (C) 2012 Nathanael C. Fritz - This file is part of SleekXMPP. + This file is part of Slixmpp. See the file LICENSE for copying permission. """ import os -import sys import imghdr import logging -import getpass +from getpass import getpass import threading -from optparse import OptionParser +from argparse import ArgumentParser -import sleekxmpp -from sleekxmpp.exceptions import XMPPError +import slixmpp +from slixmpp.exceptions import XMPPError +from slixmpp import asyncio - -# Python versions before 3.0 do not use UTF-8 encoding -# by default. To ensure that Unicode is handled properly -# throughout SleekXMPP, we will set the default encoding -# ourselves to UTF-8. -if sys.version_info < (3, 0): - from sleekxmpp.util.misc_ops import setdefaultencoding - setdefaultencoding('utf8') -else: - raw_input = input - - -class AvatarSetter(sleekxmpp.ClientXMPP): +class AvatarSetter(slixmpp.ClientXMPP): """ A basic script for downloading the avatars for a user's contacts. """ def __init__(self, jid, password, filepath): - sleekxmpp.ClientXMPP.__init__(self, jid, password) + slixmpp.ClientXMPP.__init__(self, jid, password) - self.add_event_handler("session_start", self.start, threaded=True) + self.add_event_handler("session_start", self.start) self.filepath = filepath + @asyncio.coroutine def start(self, event): """ Process the session_start event. @@ -77,32 +66,31 @@ class AvatarSetter(sleekxmpp.ClientXMPP): avatar_file.close() used_xep84 = False - try: - print('Publish XEP-0084 avatar data') - self['xep_0084'].publish_avatar(avatar) - used_xep84 = True - except XMPPError: + + print('Publish XEP-0084 avatar data') + result = yield from self['xep_0084'].publish_avatar(avatar) + if isinstance(result, XMPPError): print('Could not publish XEP-0084 avatar') + else: + used_xep84 = True - try: - print('Update vCard with avatar') - self['xep_0153'].set_avatar(avatar=avatar, mtype=avatar_type) - except XMPPError: + print('Update vCard with avatar') + result = yield from self['xep_0153'].set_avatar(avatar=avatar, mtype=avatar_type) + if isinstance(result, XMPPError): print('Could not set vCard avatar') if used_xep84: - try: - print('Advertise XEP-0084 avatar metadata') - self['xep_0084'].publish_avatar_metadata([ - {'id': avatar_id, - 'type': avatar_type, - 'bytes': avatar_bytes} - # We could advertise multiple avatars to provide - # options in image type, source (HTTP vs pubsub), - # size, etc. - # {'id': ....} - ]) - except XMPPError: + print('Advertise XEP-0084 avatar metadata') + result = yield from self['xep_0084'].publish_avatar_metadata([ + {'id': avatar_id, + 'type': avatar_type, + 'bytes': avatar_bytes} + # We could advertise multiple avatars to provide + # options in image type, source (HTTP vs pubsub), + # size, etc. + # {'id': ....} + ]) + if isinstance(result, XMPPError): print('Could not publish XEP-0084 metadata') print('Wait for presence updates to propagate...') @@ -111,64 +99,44 @@ class AvatarSetter(sleekxmpp.ClientXMPP): if __name__ == '__main__': # Setup the command line arguments. - optp = OptionParser() - optp.add_option('-q','--quiet', help='set logging to ERROR', - action='store_const', - dest='loglevel', - const=logging.ERROR, - default=logging.ERROR) - optp.add_option('-d','--debug', help='set logging to DEBUG', - action='store_const', - dest='loglevel', - const=logging.DEBUG, - default=logging.ERROR) - optp.add_option('-v','--verbose', help='set logging to COMM', - action='store_const', - dest='loglevel', - const=5, - default=logging.ERROR) + parser = ArgumentParser() + parser.add_argument("-q","--quiet", help="set logging to ERROR", + action="store_const", + dest="loglevel", + const=logging.ERROR, + default=logging.ERROR) + parser.add_argument("-d","--debug", help="set logging to DEBUG", + action="store_const", + dest="loglevel", + const=logging.DEBUG, + default=logging.ERROR) # JID and password options. - optp.add_option("-j", "--jid", dest="jid", - help="JID to use") - optp.add_option("-p", "--password", dest="password", - help="password to use") - optp.add_option("-f", "--file", dest="filepath", - help="path to the avatar file") - opts,args = optp.parse_args() + parser.add_argument("-j", "--jid", dest="jid", + help="JID to use") + parser.add_argument("-p", "--password", dest="password", + help="password to use") + parser.add_argument("-f", "--file", dest="filepath", + help="path to the avatar file") + + args = parser.parse_args() # Setup logging. - logging.basicConfig(level=opts.loglevel, + logging.basicConfig(level=args.loglevel, format='%(levelname)-8s %(message)s') - if opts.jid is None: - opts.jid = raw_input("Username: ") - if opts.password is None: - opts.password = getpass.getpass("Password: ") - if opts.filepath is None: - opts.filepath = raw_input("Avatar file location: ") + if args.jid is None: + args.jid = input("Username: ") + if args.password is None: + args.password = getpass("Password: ") + if args.filepath is None: + args.filepath = input("Avatar file location: ") - xmpp = AvatarSetter(opts.jid, opts.password, opts.filepath) + xmpp = AvatarSetter(args.jid, args.password, args.filepath) xmpp.register_plugin('xep_0054') xmpp.register_plugin('xep_0153') xmpp.register_plugin('xep_0084') - # If you are working with an OpenFire server, you may need - # to adjust the SSL version used: - # xmpp.ssl_version = ssl.PROTOCOL_SSLv3 - - # If you want to verify the SSL certificates offered by a server: - # xmpp.ca_certs = "path/to/ca/cert" - # Connect to the XMPP server and start processing XMPP stanzas. - if xmpp.connect(): - # If you do not have the dnspython library installed, you will need - # to manually specify the name of the server if it does not match - # the one in the JID. For example, to use Google Talk you would - # need to use: - # - # if xmpp.connect(('talk.google.com', 5222)): - # ... - xmpp.process(block=True) - else: - print("Unable to connect.") + xmpp.connect() + xmpp.process() |