-
Notifications
You must be signed in to change notification settings - Fork 0
/
modsde.py
98 lines (79 loc) · 2.79 KB
/
modsde.py
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
94
95
96
97
98
#!/usr/bin/env python
import sys
import json
from datetime import datetime,timedelta
import urllib, urllib2
import re
import cookielib
import xml.etree.ElementTree as ET
class ModsDeChecker:
"""
This class returns i3status parsable output of the number of
unread posts in any bookmark in the mods.de forums.
"""
last_checked = datetime.now()
unread_cache = 0
login_url = 'http://login.mods.de/'
bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php"
opener = None
cj = None
logged_in = False
settings = {
'color': '#7181fe',
'pause': 20,
'username': "",
'password': ""
}
def __init__(self, settings = None):
self.settings.update(settings)
self.cj = cookielib.CookieJar()
self.last_checked = \
datetime.now() - timedelta(seconds=self.settings['pause'])
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cj))
def get_unread_count(self):
delta = datetime.now() - self.last_checked
if delta.total_seconds() > self.settings['pause']:
if not self.logged_in:
try:
self.login()
except Exception:
pass
try:
f = self.opener.open(self.bookmark_url)
root = ET.fromstring(f.read())
self.last_checked = datetime.now()
self.unread_cache = int(root.attrib['newposts'])
except Exception:
self.cj.clear()
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cj))
self.logged_in = False
return self.unread_cache
def login(self):
data = urllib.urlencode({
"login_username": self.settings["username"],
"login_password": self.settings["password"],
"login_lifetime": "31536000"
})
response = self.opener.open(self.login_url, data)
m = re.search("http://forum.mods.de/SSO.php[^']*", response.read())
self.cj.clear()
if m and m.group(0):
# get the cookie
response = self.opener.open(m.group(0))
for cookie in self.cj:
self.cj.clear
self.logged_in = True
self.opener.addheaders.append(('Cookie',
'{}={}'.format(cookie.name, cookie.value)))
return True
return False
def output(self):
unread = self.get_unread_count()
if not unread:
return None
return {'full_text' : '%d new posts in bookmarks' % unread,
'name' : 'modsde',
'urgent' : 'true',
'color' : self.settings['color']}