Created
August 3, 2014 06:24
-
-
Save debanshu/ad7b6556912ddbd12445 to your computer and use it in GitHub Desktop.
PU - XMPP Notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| ''' | |
| NOTES: | |
| ----- | |
| This notifier can be used to alert latest changes on the PU wesite , | |
| udated at certain specified intervals. | |
| The alerts can be sent from any xmpp account to any other xmpp account. | |
| Currently gmail/gtalk/hangouts are ALSO supported (though they no longer have xmpp support). | |
| TO RUN: | |
| ------ | |
| You need to install Python 2.7.x along with the following dependencies to run this file. | |
| --xmpp/xmppy | |
| --bs4 (beasutifulsoup4) | |
| Also an additional file named 'last_read' needs to be present in the directory this python file is run in. | |
| This file should contain a single number indicating the last notice serial number read on the pu site. | |
| Put it to 0 if you want to be notified from the beginning notices also, | |
| else pu the notice number of the last one you have already read, so that you are notified of the latest ones only. | |
| ''' | |
| __author__ = "Debanshu Sinha" | |
| __email__ = "[email protected]" | |
| import sys, smtplib, xmpp, urllib, urllib2, cookielib, threading | |
| from bs4 import BeautifulSoup | |
| from datetime import datetime, date, time | |
| #connection methods | |
| #create a xmpp connection | |
| def connectXMPP(): | |
| #add user username & pasword of account from which to send message | |
| userID = '*********@pilani.bits-pilani.ac.in' | |
| password = '******' | |
| # NOTE:if using 2-step verification in google, generate 'app-specific' password | |
| jid = xmpp.protocol.JID(userID) | |
| jabber = xmpp.Client(jid.getDomain(), debug=[]) | |
| connection = jabber.connect(('talk.google.com',5222)) | |
| if not connection: | |
| sys.stderr.write('Could not connect\n') | |
| else: | |
| sys.stderr.write('Connected with %s\n' % connection) | |
| auth = jabber.auth(jid.getNode(), password) | |
| if not auth: | |
| sys.stderr.write("Could not authenticate\n") | |
| else: | |
| sys.stderr.write('Authenticate using %s\n' % auth) | |
| #jabber.RegisterHandler('message', message_handler) | |
| jabber.sendInitPresence(requestRoster=1) | |
| return jabber | |
| #define some global variables | |
| #set update interval | |
| interval = 180 | |
| #get the notices table | |
| def getTable(): | |
| #pu username/password | |
| username = '********' | |
| password = '********' | |
| #login and get notices page from pu portal | |
| cj = cookielib.CookieJar() | |
| opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
| login_data = urllib.urlencode({'username' : username, 'password' : password}) | |
| opener.open('http://pu/index.php?option=login', login_data) | |
| resp = opener.open('http://pu/index.php?option=notices') | |
| #parse page html | |
| notices = BeautifulSoup(resp.read()) | |
| #return relevant table data as list | |
| return notices.find_all('tr')[1:] | |
| #get the log of last notice read | |
| def getLastNoticeRead(): | |
| return int(open("last_read","rb").read()) | |
| #set the log of last notice read | |
| def setLastNoticeRead(n): | |
| f = open("last_read","wb") | |
| f.write(str(n)) | |
| f.close() | |
| #create the updated message | |
| def createMessage(): | |
| lastNotice = getLastNoticeRead() | |
| noticeTable = getTable() | |
| msg = [] | |
| for tr in noticeTable: | |
| td = tr.find_all('td') | |
| if int(td[0].string) <= lastNotice: | |
| break | |
| msg.append(td[0].string) | |
| msg.append(td[1].string) | |
| msg.append(td[2].string) | |
| msg.append(td[3].string) | |
| msg.append(td[5].string) | |
| msg.append("------------------") | |
| newNotice = int(noticeTable[0].td.string) | |
| #update last message read | |
| if newNotice > lastNotice: | |
| setLastNoticeRead(newNotice) | |
| return "\n".join(msg) | |
| #message handler if we recieve a request | |
| def message_handler(connect_object, message_node): | |
| message = message_node.getBody() | |
| if message: | |
| print message | |
| #send updated message | |
| def sendMessage(conn,msg): | |
| #add the username to send mesage to | |
| #make sure the username is in the contacts of the username you are sending message from | |
| receiver = "*********@gmail.com" | |
| #send message - works for xmpp/jabber/gtalk/hangouts | |
| conn.send(xmpp.Message(receiver,msg,typ='chat')) | |
| #periodically check for updates | |
| def checkForUpdates(): | |
| threading.Timer(interval,checkForUpdates).start() | |
| update = createMessage() | |
| if update: | |
| conn = connectXMPP() | |
| sendMessage(conn,update) | |
| #conn.close() | |
| print("DEBUG: Last Checked at " + datetime.now().strftime("%Y-%m-%d %H:%M:%S")) | |
| print("DEBUG: Data sent: " + update) | |
| print("---------------------------\n\n\n") | |
| #start check cycle | |
| checkForUpdates() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment