#!/bin/python
# Author: Kristof Toth
# Description: Simple script to send push notification
import os
from pusher_push_notifications import PushNotifications
def send_notification(packages, count):
if count <= 1:
title = "{0} package can be upgraded!".format(count)
else:
title = "{0} packages can be upgraded!".format(count)
body = str(packages)
pn_client = PushNotifications(
instance_id="INSTANCE_ID",
secret_key="SECRET_KEY",
)
pn_client.publish(
interests=['bXIucm9ib3Q='],
publish_body={
'fcm': {
'notification': {
"title": title,
"body": body,
},
# additional data can be passed with the notification
# 'data': {
# "info": "info",
# },
},
},
)
def upgradable_packages():
# cut out the package names and skip the first line
output = os.popen("apt list --upgradable | cut -f1 -d '/' | awk '{if(NR>1)print}'")
packages = []
for line in output.readlines():
packages.append(line.strip())
return packages
if __name__ == '__main__':
send_notification(upgradable_packages(), len(upgradable_packages()))
print "[ * ] Notification sent!"