forked from illuminatedwax/pesterchum
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdatecheck.py
132 lines (120 loc) · 4.88 KB
/
updatecheck.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Adapted from Eco-Mono's F5Stuck RSS Client
from libs import feedparser
import pickle
import os
import threading
from time import mktime
from PyQt4 import QtCore, QtGui
class MSPAChecker(QtGui.QWidget):
def __init__(self, parent=None):
QtCore.QObject.__init__(self, parent)
self.mainwindow = parent
self.refreshRate = 30 # seconds
self.status = None
self.lock = False
self.timer = QtCore.QTimer(self)
self.connect(self.timer, QtCore.SIGNAL('timeout()'),
self, QtCore.SLOT('check_site_wrapper()'))
self.timer.start(1000*self.refreshRate)
def save_state(self):
try:
current_status = open("status_new.pkl","w")
pickle.dump(self.status, current_status)
current_status.close()
try:
os.rename("status.pkl","status_old.pkl")
except:
pass
try:
os.rename("status_new.pkl","status.pkl")
except:
if os.path.exists("status_old.pkl"):
os.rename("status_old.pkl","status.pkl")
raise
if os.path.exists("status_old.pkl"):
os.remove("status_old.pkl")
except Exception, e:
print e
msg = QtGui.QMessageBox(self)
msg.setText("Problems writing save file.")
msg.show()
@QtCore.pyqtSlot()
def check_site_wrapper(self):
if not self.mainwindow.config.checkMSPA():
return
if self.lock:
return
print "Checking MSPA updates..."
s = threading.Thread(target=self.check_site)
s.start()
def check_site(self):
rss = None
must_save = False
try:
self.lock = True
rss = feedparser.parse("http://www.mspaintadventures.com/rss/rss.xml")
except:
return
finally:
self.lock = False
if len(rss.entries) == 0:
return
entries = sorted(rss.entries,key=(lambda x: mktime(x.updated_parsed)))
if self.status == None:
self.status = {}
self.status['last_visited'] = {'pubdate':mktime(entries[-1].updated_parsed),'link':entries[-1].link}
self.status['last_seen'] = {'pubdate':mktime(entries[-1].updated_parsed),'link':entries[-1].link}
must_save = True
elif mktime(entries[-1].updated_parsed) > self.status['last_seen']['pubdate']:
#This is the first time the app itself has noticed this update.
self.status['last_seen'] = {'pubdate':mktime(entries[-1].updated_parsed),'link':entries[-1].link}
must_save = True
if self.status['last_seen']['pubdate'] > self.status['last_visited']['pubdate']:
if not hasattr(self, "mspa"):
self.mspa = None
if not self.mspa:
self.mspa = MSPAUpdateWindow(self.parent())
self.connect(self.mspa, QtCore.SIGNAL('accepted()'),
self, QtCore.SLOT('visit_site()'))
self.connect(self.mspa, QtCore.SIGNAL('rejected()'),
self, QtCore.SLOT('nothing()'))
self.mspa.show()
else:
#print "No new updates :("
pass
if must_save:
self.save_state()
@QtCore.pyqtSlot()
def visit_site(self):
print self.status['last_visited']['link']
QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.status['last_visited']['link'], QtCore.QUrl.TolerantMode))
if self.status['last_seen']['pubdate'] > self.status['last_visited']['pubdate']:
#Visited for the first time. Untrip the icon and remember that we saw it.
self.status['last_visited'] = self.status['last_seen']
self.save_state()
self.mspa = None
@QtCore.pyqtSlot()
def nothing(self):
self.mspa = None
class MSPAUpdateWindow(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.mainwindow = parent
self.setStyleSheet(self.mainwindow.theme["main/defaultwindow/style"])
self.setWindowTitle("MSPA Update!")
self.setModal(False)
self.title = QtGui.QLabel("You have an unread MSPA update! :o)")
layout_0 = QtGui.QVBoxLayout()
layout_0.addWidget(self.title)
self.ok = QtGui.QPushButton("GO READ NOW!", self)
self.ok.setDefault(True)
self.connect(self.ok, QtCore.SIGNAL('clicked()'),
self, QtCore.SLOT('accept()'))
self.cancel = QtGui.QPushButton("LATER", self)
self.connect(self.cancel, QtCore.SIGNAL('clicked()'),
self, QtCore.SLOT('reject()'))
layout_2 = QtGui.QHBoxLayout()
layout_2.addWidget(self.cancel)
layout_2.addWidget(self.ok)
layout_0.addLayout(layout_2)
self.setLayout(layout_0)