Following on from last week’s post - Couple of lines of code for Saving money. Well I enhanced it and this morning I got my text with the first amount to save.
I received my text message this morning, first amount is £34. You can see the code below. I use TxtLocal to send the text messages. This is a paid service, it isn’t much at all I use the text for many other things, like an automated birthday SMS, plus I can easily send all the current members of a club I run with just changing the message in the script.
Please see the code below, I have changed my details.
import urllib
import urllib2
from time import strftime
import random
lines = []
with open("/home/ec2-user/repo/python/money.txt") as file:
lines = [line.strip() for line in file]
for x in range(len(lines)):
print lines[x],
random_num = random.choice(lines)
print random_num
lines.remove(random_num)
with open('/home/ec2-user/repo/python/money.txt', 'w') as f:
for item in lines:
f.write("%s\n" % item)
TDATE = strftime("%d-%m")
SNUMBER = '11111111111'
MESSAGE = ('Amount to save is ' + random_num)
USERNAME = 'name@email.com'
SENDER = 'Auto Craig'
HASH = 'Put your hash here from TxtLocal Account'
NUMBERS = (SNUMBER)
# Set flag to 1 to simulate sending, this saves credits while testing your code. # To send real message set this flag to 0
TEST_FLAG = 0
#-----------------------------------
# No need to edit anything below this line
#-----------------------------------
VALUES = {'test' : TEST_FLAG,
'uname' : USERNAME,
'hash' : HASH,
'message' : MESSAGE,
'from' : SENDER,
'selectednums' : NUMBERS}
URL = 'http://www.txtlocal.com/sendsmspost.php'
POSTDATA = urllib.urlencode(VALUES)
REQ = urllib2.Request(URL, POSTDATA)
print 'Attempting to send SMS to ' + SNUMBER + ' on ' + TDATE
try:
RESPONSE = urllib2.urlopen(REQ)
RESPONSE_URL = RESPONSE.geturl()
if RESPONSE_URL == URL:
print 'SMS sent!'
except urllib2.URLError, e:
print 'Send failed!'
print e.reason
