Facebookで新しい通知を受け取り、「m」をシリアルポート
(PCに接続されたarduino) に送信するPythonコードに取り組んでいます
Python モニターでは、Facebook にログインするためのメールアドレスとパスワードを入力できます
。まだ新しい通知がない場合は、「Zzz...」が表示されます。新着通知があれば「Facebookに新着通知、旗揚げ中…」とモニターに表示されます。
facebook にログインして 'Zzz...' を印刷する場合、問題なく動作します。しかし、問題は、Facebook に新しい通知があったとしても、画面にコードの違いが表示されないことです。
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から行われました。