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.
This commit is contained in:
Abhay Rana 2013-11-30 01:16:39 +05:30
parent 306415f8d9
commit 8f0b08137b
4 changed files with 39 additions and 7 deletions

View File

@ -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()

25
hackertray/hackernews.py Normal file
View File

@ -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

10
hackertray/hn_test.py Normal file
View File

@ -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()

View File

@ -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',