summaryrefslogtreecommitdiff
path: root/rsnotify.py
blob: 5cdc37797164fa988b2f410d82e334e298a0ea20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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))