2011-06-23 16:40:22 -04:00
|
|
|
# Adapted from Eco-Mono's F5Stuck RSS Client
|
|
|
|
|
2016-12-21 17:28:02 -05:00
|
|
|
# karxi: Now that Homestuck is over, this might be removed.
|
|
|
|
# Even if it isn't, it needs cleanup; the code is unpleasant.
|
|
|
|
|
2021-03-23 17:37:31 -04:00
|
|
|
import feedparser
|
2011-06-23 16:40:22 -04:00
|
|
|
import pickle
|
|
|
|
import os
|
2016-12-21 17:28:02 -05:00
|
|
|
import sys
|
2011-07-23 15:28:40 -04:00
|
|
|
import threading
|
2011-06-23 16:40:22 -04:00
|
|
|
from time import mktime
|
2021-03-23 17:36:43 -04:00
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
2011-06-23 16:40:22 -04:00
|
|
|
|
2016-12-21 17:28:02 -05:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
class MSPAChecker(QtWidgets.QWidget):
|
2011-06-23 16:40:22 -04:00
|
|
|
def __init__(self, parent=None):
|
2016-12-21 17:28:02 -05:00
|
|
|
#~QtCore.QObject.__init__(self, parent)
|
|
|
|
# karxi: Why would you do that?
|
|
|
|
super(MSPAChecker, self).__init__(parent)
|
2011-06-23 16:40:22 -04:00
|
|
|
self.mainwindow = parent
|
|
|
|
self.refreshRate = 30 # seconds
|
|
|
|
self.status = None
|
2011-07-23 15:28:40 -04:00
|
|
|
self.lock = False
|
2016-11-13 01:14:39 -05:00
|
|
|
self.timer = QtCore.QTimer(self)
|
2021-03-23 17:36:43 -04:00
|
|
|
self.timer.timeout.connect(self.check_site_wrapper)
|
2011-06-23 16:40:22 -04:00
|
|
|
self.timer.start(1000*self.refreshRate)
|
|
|
|
|
|
|
|
def save_state(self):
|
|
|
|
try:
|
2016-12-21 17:28:02 -05:00
|
|
|
with open("status_new.pkl", "w") as current_status:
|
|
|
|
pickle.dump(self.status, current_status)
|
2011-06-23 16:40:22 -04:00
|
|
|
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")
|
2016-11-29 15:20:41 -05:00
|
|
|
except Exception as e:
|
2016-12-21 17:28:02 -05:00
|
|
|
logging.error("Error: %s", e, exc_info=sys.exc_info())
|
2021-03-23 17:36:43 -04:00
|
|
|
msg = QtWidgets.QMessageBox(self)
|
2011-06-23 16:40:22 -04:00
|
|
|
msg.setText("Problems writing save file.")
|
|
|
|
msg.show()
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot()
|
2011-07-23 15:28:40 -04:00
|
|
|
def check_site_wrapper(self):
|
2011-06-23 16:40:22 -04:00
|
|
|
if not self.mainwindow.config.checkMSPA():
|
|
|
|
return
|
2011-07-23 13:06:06 -04:00
|
|
|
if self.lock:
|
|
|
|
return
|
2016-12-21 17:28:02 -05:00
|
|
|
logging.debug("Checking MSPA updates...")
|
2011-07-23 15:28:40 -04:00
|
|
|
s = threading.Thread(target=self.check_site)
|
|
|
|
s.start()
|
|
|
|
|
|
|
|
def check_site(self):
|
2011-06-23 16:40:22 -04:00
|
|
|
rss = None
|
|
|
|
must_save = False
|
|
|
|
try:
|
2011-07-23 13:06:06 -04:00
|
|
|
self.lock = True
|
2011-06-23 16:40:22 -04:00
|
|
|
rss = feedparser.parse("http://www.mspaintadventures.com/rss/rss.xml")
|
|
|
|
except:
|
|
|
|
return
|
2011-07-23 13:06:06 -04:00
|
|
|
finally:
|
|
|
|
self.lock = False
|
2011-06-23 16:40:22 -04:00
|
|
|
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']:
|
2011-07-04 08:02:36 -04:00
|
|
|
if not hasattr(self, "mspa"):
|
|
|
|
self.mspa = None
|
2011-07-08 19:41:18 -04:00
|
|
|
if not self.mspa:
|
2011-07-04 08:02:36 -04:00
|
|
|
self.mspa = MSPAUpdateWindow(self.parent())
|
2021-03-23 17:36:43 -04:00
|
|
|
self.mspa.accepted.connect(self.visit_site)
|
|
|
|
self.mspa.rejected.connect(self.nothing)
|
2011-07-04 08:02:36 -04:00
|
|
|
self.mspa.show()
|
2011-06-23 16:40:22 -04:00
|
|
|
else:
|
2016-12-21 17:28:02 -05:00
|
|
|
#logging.info("No new updates :(")
|
2011-06-23 16:40:22 -04:00
|
|
|
pass
|
|
|
|
if must_save:
|
|
|
|
self.save_state()
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot()
|
|
|
|
def visit_site(self):
|
2016-12-21 17:28:02 -05:00
|
|
|
logging.debug(self.status['last_visited']['link'])
|
2011-06-23 16:40:22 -04:00
|
|
|
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
|
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
class MSPAUpdateWindow(QtWidgets.QDialog):
|
2011-06-23 16:40:22 -04:00
|
|
|
def __init__(self, parent=None):
|
2016-12-21 17:28:02 -05:00
|
|
|
super(MSPAUpdateWindow, self).__init__(parent)
|
2011-06-23 16:40:22 -04:00
|
|
|
self.mainwindow = parent
|
|
|
|
self.setStyleSheet(self.mainwindow.theme["main/defaultwindow/style"])
|
|
|
|
self.setWindowTitle("MSPA Update!")
|
|
|
|
self.setModal(False)
|
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
self.title = QtWidgets.QLabel("You have an unread MSPA update! :o)")
|
2011-06-23 16:40:22 -04:00
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
layout_0 = QtWidgets.QVBoxLayout()
|
2011-06-23 16:40:22 -04:00
|
|
|
layout_0.addWidget(self.title)
|
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
self.ok = QtWidgets.QPushButton("GO READ NOW!", self)
|
2011-06-23 16:40:22 -04:00
|
|
|
self.ok.setDefault(True)
|
2021-03-23 17:36:43 -04:00
|
|
|
self.ok.clicked.connect(self.accept)
|
|
|
|
self.cancel = QtWidgets.QPushButton("LATER", self)
|
|
|
|
self.cancel.clicked.connect(self.reject)
|
|
|
|
layout_2 = QtWidgets.QHBoxLayout()
|
2011-06-23 16:40:22 -04:00
|
|
|
layout_2.addWidget(self.cancel)
|
|
|
|
layout_2.addWidget(self.ok)
|
|
|
|
|
|
|
|
layout_0.addLayout(layout_2)
|
|
|
|
|
|
|
|
self.setLayout(layout_0)
|