From 205e656745e8d4efe60deb78b4a70de78ea5f187 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Tue, 5 Nov 2013 23:23:06 -0500 Subject: Version 0.1.0 --- .gitignore | 2 ++ config.py.example | 15 +++++++++ rsnotify.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 config.py.example create mode 100755 rsnotify.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ad16eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/config.py +/icon.png diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..c031e50 --- /dev/null +++ b/config.py.example @@ -0,0 +1,15 @@ +import os, logging + +USERNAME='' +PASSWORD='' + +# log level +LOGLEVEL=logging.INFO + +# seconds to sleep between queries +SLEEP=300 + +# icon for notifications +ICON=os.path.dirname(__file__ + '/icon.png') + +# vim:ft=python: diff --git a/rsnotify.py b/rsnotify.py new file mode 100755 index 0000000..5cdc377 --- /dev/null +++ b/rsnotify.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python + +import config, logging, requests, sys, time +from lxml import etree +from gi.repository import Notify + +logger = logging.getLogger(__name__) +logger.setLevel(config.LOGLEVEL) + +# create console handler and set level to debug +ch = logging.StreamHandler() +ch.setLevel(config.LOGLEVEL) + +# create formatter +formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + +# add formatter to ch +ch.setFormatter(formatter) + +# add ch to logger +logger.addHandler(ch) + +s = requests.Session() + +logger.info('Logging in as %s' % config.USERNAME) +r = s.post('https://secure.runescape.com/m=weblogin/login.ws', data={ + 'username': config.USERNAME, + 'password': config.PASSWORD, + 'mod': 'www', + 'dest': '' +}) + +if 'recaptcha' in r.text: + raise Error("Login encountered reCAPTCHA. Log in with a browser and try again.") + +logger.info("'Authorising' toolbar") +s.post('http://services.runescape.com/m=toolbar/authorise.ws', data='remember=remember&submit=login') + +s.cookies['toolbar_activity_filter'] = 'AQAOACkAAAAqAAAAGAAAAAoAAAAXAAAAAAAAABYAAAABAAAAKAAAAAsAAAACAAAAAwAAAAwAAAAUAAA=' + +checks = [ + { + 'name': 'D&D', + 'url': 'http://services.runescape.com/m=toolbar/activities.ws', + 'last': [], + }, + { + 'name': 'GE', + 'url': 'http://services.runescape.com/m=toolbar/geupdate.ws', + 'last': [], + }, +] + +def load(toload): + + logger.debug("Loading %s (%s)" % (toload['name'], toload['url'])) + + r = s.get(toload['url']) + + xml = etree.fromstring(r.content) + items = xml.xpath('//MENU_ITEM') + + toload['last'] = [next(item.iter('CAPTION')).text for item in items] + +def check(tocheck): + + logger.info("Checking %s (%s)" % (tocheck['name'], tocheck['url'])) + + thislast = tocheck['last'] + + load(tocheck) + + for caption in tocheck['last']: + logger.debug('Checking for "%s" in last' % caption) + if caption not in thislast: + notify('RuneScape %s notification' % tocheck['name'], caption) + else: + logger.debug('"%s" found in last, skipping notification' % caption) + +Notify.init('RuneScape Notifier') + +def notify(summary, body): + logger.info("Sending %s: %s" % (summary, body)) + n = Notify.Notification.new(summary, body, config.ICON) + n.show() + +logger.info("Preloading checks") +list(map(load, checks)) + +while True: + logger.info("Sleeping for %d seconds" % config.SLEEP) + time.sleep(config.SLEEP) + list(map(check, checks)) -- cgit v1.2.3-54-g00ecf