Be more Pythonic and Python 3.x compatible
This commit is contained in:
parent
2b4dd31a65
commit
1e83a78129
|
@ -1,10 +1,10 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import pygtk
|
import pygtk
|
||||||
|
|
||||||
pygtk.require('2.0')
|
pygtk.require('2.0')
|
||||||
import gtk
|
import gtk
|
||||||
|
|
||||||
import requests
|
|
||||||
import webbrowser
|
import webbrowser
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
|
@ -21,28 +21,31 @@ from appindicator_replacement import get_icon_filename
|
||||||
##This is to get --version to work
|
##This is to get --version to work
|
||||||
try:
|
try:
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
|
|
||||||
__version = pkg_resources.require("hackertray")[0].version
|
__version = pkg_resources.require("hackertray")[0].version
|
||||||
except ImportError, e:
|
except ImportError:
|
||||||
__version = "Can't read version number."
|
__version = "Can't read version number."
|
||||||
|
|
||||||
from hackernews import HackerNews
|
from hackernews import HackerNews
|
||||||
|
|
||||||
|
|
||||||
class HackerNewsApp:
|
class HackerNewsApp:
|
||||||
HN_URL_PREFIX="https://news.ycombinator.com/item?id="
|
HN_URL_PREFIX = "https://news.ycombinator.com/item?id="
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
#Load the database
|
#Load the database
|
||||||
home = expanduser("~")
|
home = expanduser("~")
|
||||||
with open(home+'/.hackertray.json', 'a+') as content_file:
|
with open(home + '/.hackertray.json', 'a+') as content_file:
|
||||||
content_file.seek(0)
|
content_file.seek(0)
|
||||||
content = content_file.read()
|
content = content_file.read()
|
||||||
try:
|
try:
|
||||||
self.db = set(json.loads(content))
|
self.db = set(json.loads(content))
|
||||||
except:
|
except ValueError:
|
||||||
self.db = set()
|
self.db = set()
|
||||||
|
|
||||||
# create an indicator applet
|
# 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_icon(get_icon_filename("hacker-tray.png"))
|
self.ind.set_icon(get_icon_filename("hacker-tray.png"))
|
||||||
|
|
||||||
# create a menu
|
# create a menu
|
||||||
|
@ -68,7 +71,8 @@ class HackerNewsApp:
|
||||||
|
|
||||||
btnRefresh = gtk.MenuItem("Refresh")
|
btnRefresh = gtk.MenuItem("Refresh")
|
||||||
btnRefresh.show()
|
btnRefresh.show()
|
||||||
btnRefresh.connect("activate", self.refresh, True) #the last parameter is for not running the timer
|
#the last parameter is for not running the timer
|
||||||
|
btnRefresh.connect("activate", self.refresh, True)
|
||||||
self.menu.append(btnRefresh)
|
self.menu.append(btnRefresh)
|
||||||
|
|
||||||
btnQuit = gtk.MenuItem("Quit")
|
btnQuit = gtk.MenuItem("Quit")
|
||||||
|
@ -81,22 +85,24 @@ class HackerNewsApp:
|
||||||
self.ind.set_menu(self.menu)
|
self.ind.set_menu(self.menu)
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
'''Whether comments page is opened or not'''
|
|
||||||
def toggleComments(self, widget):
|
def toggleComments(self, widget):
|
||||||
|
"""Whether comments page is opened or not"""
|
||||||
self.commentState = not self.commentState
|
self.commentState = not self.commentState
|
||||||
|
|
||||||
'''Handle the about btn'''
|
|
||||||
def showAbout(self, widget):
|
def showAbout(self, widget):
|
||||||
|
"""Handle the about btn"""
|
||||||
webbrowser.open("https://github.com/captn3m0/hackertray/")
|
webbrowser.open("https://github.com/captn3m0/hackertray/")
|
||||||
|
|
||||||
''' Handler for the quit button'''
|
|
||||||
#ToDo: Handle keyboard interrupt properly
|
#ToDo: Handle keyboard interrupt properly
|
||||||
def quit(self, widget, data=None):
|
def quit(self, widget, data=None):
|
||||||
l=list(self.db)
|
""" Handler for the quit button"""
|
||||||
|
l = list(self.db)
|
||||||
home = expanduser("~")
|
home = expanduser("~")
|
||||||
|
|
||||||
#truncate the file
|
#truncate the file
|
||||||
with open(home+'/.hackertray.json', 'w+') as file:
|
with open(home + '/.hackertray.json', 'w+') as file:
|
||||||
file.write(json.dumps(l))
|
file.write(json.dumps(l))
|
||||||
|
|
||||||
gtk.main_quit()
|
gtk.main_quit()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
@ -104,24 +110,30 @@ class HackerNewsApp:
|
||||||
gtk.main()
|
gtk.main()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
'''Opens the link in the web browser'''
|
|
||||||
def open(self, widget, event=None, data=None):
|
def open(self, widget, event=None, data=None):
|
||||||
|
"""Opens the link in the web browser"""
|
||||||
#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
|
||||||
if(widget.get_active() == False):
|
if not widget.get_active():
|
||||||
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)
|
self.db.add(widget.item_id)
|
||||||
webbrowser.open(widget.url)
|
webbrowser.open(widget.url)
|
||||||
if(self.commentState):
|
|
||||||
webbrowser.open(self.HN_URL_PREFIX+widget.hn_id)
|
|
||||||
|
|
||||||
'''Adds an item to the menu'''
|
if self.commentState:
|
||||||
|
webbrowser.open(self.HN_URL_PREFIX + widget.hn_id)
|
||||||
|
|
||||||
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
|
"""Adds an item to the menu"""
|
||||||
|
#This is in the case of YC Job Postings, which we skip
|
||||||
|
if item['points'] == 0 or item['points'] is None:
|
||||||
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.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)
|
||||||
|
@ -130,19 +142,23 @@ class HackerNewsApp:
|
||||||
self.menu.prepend(i)
|
self.menu.prepend(i)
|
||||||
i.show()
|
i.show()
|
||||||
|
|
||||||
'''Refreshes the menu '''
|
|
||||||
def refresh(self, widget=None, no_timer=False):
|
def refresh(self, widget=None, no_timer=False):
|
||||||
data = reversed(HackerNews.getHomePage()[0:20]);
|
"""Refreshes the menu """
|
||||||
|
data = reversed(HackerNews.getHomePage()[0:20])
|
||||||
|
|
||||||
#Remove all the current stories
|
#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)
|
||||||
|
|
||||||
#Add back all the refreshed news
|
#Add back all the refreshed news
|
||||||
for i in data:
|
for i in data:
|
||||||
self.addItem(i)
|
self.addItem(i)
|
||||||
|
|
||||||
#Call every 5 minutes
|
#Call every 5 minutes
|
||||||
if no_timer==False:
|
if not no_timer:
|
||||||
gtk.timeout_add(10*60*1000, self.refresh)
|
gtk.timeout_add(10 * 60 * 1000, self.refresh)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Hacker News in your System Tray')
|
parser = argparse.ArgumentParser(description='Hacker News in your System Tray')
|
||||||
|
|
|
@ -14,7 +14,6 @@ import gobject
|
||||||
|
|
||||||
# We also need os and sys
|
# We also need os and sys
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
from pkg_resources import resource_filename
|
from pkg_resources import resource_filename
|
||||||
|
|
||||||
|
@ -27,18 +26,16 @@ STATUS_ATTENTION = 1
|
||||||
|
|
||||||
# Locations to search for the given icon
|
# Locations to search for the given icon
|
||||||
|
|
||||||
def get_icon_filename(icon_name):
|
|
||||||
|
|
||||||
|
def get_icon_filename(icon_name):
|
||||||
# Determine where the icon is
|
# Determine where the icon is
|
||||||
return os.path.abspath(resource_filename('hackertray.data', 'hacker-tray.png'))
|
return os.path.abspath(resource_filename('hackertray.data', 'hacker-tray.png'))
|
||||||
|
|
||||||
|
|
||||||
# The main class
|
# The main class
|
||||||
class Indicator:
|
class Indicator:
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
def __init__(self, unknown, icon, category):
|
||||||
def __init__ (self,unknown,icon,category):
|
|
||||||
|
|
||||||
# Store the settings
|
# Store the settings
|
||||||
self.inactive_icon = get_icon_filename(icon)
|
self.inactive_icon = get_icon_filename(icon)
|
||||||
self.active_icon = "" # Blank until the user calls set_attention_icon
|
self.active_icon = "" # Blank until the user calls set_attention_icon
|
||||||
|
@ -52,8 +49,7 @@ class Indicator:
|
||||||
# Set the rest of the vars
|
# Set the rest of the vars
|
||||||
self.menu = None # We have no menu yet
|
self.menu = None # We have no menu yet
|
||||||
|
|
||||||
def set_menu(self,menu):
|
def set_menu(self, menu):
|
||||||
|
|
||||||
# Save a copy of the menu
|
# Save a copy of the menu
|
||||||
self.menu = menu
|
self.menu = menu
|
||||||
|
|
||||||
|
@ -63,7 +59,6 @@ class Indicator:
|
||||||
self.icon.connect("activate", self.show_menu)
|
self.icon.connect("activate", self.show_menu)
|
||||||
|
|
||||||
def set_status(self, status):
|
def set_status(self, status):
|
||||||
|
|
||||||
# Status defines whether the active or inactive
|
# Status defines whether the active or inactive
|
||||||
# icon should be displayed.
|
# icon should be displayed.
|
||||||
if status == STATUS_ACTIVE:
|
if status == STATUS_ACTIVE:
|
||||||
|
@ -76,19 +71,16 @@ class Indicator:
|
||||||
return
|
return
|
||||||
|
|
||||||
def set_icon(self, icon):
|
def set_icon(self, icon):
|
||||||
|
|
||||||
# Set the new icon
|
# Set the new icon
|
||||||
self.icon.set_from_file(get_icon_filename(icon))
|
self.icon.set_from_file(get_icon_filename(icon))
|
||||||
|
|
||||||
def set_attention_icon(self, icon):
|
def set_attention_icon(self, icon):
|
||||||
|
|
||||||
# Set the icon filename as the attention icon
|
# Set the icon filename as the attention icon
|
||||||
self.active_icon = get_icon_filename(icon)
|
self.active_icon = get_icon_filename(icon)
|
||||||
|
|
||||||
def show_menu(self, widget):
|
def show_menu(self, widget):
|
||||||
|
|
||||||
# Show the menu
|
# Show the menu
|
||||||
self.menu.popup(None,None,None,0,0)
|
self.menu.popup(None, None, None, 0, 0)
|
||||||
|
|
||||||
# Get the location and size of the window
|
# Get the location and size of the window
|
||||||
mouse_rect = self.menu.get_window().get_frame_extents()
|
mouse_rect = self.menu.get_window().get_frame_extents()
|
||||||
|
@ -102,14 +94,13 @@ class Indicator:
|
||||||
self.timer = gobject.timeout_add(100, self.check_mouse)
|
self.timer = gobject.timeout_add(100, self.check_mouse)
|
||||||
|
|
||||||
def check_mouse(self):
|
def check_mouse(self):
|
||||||
|
|
||||||
if not self.menu.get_window().is_visible():
|
if not self.menu.get_window().is_visible():
|
||||||
return
|
return
|
||||||
|
|
||||||
# Now check the global mouse coords
|
# Now check the global mouse coords
|
||||||
root = self.menu.get_screen().get_root_window()
|
root = self.menu.get_screen().get_root_window()
|
||||||
|
|
||||||
x,y,z = root.get_pointer()
|
x, y, z = root.get_pointer()
|
||||||
|
|
||||||
if x < self.x or x > self.right or y < self.y or y > self.bottom:
|
if x < self.x or x > self.right or y < self.y or y > self.bottom:
|
||||||
self.hide_menu()
|
self.hide_menu()
|
||||||
|
@ -117,5 +108,4 @@ class Indicator:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def hide_menu(self):
|
def hide_menu(self):
|
||||||
|
|
||||||
self.menu.popdown()
|
self.menu.popdown()
|
||||||
|
|
|
@ -3,15 +3,16 @@ import requests
|
||||||
|
|
||||||
urls = [
|
urls = [
|
||||||
'https://node-hnapi.herokuapp.com/'
|
'https://node-hnapi.herokuapp.com/'
|
||||||
];
|
]
|
||||||
|
|
||||||
|
|
||||||
class HackerNews:
|
class HackerNews:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getHomePage():
|
def getHomePage():
|
||||||
random.shuffle(urls)
|
random.shuffle(urls)
|
||||||
for i in urls:
|
for i in urls:
|
||||||
r = requests.get(i+"news")
|
r = requests.get(i + "news")
|
||||||
try:
|
try:
|
||||||
return r.json()
|
return r.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue;
|
continue
|
|
@ -1,10 +1,12 @@
|
||||||
import unittest
|
import unittest
|
||||||
from hackernews import HackerNews
|
from hackernews import HackerNews
|
||||||
|
|
||||||
|
|
||||||
class HNTest(unittest.TestCase):
|
class HNTest(unittest.TestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
data = HackerNews.getHomePage()
|
data = HackerNews.getHomePage()
|
||||||
self.assertTrue(len(data)>0)
|
self.assertTrue(len(data) > 0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
6
setup.py
6
setup.py
|
@ -1,6 +1,8 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
from setuptools import find_packages
|
from setuptools import find_packages
|
||||||
import sys
|
|
||||||
|
|
||||||
requirements = ['requests']
|
requirements = ['requests']
|
||||||
if sys.version_info < (2, 7):
|
if sys.version_info < (2, 7):
|
||||||
|
@ -17,7 +19,7 @@ setup(name='hackertray',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
package_data={
|
package_data={
|
||||||
'hackertray.data':['hacker-tray.png']
|
'hackertray.data': ['hacker-tray.png']
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'requests>=2.0',
|
'requests>=2.0',
|
||||||
|
|
Loading…
Reference in New Issue