summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu <alex_y_xu@yahoo.ca>2013-11-19 19:25:16 -0500
committerAlex Xu <alex_y_xu@yahoo.ca>2013-11-19 19:25:16 -0500
commitc00e1ce40cb3d6454593a7ef99e26c338e06c17d (patch)
treec562d5d709e9df8662cf112876ce0840d54b369c
parent205e656745e8d4efe60deb78b4a70de78ea5f187 (diff)
downloadrsnotify-master.tar.xz
rsnotify-master.zip
Fix. Stuff.HEADmaster
-rwxr-xr-xrsnotify.py92
1 files changed, 58 insertions, 34 deletions
diff --git a/rsnotify.py b/rsnotify.py
index 5cdc377..d04b719 100755
--- a/rsnotify.py
+++ b/rsnotify.py
@@ -4,39 +4,48 @@ 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)
+def init_logging():
+ global logger
+ logger = logging.getLogger(__name__)
+ logger.setLevel(config.LOGLEVEL)
-# create formatter
-formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
+ # create console handler and set level to debug
+ ch = logging.StreamHandler()
+ ch.setLevel(config.LOGLEVEL)
-# add formatter to ch
-ch.setFormatter(formatter)
+ # create formatter
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
-# add ch to logger
-logger.addHandler(ch)
+ # add formatter to ch
+ ch.setFormatter(formatter)
-s = requests.Session()
+ # add ch to logger
+ logger.addHandler(ch)
-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': ''
-})
+init_logging()
-if 'recaptcha' in r.text:
- raise Error("Login encountered reCAPTCHA. Log in with a browser and try again.")
+def login():
+ global s
+ s = requests.Session()
-logger.info("'Authorising' toolbar")
-s.post('http://services.runescape.com/m=toolbar/authorise.ws', data='remember=remember&submit=login')
+ 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': ''
+ })
-s.cookies['toolbar_activity_filter'] = 'AQAOACkAAAAqAAAAGAAAAAoAAAAXAAAAAAAAABYAAAABAAAAKAAAAAsAAAACAAAAAwAAAAwAAAAUAAA='
+ 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='
+
+login()
checks = [
{
@@ -51,31 +60,43 @@ checks = [
},
]
-def load(toload):
+def load(toload, tryrelog=True):
- logger.debug("Loading %s (%s)" % (toload['name'], toload['url']))
+ logger.info("Loading %s (%s)" % (toload['name'], toload['url']))
r = s.get(toload['url'])
- xml = etree.fromstring(r.content)
+ try:
+ xml = etree.fromstring(r.content)
+ except etree.XMLSyntaxError as e:
+ logger.info("Parse error (%s)" % e.args)
+ if tryrelog:
+ login()
+ return load(toload, tryrelog=False)
+ else:
+ raise e
+
items = xml.xpath('//MENU_ITEM')
toload['last'] = [next(item.iter('CAPTION')).text for item in items]
-def check(tocheck):
+ logger.debug('Loaded "%s"' % toload['last'])
- logger.info("Checking %s (%s)" % (tocheck['name'], tocheck['url']))
+def check(tocheck):
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)
+ if ' 0%' in caption:
+ logger.debug('Skipping "%s", contains 0%%' % caption)
else:
- logger.debug('"%s" found in last, skipping notification' % caption)
+ 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')
@@ -90,4 +111,7 @@ list(map(load, checks))
while True:
logger.info("Sleeping for %d seconds" % config.SLEEP)
time.sleep(config.SLEEP)
- list(map(check, checks))
+ try:
+ list(map(check, checks))
+ except requests.exceptions.ConnectionError:
+ logger.warning("Could not connect, trying again later.")