Adds persistent read-memory ie Remembers which links you opened

Uses json format for remembering (~/.hackertray.json)
This commit is contained in:
Abhay Rana 2013-11-28 12:47:04 +05:30
parent af21ec43a5
commit 167397e51f
2 changed files with 33 additions and 5 deletions

View File

@ -16,8 +16,6 @@ because nw doesn't support AppIndicators yet.
4. Shows Points/Comment count in a simple format 4. Shows Points/Comment count in a simple format
##To-Do ##To-Do
- Implement a way to remember which stories have been read
- Ideally app-memory should be persistent (save on disk)
- Auto Start - Auto Start
- Convert to a Python Module - Convert to a Python Module
- Try to convert right click to comments link - Try to convert right click to comments link

View File

@ -6,6 +6,9 @@ import gtk
import requests import requests
import webbrowser import webbrowser
import json
from os.path import expanduser
try: try:
import appindicator import appindicator
@ -14,6 +17,17 @@ except ImportError:
class HackerNewsApp: class HackerNewsApp:
def __init__(self): def __init__(self):
#Load the database
home = expanduser("~")
with open(home+'/.hackertray.json', 'a+') as content_file:
content_file.seek(0)
content = content_file.read()
try:
self.db = set(json.loads(content))
except:
self.db = set()
# create an indicator applet
self.ind = appindicator.Indicator ("Hacker Tray", "hacker-tray", appindicator.CATEGORY_APPLICATION_STATUS) self.ind = appindicator.Indicator ("Hacker Tray", "hacker-tray", appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status (appindicator.STATUS_ACTIVE) self.ind.set_status (appindicator.STATUS_ACTIVE)
self.ind.set_label("Y") self.ind.set_label("Y")
@ -21,7 +35,7 @@ class HackerNewsApp:
# create a menu # create a menu
self.menu = gtk.Menu() self.menu = gtk.Menu()
# create items for the menu - labels, checkboxes, radio buttons and images are supported: # create items for the menu - refresh, quit and a separator
menuSeparator = gtk.SeparatorMenuItem() menuSeparator = gtk.SeparatorMenuItem()
menuSeparator.show() menuSeparator.show()
self.menu.append(menuSeparator) self.menu.append(menuSeparator)
@ -41,9 +55,17 @@ class HackerNewsApp:
self.ind.set_menu(self.menu) self.ind.set_menu(self.menu)
self.refresh() self.refresh()
''' Handler for the quit button'''
#ToDo: Handle keyboard interrupt properly
def quit(self, widget, data=None): def quit(self, widget, data=None):
l=list(self.db)
home = expanduser("~")
#truncate the file
file = open(home+'/.hackertray.json', 'w+')
file.write(json.dumps(l))
gtk.main_quit() gtk.main_quit()
'''Opens the link in the web browser'''
def open(self, widget, event=None, data=None): def open(self, widget, event=None, data=None):
#We disconnect and reconnect the event in case we have #We disconnect and reconnect the event in case we have
#to set it to active and we don't want the signal to be processed #to set it to active and we don't want the signal to be processed
@ -51,27 +73,35 @@ class HackerNewsApp:
widget.disconnect(widget.signal_id) widget.disconnect(widget.signal_id)
widget.set_active(True) widget.set_active(True)
widget.signal_id = widget.connect('activate', self.open) widget.signal_id = widget.connect('activate', self.open)
self.db.add(widget.item_id)
webbrowser.open(widget.url) webbrowser.open(widget.url)
'''Adds an item to the menu'''
def addItem(self, item): def addItem(self, item):
if(item['points'] == 0 or item['points'] == None): #This is in the case of YC Job Postings, which we skip if(item['points'] == 0 or item['points'] == None): #This is in the case of YC Job Postings, which we skip
return return
i = gtk.CheckMenuItem("("+str(item['points']).zfill(3)+"/"+str(item['comments_count']).zfill(3)+") "+item['title']) i = gtk.CheckMenuItem("("+str(item['points']).zfill(3)+"/"+str(item['comments_count']).zfill(3)+") "+item['title'])
i.set_active(item['id'] in self.db)
i.url = item['url'] i.url = item['url']
i.signal_id = i.connect('activate', self.open) i.signal_id = i.connect('activate', self.open)
i.item_id = item['id']
self.menu.prepend(i) self.menu.prepend(i)
i.show() i.show()
'''Refreshes the menu '''
def refresh(self, widget=None, data=None): def refresh(self, widget=None, data=None):
self.data = reversed(getHomePage()[0:20]); data = reversed(getHomePage()[0:20]);
#Remove all the current stories
for i in self.menu.get_children(): for i in self.menu.get_children():
if(hasattr(i,'url')): if(hasattr(i,'url')):
self.menu.remove(i) self.menu.remove(i)
for i in self.data: #Add back all the refreshed news
for i in data:
self.addItem(i) self.addItem(i)
#Call every 5 minutes #Call every 5 minutes
gtk.timeout_add(5*60*1000, self.refresh) gtk.timeout_add(5*60*1000, self.refresh)
'''Returns all the news stories from homepage'''
def getHomePage(): def getHomePage():
r = requests.get('https://node-hnapi.herokuapp.com/news') r = requests.get('https://node-hnapi.herokuapp.com/news')
return r.json() return r.json()