mirror of
https://github.com/captn3m0/hackertray.git
synced 2024-09-17 01:40:25 +00:00
pep8 changes
This commit is contained in:
parent
e34d767ef0
commit
3afba6f3eb
@ -5,7 +5,7 @@ import requests
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
if(os.environ.get('TRAVIS')!='true'):
|
||||
if(os.environ.get('TRAVIS') != 'true'):
|
||||
import pygtk
|
||||
|
||||
pygtk.require('2.0')
|
||||
@ -31,13 +31,15 @@ from .firefox import Firefox
|
||||
from .version import Version
|
||||
from .analytics import Analytics
|
||||
|
||||
|
||||
class HackerNewsApp:
|
||||
HN_URL_PREFIX = "https://news.ycombinator.com/item?id="
|
||||
UPDATE_URL = "https://github.com/captn3m0/hackertray#upgrade"
|
||||
ABOUT_URL = "https://github.com/captn3m0/hackertray"
|
||||
MIXPANEL_TOKEN = "51a04e37dad59393c7371407e84a8050"
|
||||
|
||||
def __init__(self, args):
|
||||
#Load the database
|
||||
# Load the database
|
||||
home = expanduser("~")
|
||||
with open(home + '/.hackertray.json', 'a+') as content_file:
|
||||
content_file.seek(0)
|
||||
@ -58,7 +60,7 @@ class HackerNewsApp:
|
||||
# create a menu
|
||||
self.menu = gtk.Menu()
|
||||
|
||||
#The default state is false, and it toggles when you click on it
|
||||
# The default state is false, and it toggles when you click on it
|
||||
self.commentState = args.comments
|
||||
|
||||
# create items for the menu - refresh, quit and a separator
|
||||
@ -79,14 +81,14 @@ class HackerNewsApp:
|
||||
|
||||
btnRefresh = gtk.MenuItem("Refresh")
|
||||
btnRefresh.show()
|
||||
#the last parameter is for not running the timer
|
||||
# the last parameter is for not running the timer
|
||||
btnRefresh.connect("activate", self.refresh, True, args.chrome)
|
||||
self.menu.append(btnRefresh)
|
||||
|
||||
if Version.new_available():
|
||||
btnUpdate = gtk.MenuItem("New Update Available")
|
||||
btnUpdate.show()
|
||||
btnUpdate.connect('activate',self.showUpdate)
|
||||
btnUpdate.connect('activate', self.showUpdate)
|
||||
self.menu.append(btnUpdate)
|
||||
|
||||
btnQuit = gtk.MenuItem("Quit")
|
||||
@ -106,7 +108,7 @@ class HackerNewsApp:
|
||||
launch_data['version'] = Version.current()
|
||||
launch_data['platform'] = platform.linux_distribution()
|
||||
try:
|
||||
launch_data['browser'] = subprocess.check_output(["xdg-settings","get","default-web-browser"]).strip()
|
||||
launch_data['browser'] = subprocess.check_output(["xdg-settings", "get", "default-web-browser"]).strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
launch_data['browser'] = "unknown"
|
||||
self.tracker.track('launch', launch_data)
|
||||
@ -115,26 +117,25 @@ class HackerNewsApp:
|
||||
"""Whether comments page is opened or not"""
|
||||
self.commentState = not self.commentState
|
||||
|
||||
def showUpdate(self,widget):
|
||||
def showUpdate(self, widget):
|
||||
"""Handle the update button"""
|
||||
webbrowser.open(HackerNewsApp.UPDATE_URL)
|
||||
# Remove the update button once clicked
|
||||
self.menu.remove(widget)
|
||||
self.tracker.visit(HackerNewsApp.UPDATE_URL)
|
||||
|
||||
|
||||
def showAbout(self, widget):
|
||||
"""Handle the about btn"""
|
||||
webbrowser.open(HackerNewsApp.ABOUT_URL)
|
||||
self.tracker.visit(HackerNewsApp.ABOUT_URL)
|
||||
|
||||
#ToDo: Handle keyboard interrupt properly
|
||||
# ToDo: Handle keyboard interrupt properly
|
||||
def quit(self, widget, data=None):
|
||||
""" Handler for the quit button"""
|
||||
l = list(self.db)
|
||||
home = expanduser("~")
|
||||
|
||||
#truncate the file
|
||||
# truncate the file
|
||||
with open(home + '/.hackertray.json', 'w+') as file:
|
||||
file.write(json.dumps(l))
|
||||
|
||||
@ -148,8 +149,8 @@ class HackerNewsApp:
|
||||
|
||||
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
|
||||
#to set it to active and we don't want the signal to be processed
|
||||
# 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
|
||||
if not widget.get_active():
|
||||
widget.disconnect(widget.signal_id)
|
||||
widget.set_active(True)
|
||||
@ -164,7 +165,7 @@ class HackerNewsApp:
|
||||
|
||||
def addItem(self, item):
|
||||
"""Adds an item to the menu"""
|
||||
#This is in the case of YC Job Postings, which we skip
|
||||
# This is in the case of YC Job Postings, which we skip
|
||||
if item['points'] == 0 or item['points'] is None:
|
||||
return
|
||||
|
||||
@ -184,7 +185,6 @@ class HackerNewsApp:
|
||||
i.show()
|
||||
|
||||
def refresh(self, widget=None, no_timer=False, chrome_data_directory=None, firefox_data_directory=None):
|
||||
|
||||
"""Refreshes the menu """
|
||||
try:
|
||||
# Create an array of 20 false to denote matches in History
|
||||
@ -197,12 +197,12 @@ class HackerNewsApp:
|
||||
if(firefox_data_directory):
|
||||
searchResults = self.mergeBoolArray(searchResults, Firefox.search(urls, firefox_data_directory))
|
||||
|
||||
#Remove all the current stories
|
||||
# Remove all the current stories
|
||||
for i in self.menu.get_children():
|
||||
if hasattr(i, 'url'):
|
||||
self.menu.remove(i)
|
||||
|
||||
#Add back all the refreshed news
|
||||
# Add back all the refreshed news
|
||||
for index, item in enumerate(data):
|
||||
item['history'] = searchResults[index]
|
||||
if item['url'].startswith('item?id='):
|
||||
@ -223,13 +223,14 @@ class HackerNewsApp:
|
||||
original[index] = original[index] or patch[index]
|
||||
return original
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Hacker News in your System Tray')
|
||||
parser.add_argument('-v','--version', action='version', version=Version.current())
|
||||
parser.add_argument('-c','--comments', dest='comments',action='store_true', help="Load the HN comments link for the article as well")
|
||||
parser.add_argument('-v', '--version', action='version', version=Version.current())
|
||||
parser.add_argument('-c', '--comments', dest='comments', action='store_true', help="Load the HN comments link for the article as well")
|
||||
parser.add_argument('--chrome', dest='chrome', help="Specify a Google Chrome Profile directory to use for matching chrome history")
|
||||
parser.add_argument('--firefox', dest='firefox', help="Specify a Firefox Profile directory to use for matching firefox history")
|
||||
parser.add_argument('--dnt', dest='dnt',action='store_true', help="Disable all analytics (Do Not Track)")
|
||||
parser.add_argument('--dnt', dest='dnt', action='store_true', help="Disable all analytics (Do Not Track)")
|
||||
parser.set_defaults(comments=False)
|
||||
parser.set_defaults(dnt=False)
|
||||
args = parser.parse_args()
|
||||
|
@ -1,9 +1,11 @@
|
||||
from mixpanel import Mixpanel
|
||||
|
||||
|
||||
class Analytics:
|
||||
# Setup analytics.
|
||||
# dnt - do not track. Disables tracking if True
|
||||
# token - The mixpanel token
|
||||
|
||||
def __init__(self, dnt, token):
|
||||
self.dnt = dnt
|
||||
self.tracker = Mixpanel(token)
|
||||
@ -12,12 +14,14 @@ class Analytics:
|
||||
# Track an event
|
||||
# event - string containing the event name
|
||||
# data - data related to the event, defaults to {}
|
||||
def track(self, event, data = {}):
|
||||
|
||||
def track(self, event, data={}):
|
||||
if(self.dnt == False):
|
||||
# All events are tracked anonymously
|
||||
self.tracker.track("anonymous", event, data)
|
||||
# Track a visit to a URL
|
||||
# The url maybe an HN submission or
|
||||
# The url maybe an HN submission or
|
||||
# some meta-url pertaining to hackertray
|
||||
|
||||
def visit(self, url):
|
||||
self.track('visit', {"link":url})
|
||||
self.track('visit', {"link": url})
|
||||
|
@ -35,6 +35,7 @@ def get_icon_filename(icon_name):
|
||||
# The main class
|
||||
class Indicator:
|
||||
# Constructor
|
||||
|
||||
def __init__(self, unknown, icon, category):
|
||||
# Store the settings
|
||||
self.inactive_icon = get_icon_filename(icon)
|
||||
|
@ -4,8 +4,10 @@ import shutil
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
class Chrome:
|
||||
HISTORY_TMP_LOCATION = '/tmp/hackertray.chrome'
|
||||
|
||||
@staticmethod
|
||||
def search(urls, config_folder_path):
|
||||
Chrome.setup(config_folder_path)
|
||||
@ -13,13 +15,14 @@ class Chrome:
|
||||
db = conn.cursor()
|
||||
result = []
|
||||
for url in urls:
|
||||
db_result = db.execute('SELECT url from urls WHERE url=:url',{"url":url})
|
||||
db_result = db.execute('SELECT url from urls WHERE url=:url', {"url": url})
|
||||
if(db.fetchone() == None):
|
||||
result.append(False)
|
||||
else:
|
||||
result.append(True)
|
||||
os.remove(Chrome.HISTORY_TMP_LOCATION)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def setup(config_folder_path):
|
||||
file_name = os.path.abspath(config_folder_path+'/History')
|
||||
|
@ -4,9 +4,11 @@ import shutil
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
class Firefox:
|
||||
HISTORY_TMP_LOCATION = '/tmp/hackertray.firefox'
|
||||
HISTORY_FILE_NAME = '/places.sqlite'
|
||||
|
||||
@staticmethod
|
||||
def search(urls, config_folder_path):
|
||||
Firefox.setup(config_folder_path)
|
||||
@ -14,17 +16,18 @@ class Firefox:
|
||||
db = conn.cursor()
|
||||
result = []
|
||||
for url in urls:
|
||||
db_result = db.execute('SELECT url from moz_places WHERE url=:url',{"url":url})
|
||||
db_result = db.execute('SELECT url from moz_places WHERE url=:url', {"url": url})
|
||||
if(db.fetchone() == None):
|
||||
result.append(False)
|
||||
else:
|
||||
result.append(True)
|
||||
os.remove(Firefox.HISTORY_TMP_LOCATION)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def setup(config_folder_path):
|
||||
file_name = os.path.abspath(config_folder_path+Firefox.HISTORY_FILE_NAME)
|
||||
if not os.path.isfile(file_name):
|
||||
print("ERROR: ", "Could not find Firefox history file", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
shutil.copyfile(file_name, Firefox.HISTORY_TMP_LOCATION)
|
||||
shutil.copyfile(file_name, Firefox.HISTORY_TMP_LOCATION)
|
||||
|
@ -7,6 +7,7 @@ urls = [
|
||||
|
||||
|
||||
class HackerNews:
|
||||
|
||||
@staticmethod
|
||||
def getHomePage():
|
||||
random.shuffle(urls)
|
||||
@ -15,4 +16,4 @@ class HackerNews:
|
||||
try:
|
||||
return r.json()
|
||||
except ValueError:
|
||||
continue
|
||||
continue
|
||||
|
@ -1,8 +1,10 @@
|
||||
import requests
|
||||
import pkg_resources
|
||||
|
||||
|
||||
class Version:
|
||||
PYPI_URL = "https://pypi.python.org/pypi/hackertray/json"
|
||||
|
||||
@staticmethod
|
||||
def latest():
|
||||
res = requests.get(Version.PYPI_URL).json()
|
||||
@ -14,7 +16,7 @@ class Version:
|
||||
|
||||
@staticmethod
|
||||
def new_available():
|
||||
latest = Version.latest()
|
||||
latest = Version.latest()
|
||||
current = Version.current()
|
||||
try:
|
||||
if pkg_resources.parse_version(latest) > pkg_resources.parse_version(current):
|
||||
@ -24,4 +26,4 @@ class Version:
|
||||
return False
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("[+] There was an error in trying to fetch updates")
|
||||
return False
|
||||
return False
|
||||
|
Loading…
Reference in New Issue
Block a user