From 8f0b08137b6c4b05ebe63a33029a36c068cfbc05 Mon Sep 17 00:00:00 2001 From: Abhay Rana Date: Sat, 30 Nov 2013 01:16:39 +0530 Subject: [PATCH] Implements a simple load balancer to divide API requests among 5 servers. Made possible thanks to @cheeaun's work on hackerweb. Moves hackernews to its own module. Adds test for hackernews module. --- hackertray/__init__.py | 9 +++------ hackertray/hackernews.py | 25 +++++++++++++++++++++++++ hackertray/hn_test.py | 10 ++++++++++ setup.py | 2 +- 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 hackertray/hackernews.py create mode 100644 hackertray/hn_test.py diff --git a/hackertray/__init__.py b/hackertray/__init__.py index d519c21..7a9994a 100644 --- a/hackertray/__init__.py +++ b/hackertray/__init__.py @@ -15,6 +15,8 @@ try: except ImportError: import appindicator_replacement as appindicator +from hackernews import HackerNews + class HackerNewsApp: def __init__(self): #Load the database @@ -103,7 +105,7 @@ class HackerNewsApp: '''Refreshes the menu ''' def refresh(self, widget=None, data=None): - data = reversed(getHomePage()[0:20]); + data = reversed(HackerNews.getHomePage()[0:20]); #Remove all the current stories for i in self.menu.get_children(): if(hasattr(i,'url')): @@ -114,11 +116,6 @@ class HackerNewsApp: #Call every 5 minutes gtk.timeout_add(5*60*1000, self.refresh) -'''Returns all the news stories from homepage''' -def getHomePage(): - r = requests.get('https://node-hnapi.herokuapp.com/news') - return r.json() - def main(): indicator = HackerNewsApp() indicator.run() diff --git a/hackertray/hackernews.py b/hackertray/hackernews.py new file mode 100644 index 0000000..5f3db10 --- /dev/null +++ b/hackertray/hackernews.py @@ -0,0 +1,25 @@ +import random +import requests + +urls = [ + 'http://node-hnapi-eu.herokuapp.com/', # Heroku (EU) + 'http://node-hnapi.azurewebsites.net/', # Windows Azure (North EU) + 'http://node-hnapi-asia.azurewebsites.net/', # Windows Azure (East Asia) + 'http://node-hnapi-eus.azurewebsites.net/', # Windows Azure (East US) + 'http://node-hnapi-weu.azurewebsites.net/', # Windows Azure (West EU) + 'http://node-hnapi-wus.azurewebsites.net/', # Windows Azure (West US) + 'http://node-hnapi-ncus.azurewebsites.net/' # Windows Azure (North Central US) +]; + +class HackerNews: + @staticmethod + def getHomePage(): + random.shuffle(urls) + for i in urls: + r = requests.get(i+"news") + try: + return r.json() + except ValueError: + continue; + finally: + print i \ No newline at end of file diff --git a/hackertray/hn_test.py b/hackertray/hn_test.py new file mode 100644 index 0000000..90debb3 --- /dev/null +++ b/hackertray/hn_test.py @@ -0,0 +1,10 @@ +import unittest +from hackernews import HackerNews + +class HNTest(unittest.TestCase): + def runTest(self): + data = HackerNews.getHomePage() + self.assertTrue(len(data)>0) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/setup.py b/setup.py index e00999f..7b9686f 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup(name='hackertray', - version='1.5', + version='1.6', description='Hacker News app that sits in your System Tray', long_description='HackerTray is a simple Hacker News Linux application that lets you view top HN stories in your System Tray. It relies on appindicator, so it is not guaranteed to work on all systems. It also provides a Gtk StatusIcon fallback in case AppIndicator is not available.', keywords='hacker news hn tray system tray icon hackertray',