From ba182c025c44771aff9ae9e0ce60cea78e79bb18 Mon Sep 17 00:00:00 2001 From: Mikkel Munch Mortensen <3xm@detfalskested.dk> Date: Thu, 2 Sep 2021 17:33:39 +0200 Subject: [PATCH] Update to work with Python 3 (only) Also bump the version to 0.2 --- README.txt | 2 +- cpsms/__init__.py | 3 + cpsms/cpsms.py | 187 +++++++++++++++++++++++++--------------------- examples.py | 26 ++++--- setup.py | 34 +++++---- 5 files changed, 136 insertions(+), 116 deletions(-) diff --git a/README.txt b/README.txt index b890a21..51ffb83 100644 --- a/README.txt +++ b/README.txt @@ -1,5 +1,5 @@ THIS IS -A Python wrapper around the SMS gateway from CPSMS . +A Python 3 wrapper around the SMS gateway from CPSMS . THIS IS NOT Some sort of magic, free SMS gateway. diff --git a/cpsms/__init__.py b/cpsms/__init__.py index e69de29..879b5e1 100644 --- a/cpsms/__init__.py +++ b/cpsms/__init__.py @@ -0,0 +1,3 @@ +"""The cpsms package.""" + +from .cpsms import Gateway diff --git a/cpsms/cpsms.py b/cpsms/cpsms.py index 0fda994..d278da3 100644 --- a/cpsms/cpsms.py +++ b/cpsms/cpsms.py @@ -1,8 +1,5 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*-' - """ -Copyright 2010, 2011 Mikkel Munch Mortensen <3xm@detfalskested.dk>. +Copyright 2010-2021 Mikkel Munch Mortensen <3xm@detfalskested.dk>. This file is part of SMS Gateway. @@ -20,89 +17,105 @@ You should have received a copy of the GNU General Public License along with SMS Gateway. If not, see . """ -import urllib, getopt +import getopt +from urllib.parse import urlencode +from urllib.request import urlopen +from typing import Any, Tuple -class Gateway(): - ''' - A Python wrapper around the SMS gateway from CPSMS . - Please look at the README for further description. - ''' - default_options = { - 'recipients' : [], - 'message' : '', # Should be a unicode string. - 'callback_url' : '', - 'timestamp' : '', # For delaying messages. Format: YYYYMMDDHHMM. - 'utf8' : 1, - 'flash' : 0, - 'group' : 0, - 'gateway_base_url' : 'https://www.cpsms.dk/sms/?' - } - options = {} - - def __init__(self, username, password, sender_name, options = None): - ''' - Initialize SMS gateway, with some options. - ''' - self.options = self.default_options - if options != None: - self.options.update(options) +class Gateway: + """ + A Python wrapper around the SMS gateway from CPSMS . - self.options['username'] = username - self.options['password'] = password - self.options['from'] = sender_name - - def add_recipient(self, number): - ''' - Add a number to the list of recipients. - ''' - self.options['recipients'].append(number) - - def send(self, message = None): - ''' - Send the message specified in self.options to all recipients. Optionally, override the message to be sent. - ''' - # Decide what to send - if message == None: - message = self.options['message'] - - # Raise error if message is empty. - if len(message) == 0: - raise ValueError('Message empty.') - - # Raise error if message is too long. - if len(message) > 459: - raise ValueError('Message not allowed to be more than 459 characters. Current message is %i characters.' % len(message)) - - # Raise error if recipients is empty. - if len(self.options['recipients']) == 0: - raise ValueError('No recipients.') - - # Construct gateway URL. - options = [ - ('username', self.options['username']), - ('password', self.options['password']), - ('message', message.encode('utf-8')), - ('from', self.options['from']), - ('utf8', self.options['utf8']), - ('flash', self.options['flash']), - ('group', self.options['group']), - ('url', self.options['callback_url']), - ] - - for r in self.options['recipients']: - options.append(('recipient[]', r)) - - if self.options['timestamp'] != '': - options.append(('timestamp', self.options['timestamp'])) - - url = self.options['gateway_base_url'] + urllib.urlencode(options) - - # Send SMS. - remote_call = urllib.urlopen(url) - result = remote_call.read() - remote_call.close() - if result.find('') > -1: - return True, result - else: - return False, result + Please look at the README for further description. + """ + + options: dict[str, Any] = {} + + @property + def default_options(self) -> dict[str, Any]: + return { + "recipients": [], + "message": "", + "callback_url": "", + "timestamp": "", # For delaying messages. Format: YYYYMMDDHHMM. + "utf8": True, + "flash": False, + "group": False, + "gateway_base_url": "https://www.cpsms.dk/sms/?", + } + + def __init__( + self, + username: str, + password: str, + sender_name: str, + options: dict[str, Any] = None, + ) -> None: + """Initialize SMS gateway, with some options.""" + self.options = self.default_options + if options is not None: + self.options.update(options) + + self.options["username"] = username + self.options["password"] = password + self.options["from"] = sender_name + + def add_recipient(self, number: str) -> None: + """Add a number to the list of recipients.""" + self.options["recipients"].append(number) + + def send(self, message: str = None) -> Tuple[bool, str]: + """ + Send the message specified in self.options to all recipients. + + Optionally, override the message to be sent. + """ + # Decide what to send. + if message is None: + message = self.options["message"] + + # Raise error if message is empty. + if message == "": + raise ValueError("Message empty.") + + # Raise error if message is too long. + if len(message) > 459: + raise ValueError( + "Message not allowed to be more than 459 characters." + "Current message is %i characters." % len(message) + ) + + # Raise error if recipients is empty. + if len(self.options["recipients"]) == 0: + raise ValueError("No recipients.") + + # Construct gateway URL. + options = [ + ("username", self.options["username"]), + ("password", self.options["password"]), + ("message", message), + ("from", self.options["from"]), + ("utf8", int(self.options["utf8"])), + ("flash", int(self.options["flash"])), + ("group", int(self.options["group"])), + ("url", self.options["callback_url"]), + ] + + for r in self.options["recipients"]: + options.append(("recipient[]", r)) + + if self.options["timestamp"] != "": + options.append(("timestamp", self.options["timestamp"])) + + url = self.options["gateway_base_url"] + urlencode(options) + print(url) + + # Send SMS. + remote_call = urlopen(url) + result = remote_call.read().decode() + remote_call.close() + if result.find("") > -1: + return True, result + else: + return False, result diff --git a/examples.py b/examples.py index 6694ab2..1e65dbd 100755 --- a/examples.py +++ b/examples.py @@ -1,8 +1,5 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*-' - """ -Copyright 2010, 2011 Mikkel Munch Mortensen <3xm@detfalskested.dk>. +Copyright 2010-2021 Mikkel Munch Mortensen <3xm@detfalskested.dk>. This file is part of SMS Gateway. @@ -23,13 +20,18 @@ along with SMS Gateway. If not, see . import cpsms # The easiest way to send a single message. -gateway1 = cpsms.Gateway('username', 'password', 'SMS Test') -gateway1.add_recipient('+4512345678') -print gateway1.send(u'One way of sending a massage.') +gateway1 = cpsms.Gateway("username", "password", "Sender Name") +gateway1.add_recipient("+4512345678") +print(gateway1.send("One way of sending a massage.")) # The easiest way to send a message to multiple recipients. -gateway2 = cpsms.Gateway('username', 'password', 'SMS Test', { - 'recipients' : ['+4512345678', '+4587654321'], - 'message' : u'Another way of sending a message.', -}) -print gateway2.send() +gateway2 = cpsms.Gateway( + "username", + "password", + "SMS Test", + { + "recipients": ["+4512345678", "+4587654321"], + "message": "Another way of sending a message.", + }, +) +print(gateway2.send()) diff --git a/setup.py b/setup.py index 0c670fc..97429e0 100644 --- a/setup.py +++ b/setup.py @@ -2,20 +2,22 @@ from distutils.core import setup setup( - name='cpsms', - packages=['cpsms'], - version='0.1', - author='Mikkel Munch Mortensen', - author_email='3xm@detfalskested.dk', - url='https://github.com/decibyte/cpsms', - description='A Python wrapper around the SMS gateway ' \ - 'from CPSMS .', - classifiers = [ - 'Programming Language :: Python', - 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', - 'Operating System :: OS Independent', - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'Topic :: Communications :: Telephony', - ] + name="cpsms", + packages=["cpsms"], + version="0.2", + author="Mikkel Munch Mortensen", + author_email="3xm@detfalskested.dk", + url="https://github.com/decibyte/cpsms", + description=( + "A Python wrapper around the SMS gateway from " + "CPSMS ." + ), + classifiers=[ + "Programming Language :: Python", + "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", + "Operating System :: OS Independent", + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Topic :: Communications :: Telephony", + ], )