Mail Allert

Questo è il codice contenuto in uno dei tutorial del libro Python per Raspberry edito da Amazon KDP. L’utilizzo di questo codice è permesso solo ai possessori del libro Python per Raspberry. Si declina ogni responsabilità dall’uso proprio o improprio di tale codice. Tutti i diritti riservati.

#!/usr/bin/enx python
import RPi.GPIO as GPIO, feedparser, time, os.path

GPIO.setwarnings(False)
USERNAME = "corroborante@gmail.com"
PASSWORD = "chiccoiden"
GPIO.setmode(GPIO.BCM)
LIGHTS = 25
GPIO.setup(LIGHTS, GPIO.OUT)
while True:
cur_mails = feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/gmail/feed/atom")
unread_count = int(cur_mails["feed"]["fullcount"])

print("You Have: ")
print(unread_count)
print(" emails in your inbox.")
if os.path.isfile("emails.txt") == False:
f = open('emails.txt', 'w')
f.write('0');
f.close
f = open('emails.txt','r')
last_mails = int(f.read())
f.close()
print ("Last know number of mail: ")
print (last_mails)
if unread_count < last_mails:
last_mails = unread_count
f = open('email.txt', 'w')
f.write(str(last_mails))
if unread_count > last_mails:
last_mails = unread_count
f = open('emails.txt','w')
f.write(str(last_mails))
GPIO.output(LIGHTS, True)
time.sleep(0.4)
GPIO.output(LIGHTS, False)
time.sleep(0.4)
GPIO.output(LIGHTS, True)
time.sleep(0.4)
GPIO.output(LIGHTS, False)
f.close()
time.sleep(60)