Created
May 31, 2014 20:53
-
-
Save tochev/1bdee0e38a54306d7b93 to your computer and use it in GitHub Desktop.
A script to send SMS from the internet for the Bulgaria operators mtel and globul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# set encoding=utf-8 | |
# A script to send SMS from the internet for the Bulgaria operators mtel and globul | |
import argparse | |
import json | |
import mechanize | |
import os.path | |
CONFIG_FILENAME = os.path.expanduser('~/.send_sms.json') | |
OPERATORS = { | |
'mtel': ['35988'], | |
'globul': ['35989'], | |
} | |
def send_sms_mtel(recipient, message, auth, sender="-"): | |
assert len(message) <= 100, "Message too long for operator" | |
br = mechanize.Browser() | |
br.open('https://www.mtel.bg') | |
br.follow_link(text='Вход') | |
br.select_form(nr=1) | |
br['maccount'], br['mpassword'] = auth | |
#br.form.find_control(name='redirect_https') | |
br.form.controls[2]._value = 'https' | |
response = br.submit() | |
assert 'Изход' in response.get_data(), 'Unable to login' | |
br.follow_link(text='SMS център') | |
br.select_form(nr=1) | |
br['from'] = sender | |
br['msisdn'] = recipient | |
br['smstext'] = message | |
response = br.submit() | |
assert 'В обработка' in response.get_data(), 'Message might not be send' | |
# TODO: display the limit | |
def send_sms_globul(recipient, message, auth, sender=None): | |
assert len(message) <= 100, "Message too long for operator" | |
recipient = "0" + recipient[3:] # remove 359 | |
br = mechanize.Browser() | |
br.open('https://my.globul.bg') | |
br.follow_link(text_regex=u'Вход'.encode('cp1251')) | |
br.select_form(predicate=lambda form: 'password' in (c.name for c in form.controls)) | |
br['username'], br['password'] = auth | |
response = br.submit() | |
assert u'Последно влизане:'.encode('cp1251') in response.get_data(), 'Unable to login' | |
for next_action in [u'SMS Център', u'Изпращане на SMS']: | |
for link in br.links(): | |
if link.text.decode('cp1251').strip() == next_action: | |
br.open(link.absolute_url) | |
break | |
else: | |
assert next_action == False | |
br.select_form(predicate=lambda form: 'receiverPhoneNum' in (c.name for c in form.controls)) | |
br['receiverPhoneNum'] = recipient | |
br['txtareaMessage'] = message | |
response = br.submit() | |
assert u'Статус на съобщение'.encode('cp1251') in response.get_data(), 'Message might not be send' | |
# TODO: display the limit | |
def to_MSISDN(number, default_country_code='359'): | |
if not (4 < len(number) < 16): | |
raise ValueError("Invalid number `%s'" % number) | |
if number.startswith('00'): | |
return number[2:] | |
if number.startswith('0'): | |
return default_country_code + number[1:] | |
return number | |
def extract_operator(number): | |
for (operator, prefixes) in OPERATORS.items(): | |
for prefix in prefixes: | |
if number.startswith(prefix): | |
return operator | |
raise ValueError("Unable to determine the operator.") | |
def main(): | |
parser = argparse.ArgumentParser(description="Send sms using the internet") | |
parser.add_argument('-c', '--config', | |
default=CONFIG_FILENAME, | |
type=argparse.FileType('r'), | |
help="configuration file for auth") | |
parser.add_argument('recipient', | |
type=to_MSISDN, | |
help="MSISDN of the recipient") | |
parser.add_argument('message', type=str, help="message") | |
parser.add_argument('-s', '--sender', | |
default='-', | |
type=str, help="description of the sender") | |
parser.add_argument('-o', '--operator', | |
choices=['AUTO', 'globul', 'mtel'], | |
default='AUTO') | |
args = parser.parse_args() | |
recipient = args.recipient | |
operator = args.operator | |
if operator == 'AUTO': | |
operator = extract_operator(recipient) | |
credentials = json.load(args.config) | |
if operator not in credentials: | |
raise ValueError("Please add json dictionary to config {operator : [user, pass]}") | |
auth = credentials[operator] | |
({'mtel': send_sms_mtel, | |
'globul': send_sms_globul}[operator])( | |
recipient, | |
args.message, | |
auth=auth, | |
sender=args.sender) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment