我的python代码无法读取新通知

时间:2012-12-13 04:08:51

标签: python facebook

我正在处理一个python代码,它在facebook上接收新通知并将“m”发送到串口
(这是与PC连接的arduino)

在python监视器上,您可以编写电子邮件和密码登录到您的facebook 如果还没有新通知,则会显示“Zzz ...”。如果有新通知,显示器上会显示“Facebook上的新通知,提升标志......”。

登录Facebook并打印'Zzz ...'时效果很好。但问题是,即使我的脸书上有新的通知,代码也没有在屏幕上显示任何差异。

import mechanize
import cookielib
import serial
import sys
import time
import warnings
'VERSION 2.3 Unlike version 2.0, in this implementation: Instead of counting starting from 2 HTML tags <div class="c"> default, and'
'possible clikkare the link: clik on Clear Notifications (will not be '
'just a <div class="c">) and start counting for> 1'
'In this way, we avoid counting invitations to events or warnings as if they were birthday notifications.'
warnings.filterwarnings("ignore") 
# hiding any warning 
SERIALPORT = raw_input("Enter the serial port to which' the connected device:") 

# Set up serial port 
try:
    ser = serial.Serial(SERIALPORT, 9600)
    ser.timeout = None
except serial.SerialException:
    print "No device connected. exiting..."
    time.sleep(4)
    sys.exit()

# Alternatively you can 'go on facebook. com / notifications.php and lean to feed (using then the python library: feedparser). 
# Browser 
br = mechanize.Browser()

# Cookie Jar 
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj) 

# Browser options
br.set_handle_equiv(True)
#br.set_handle_grip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False) 

# Follows refresh 0 but not hangs on refresh> 0 
# br.set_handle_refresh (mechanize._http.HTTPRefreshProcessor (), max_time = 1) 



# User-Agent (this is cheating, ok?) 
br.addheaders = [('User-agent' , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')] 

# Open some site, let's pick a random one, the first That pops into mind: 
r = br.open('http://m.facebook.com') 
#html= r.read() 

# Show the source 
#print html
# or  
#print br. response (). read () 

# Show the html title 
print "- Connected to:" + br.title ()

# Select the first (index zero) form 
br.select_form(nr = 0) 

try: 
    wait = input("\ n Every how many seconds you want to check for new notifications? ") 
    while(wait>=5):
        wait = input(" \ n - E 'recommended a value below 5 seconds. \ n Re-Enter a value: ") 
except: 
    print "It 's necessary to enter a numeric value, not a string. \ n Closing the program ..."
    time.sleep(5) 
    sys.exit() 

print "\ n - Enter your login for Facebook: \ n "
# Let's search 
br.form['email'] = '% s'% raw_input(" Enter Email ") 
br.form['pass'] = '% s'% raw_input(" Enter Password: ") 

br.submit()
x = br.response().read() 
z = x.count("Combination e-mail/password incorrect.") 

while z: 
    print "Unable to login, username or password incorrect. Retry: \ n "
    go = False
    br. select_form (nr = 0) 
    br. form ['email'] = '% s'% raw_input (" Enter Email ") 
    br. form ['pass'] = '% s'% raw_input ("Enter Password:") 
    br. submit () 
    x = br. response().read () 
    z = x. count("Combination e-mail/password incorrect.")
else: 
    # we are fully logged.  (Z = 0)
    go = True 
    status = False #status flag, lowered = False 
    try:
        br.Follow_link(text = 'Delete notifications', nr = 0) # Clear previous notifications 
    except: 
        print "\ n No Notification! \ n "
    print " \ n - Mark as read, all previous notifications ... \ n "
    ser.write(" n ") # report the servant to the starting position. 

while True and go: 
    try: 
      # br.reload ( ) 
      # print br.response (). read () 
      x = br.open ('http://m.facebook.com/home.php?refid=0').read () 
      i = x.count ('< div class = "c"> ') # This field HTML is repeated more' than one time if there 'a new notification (before but' are read all notifications such events etc..). 
    except: 
        print "Delay in charge the page, wait 3 seconds applied .. \ n "
        time.sleep(3) 
    if i>= 1: 
        notification = True # Notifications to read 
    else:
        i = 0 #No notification 
        notification = False 

# to read control: 
    if notification == True: 
        if status == False: # If the flag and the 'down 
            ser.write("m") 
            print "New Notification on Facebook, hoisting flag ..." 
            status = True 
        else: 
            print "There are still notifications view ... "

    elif status == False: 
        print " Zzz ... " #flag already 'down, no notification to view 

    else: 
        ser.write("n") 
        print "No new notification flag down ... "
        status = False 

    time.sleep(wait) # Wait before checking for new notifications 

#END 

我认为从Facebook接收数据存在一些问题,来自代码:

while True and go: 
    try: 
      # br.reload ( ) 
      # print br.response (). read () 
      x = br.open ('http://m.facebook.com/home.php?refid=0').read () 
      i = x.count ('< div class = "c"> ') # This field HTML is repeated more' than one time if there 'a new notification (before but' are read all notifications such events etc..). 
    except: 
        print "Delay in charge the page, wait 3 seconds applied .. \ n "
        time.sleep(3) 
    if i>= 1: 
        notification = True # Notifications to read 
    else:
        i = 0 #No notification 
        notification = False 
你怎么看?编码是从python 2.7完成的。

0 个答案:

没有答案