Personal Notification Hub

Dhruv Pandey
Apr 12 · 6 min read

A long time back one of my friends asked me to check this cool idea which was simply a small display device that “keeps you updated for anything important for you”. Somehow the idea did not succeed in raising funds and eventually scraped out.

Just for reference purposes. No intention to create the exact device. This image is completely part of https://smart-stickers.com/. The red part is what we are trying to achieve through this article

The idea

Currently available options in the market

Implementation

  1. To begin with, you would need Chromedriver for Windows that can be downloaded from here. Any version will work.
  2. Import necessary packages: selenium for scraping purpose, threading for creating parallel threads for each social media platform, and flask for creating the final microservice.
from selenium import webdriver
import time
import threading
from flask import Flask, request, jsonify, make_response

3. Create the driver instance for each social media platform

fbDriver = webdriver.Chrome("chromedriver")
fbDriver.get("https://www.facebook.com/")
waDriver = webdriver.Chrome("chromedriver")
waDriver.get("https://web.whatsapp.com/")
ytDriver = webdriver.Chrome("chromedriver")
ytDriver.get("https://www.youtube.com/")

4. Define global variables that hold the counts for each SoMe(Social media) platform. Why global? So that the scraping(setter of the variable) and Flask-microservice(getter of the variable for the end-user) use the same variable and there is no discrepancy in values.

fbNotifications=0   #for facebook related notification
msgNotifications=0 #for messenger related notification
waNotifications=0 #for whatsapp related notification
ytNotifications=0 # for youtube related notiifcation

5. Create a handler function for each social media platform where the real scraping stuff happens. Update the global variable inside the function. Here is an example of a scraping function for Facebook

def fbCountManager():
global fbNotifications
global msgNotifications
while True:
try:
fbNotifications=fbDriver.find_element_by_id('notificationsCountValue')
msgNotifications=fbDriver.find_element_by_id('mercurymessagesCountValue')
except:
fbNotifications=0
msgNotifications=0

fbDriver.refresh()
time.sleep(20)

What we want is the values of these 2 badges from UI(the first badge gives notification count for messenger and the second one is for Facebook notification). I would not be discussing how I identified the element id’s but you can go through Selenium syntaxes and tutorials to check more about that.

Here is a similar function for whatsapp

def waCountManager():
global waNotifications
while True:
try:
waNotifications=0
allMsgs=waDriver.find_elements_by_css_selector('.OUeyt')

for msg in allMsgs:
waNotifications=waNotifications+int(msg.text)

except:

waNotifications=0

waDriver.refresh()
time.sleep(20)

Note that we are not storing any credentials for any platform in our python code because of security concerns. So you would have to perform login to each platform manually one time. Though this login process can also be automated through Selenium but I recommend not to compromise your credentials in a plain script file.

6. You can similarly create scraping functions for each social media platform. Next, you have to create and start parallel threads for each platform. This is how you do it. The target parameter takes in the function that this thread should be calling.

fbThread = threading.Thread(target=fbCountManager)
fbThread.start()

waThread = threading.Thread(target=waCountManager)
waThread.start()

ytThread = threading.Thread(target=ytCountManager)
ytThread.start()

7. The final part is creating a Flask-based microservice so that the end-user(which is YOU) hits the service and gets the accumulated response containing counts for each social media platform. I really like the concept of microservice(the user doesn't care how that data is getting fetched, he just hits the service and gets the results. he is happy)

app=Flask(__name__)

@app.route('/getcounts/', methods=['GET'])
def respond():
response_body = {
"fbNotifications":fbNotifications,
"msgNotifications": msgNotifications,
"waNotifications": waNotifications,
"ytNotifications": ytNotifications
}

res = make_response(jsonify(response_body), 200)
return res


@app.route('/')
def index():
getCoronaCounts()
return '<h1>Notiifcation App</h1>'

if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port=5000)

Notice how we are using the global variables that we had earlier defined. I know I could have done better here and added some security or some more fancy features. But the goal was to create a quick solution. But now that you are aware of the idea, you always have scope for improvement.

8. Everything is in place, let's try to test what is response this microservice is returning. The service is running on port 5000 which we defined in the last line.

Response from the Flask based endpoint

What next

  1. Try to deploy it on Raspberry Pi so that you are not relying on your personal laptop the entire day to run this.
  2. Create the notification count service as public using ngrok, so that you can access the counts outside your home network.
  3. Use it in your smart mirror or background wall displays.
Image from DIYPerks. Check how to create a smart mirror

Terms of Usage

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dhruv Pandey

Written by

A machine learning and computer vision enthusiast working as a web developer in Finland.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Dhruv Pandey

Written by

A machine learning and computer vision enthusiast working as a web developer in Finland.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store