Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pseudo-projects/32b6985f81bba8bada97b6f9a3b2b4f7 to your computer and use it in GitHub Desktop.
Save pseudo-projects/32b6985f81bba8bada97b6f9a3b2b4f7 to your computer and use it in GitHub Desktop.
Get the total unread email message count summed from multiple Gmail accounts via the gmail api and display it on an OLED connected to your Raspberry Pi..
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
import re
import time
import datetime
# Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used
### BEGIN Editable Vars
GMAIL_QUERY_ARGS = "label:unread category:primary"
EMAIL_ADDRESS_TEXT_LINE = "youremail@gmail.com"
TEXT_TO_APPEND_TO_COUNT = "Unread Emails"
DELAY_TIME_TO_QUERY_API = 10 # seconds
# Add a start time / end time to blank out the screen to preserve it when not in use.
SCREENSAVER_TIME_BEGIN = "00:00:00" #24h
SCREENSAVER_TIME_END = "08:00:00" #24h
'''
Each object below represents a gmail account.
Add as many as you want to be summed together for the total email count.
'''
GMAIL_AUTH_FILES = [
{"cred":"credentials.json", "token":"token.pickle"}, # <--- Gmail Account Auth files
]
### END Editable Vars
# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Load default font.
#font = ImageFont.load_default()
font = ImageFont.truetype("fonts/Arial.ttf", 16)
font_email = ImageFont.truetype("fonts/Arial.ttf", 11)
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0
cmd = "hostname -I | cut -d\' \' -f1"
IP = subprocess.check_output(cmd, shell = True )
IP = re.search( r'[0-9]+(?:\.[0-9]+){3}', str(IP) ).group()
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
class GmailMessagesUnreadCount:
def __init__(self, cred, token):
self.cred = cred
self.token = token
def ListMessagesMatchingQuery(self, service, user_id, query=''):
"""List all Messages of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'from:user@some_domain.com' for Messages from a particular sender.
Returns:
List of Messages that match the criteria of the query. Note that the
returned list contains Message IDs, you must use get with the
appropriate ID to get the details of a Message.
"""
try:
response = service.users().messages().list(userId=user_id,
q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query,
pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except:
print('An error occurred:')
def get_count(self):
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(self.token):
with open(self.token, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
self.cred, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
email_count = len(self.ListMessagesMatchingQuery(service, "me", GMAIL_QUERY_ARGS))
print("Count:", email_count)
return email_count
if __name__ == '__main__':
while True:
timeBegin = time.time()
current_time = datetime.datetime.now()
t1_begin = datetime.datetime.now()
t2_end = datetime.datetime.now()
t1_begin_list = SCREENSAVER_TIME_BEGIN.split(":")
t2_begin_list = SCREENSAVER_TIME_END.split(":")
t1_begin = t1_begin.replace(hour=int(t1_begin_list[0]), minute=int(t1_begin_list[1]), second=int(t1_begin_list[2]))
t2_end = t2_end.replace(hour=int(t2_begin_list[0]), minute=int(t2_begin_list[1]), second=int(t2_begin_list[2]))
time_delta = t2_end - t1_begin
if(time_delta.total_seconds() < 0):
t2_end_adjusted = t2_end + datetime.timedelta(days=+1)
else:
t2_end_adjusted = time_delta + t1_begin
if(current_time <= t1_begin or current_time >= t2_end_adjusted):
email_count = 0
for gmail_auth in GMAIL_AUTH_FILES:
email_count += GmailMessagesUnreadCount(gmail_auth["cred"], gmail_auth["token"]).get_count()
print("Total email count:", email_count)
# If the email unread messages count is 1 and the string ends with an "s",
# ..."pop" the "s" off the end of the string.
text_modified = TEXT_TO_APPEND_TO_COUNT
if (int(email_count) is 1 and TEXT_TO_APPEND_TO_COUNT.endswith('s')):
text_modified = TEXT_TO_APPEND_TO_COUNT[:-1]
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((x, top), str(email_count)+" "+text_modified, font=font, fill=255)
draw.text((x, top+38), EMAIL_ADDRESS_TEXT_LINE, font=font_email, fill=255)
draw.text((x, top+50), "IP: " + str(IP), font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
else:
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Display image.
disp.image(image)
disp.display()
print("Screensaver Mode - sleeping.")
timeEnd = time.time()
timeElapsed = timeEnd - timeBegin
time.sleep(DELAY_TIME_TO_QUERY_API-timeElapsed)
@pseudo-projects
Copy link
Author

Update the SCREENSAVER_TIME_BEGIN and SCREENSAVER_TIME_END values (make sure your pi is setup with the right timezone).

Next, update the GMAIL_AUTH_FILES values to point to the corresponding auth files for each account: {"cred":"credentials.json", "token":"token.pickle"}, {"cred":"credentials2.json", "token":"token2.pickle"}, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment