Skip to content
This repository has been archived by the owner on Oct 18, 2019. It is now read-only.

Commit

Permalink
adding tracked question persistence using JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Swanson committed May 31, 2010
1 parent 2ac0b64 commit cdd4b2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
*.sw*
*.pyc
tracking.json
47 changes: 36 additions & 11 deletions StackTracker.py
Expand Up @@ -8,6 +8,7 @@
import os
import copy
import re
import time

class QuestionItem(QtGui.QWidget):
def __init__(self, title, id, site):
Expand Down Expand Up @@ -46,7 +47,7 @@ def __repr__(self):


class Question():
def __init__(self, question_id, site):
def __init__(self, question_id, site, title = None):
self.last_queried = datetime.utcnow()
self.id = question_id
self.site = site
Expand All @@ -63,14 +64,20 @@ def __init__(self, question_id, site):

json_url = '%s/questions/%s/%s' \
% (api_base, self.id, StackTracker.API_KEY)
so_data = json.loads(urllib2.urlopen(json_url).read())
self.title = so_data['questions'][0]['title']

if title is None:
so_data = json.loads(urllib2.urlopen(json_url).read())
self.title = so_data['questions'][0]['title']
else:
self.title = title

if len(self.title) > 50:
self.title = self.title[:48] + '...'

def __repr__(self):
return "%s: %s" % (self.id, self.title)


class StackTracker(QtGui.QMainWindow):

SITES = {'stackoverflow.com':'#ff9900',
Expand All @@ -85,6 +92,7 @@ class StackTracker(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("StackTracker")
self.closeEvent = self.cleanUp

self.setGeometry(QtCore.QRect(0, 0, 325, 400))
self.setFixedSize(QtCore.QSize(325,400))
Expand Down Expand Up @@ -114,14 +122,7 @@ def __init__(self, parent = None):

self.tracking_list = []

self.test_questions = [('1711', 'stackoverflow.com'),
('4714', 'superuser.com'),
('45734', 'serverfault.com'),
('9134', 'meta.stackoverflow.com'),
]
for item in self.test_questions:
q = Question(item[0], item[1])
self.tracking_list.append(q)
self.deserializeQuestions() #load persisted questions from tracking.json

self.displayQuestions()

Expand All @@ -134,6 +135,30 @@ def __init__(self, parent = None):
self.connect(self.worker, QtCore.SIGNAL('newAnswer'), self.newAnswer)
self.connect(self.worker, QtCore.SIGNAL('newComment'), self.newComment)
self.worker.start()

def cleanUp(self, event):
self.serializeQuestions()

def serializeQuestions(self):
datetime_to_json = lambda obj: time.mktime(obj.timetuple()) if isinstance(obj, datetime) else None
a = []
for q in self.tracking_list:
a.append(q.__dict__)

with open('tracking.json', 'w') as fp:
json.dump({'questions':a}, fp, default = datetime_to_json, indent = 4)

def deserializeQuestions(self):
try:
with open('tracking.json', 'r') as fp:
data = fp.read()
except EnvironmentError:
#no tracking.json file
return

question_data = json.loads(data)
for q in question_data['questions']:
self.tracking_list.append(Question(q['id'], q['site'], q['title']))

def newAnswer(self, question):
self.popupUrl = question.url
Expand Down

0 comments on commit cdd4b2b

Please sign in to comment.