2012-09-09 97 views
2

我正在寫一個簡單的小腳本,當超音樂節早期鳥票發售時給我發短信,所以我可以搶奪他們。當我開始寫這篇文章時,我認爲python將是實現我的目標的一種快速方法。我所做的是收集鏈接,然後對它們進行計數,並確定是否發生變化並將Google語音短信發送給幾個號碼。這是我的代碼針對stackoverflow運行。蟒蛇noob有麻煩發送谷歌語音文本

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 = '[email protected]' 
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!" 

這裏是我不斷收到錯誤(似乎只發送給一個以上的號碼時發生)

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

錯誤:

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} 

好,我考慮你們有些人發佈了什麼,並提出了這個問題。對於號碼數組發送我的谷歌語音號碼兩次它會發送2條消息。如果我把我的朋友編號作爲第二個,它會打破它。這可能是因爲我的朋友號碼不是谷歌的語音號碼?我已經能夠使用谷歌語音和一些其他第三方iPhone應用程序發送消息到這個號碼,所以我會認爲Python模塊將以同樣的方式工作。

這是我第二次修正代碼:

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!" 

有一個2谷歌語音號碼測試,它的工作原理。仍然不適用於非谷歌語音號碼。

回答

0

谷歌可能有一個計時器,以防止你發送太多的短信息背對背。

也許你可以試着改變你的循環,以這樣的:

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

另外一個想法,它的工作更好,如果你使用2個不同的電話號碼?

+0

只與我的谷歌語音號碼,工作兩次,第二次修改代碼,但是當我把我的朋友放進去時會拋出錯誤。 – atrueresistance

0

它看起來像你使用整數的電話號碼。

電話號碼是而不是的真實數字。

嘗試字符串代替:

phoneNumber = ['9998675309', '9998675309'] 

而且,在風格筆記,看看字符串格式化:

string = 'Link Count Changed\n\nSite:\n{0}\nPrev:\n{1}\nNew:\n{2}').format(url, link_count, len(soup)) 
+0

我不知道你可以像這樣格式化字符串。感謝您的建議。閱讀起來乾淨得多。 – atrueresistance