3

I'm writing a simple little script to send me a text message when the Ultra Music Festival early bird tickets go on sale so I can snatch them up. When I came to writing this I figured python would be a quick way to achieve my goal. What I do is collect the links and then count them and determine if there is a change and send a google voice text message to a couple numbers. Here is my code ran against stackoverflow.

from googlevoice import Voice
from googlevoice.util import input
from bs4 import BeautifulSoup, SoupStrainer
from time import sleep
import urllib2
from array import *

#define login details
email = 'example@gmail.com'
password = 'password'
url = 'http://stackoverflow.com/questions'

def send_message(var_text):
    voice = Voice()
    voice.login(email, password)

    phoneNumber = array('L',[9998675309, 9998675309])   
    for i in phoneNumber:
        voice.send_sms(i, var_text)

#init
soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a'))

link_count = len(soup)

#start the loop
var = 1
while var == 1 :  # This constructs an infinite loop
    soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a'))
    if link_count != len(soup): 
        string = str('Link Count Changed\n\nSite:\n' + url + '\nPrev:\n' + str(link_count) + '\nNew:\n' + str(len(soup)))
        send_message(string)
        print (string)
        link_count = len(soup)
        sleep(10)
        pass
    else:
        print('Number of links ('+ str(link_count) + ') has not changed, going to sleep now.') 
        sleep(10)
        pass
print "Good bye!"

Here is the error I keep getting (only seems to happen when sending to more then one number)

doesn't work array('L',[9998675309, 9998675309])
works array('L',[9998675309])

ERROR:

bash-3.2# python gvsendalert.py 
Number of links (195) has not changed, going to sleep now.
Traceback (most recent call last):
  File "gvsendalert.py", line 32, in <module>
    send_message(string)
  File "gvsendalert.py", line 19, in send_message
    voice.send_sms(i, var_text)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/voice.py", line 151, in send_sms
    self.__validate_special_page('sms', {'phoneNumber': phoneNumber, 'text': text})
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/voice.py", line 225, in __validate_special_page
    load_and_validate(self.__do_special_page(page, data))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/util.py", line 65, in load_and_validate
    validate_response(loads(response.read()))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/util.py", line 59, in validate_response
    raise ValidationError('There was a problem with GV: %s' % response)
googlevoice.util.ValidationError: There was a problem with GV: {u'data': {u'code': 58}, u'ok': False}

Ok I've taken into consideration what some of you have posted and come out with this. For the number array sending my google voice number twice it will send 2 messages. If I put my friends number as the second it breaks it. Could this be because my friends number is not a google voice number? I have been able to send messages to this number using Google Voice and some other 3rd party iPhone applications so I would think the python module would work the same way.

Here is my 2nd Revision Code:

def send_message(var_text):
    voice = Voice()
    voice.login(email, password)

    phoneNumber = ['myrealgooglenumber', 'myfriendsactualphonenumber']  
    for i in phoneNumber:
        print( str('sending to: ') + str(i))
        voice.send_sms(str(i), str(var_text))
        sleep(5)

#init
soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a'))

link_count = len(soup)

#start the loop
var = 1
while var == 1 :  # This constructs an infinite loop
    soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a'))
    if link_count != len(soup): 
        string = ('Link Count Changed\n\nSite:\n{0}\nPrev:\n{1}\nNew:\n{2}').format(url, link_count, len(soup))     
        send_message(string)        
        link_count = len(soup)
        print (string)
        sleep(10)
        pass
    else:
        string = ('Number of links ({0}) has not changed, going to sleep now.').format(str(link_count))
        print(string) 
        sleep(10)
        pass
print "Good bye!"

Have tested with 2 google voice numbers and it works. Still doesn't work with non google voice numbers.

atrueresistance
  • 1,358
  • 5
  • 26
  • 48

2 Answers2

0

Google may have a timer to prevent you sending too many SMS messages back to back.

Perhaps you could try changing your loop to something like:

for i in phoneNumber:
    voice.send_sms(i, var_text)
    sleep(5)

One other thought, does it work better if you use 2 different phone numbers?

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
0

It looks like you're using ints for the phone numbers.

Phone numbers are not true numbers.

Try strings instead:

phoneNumber = ['9998675309', '9998675309']

Also, on a style note, have a look at string formatting:

string = 'Link Count Changed\n\nSite:\n{0}\nPrev:\n{1}\nNew:\n{2}').format(url, link_count, len(soup))
MRAB
  • 20,356
  • 6
  • 40
  • 33