Created
April 12, 2015 03:52
-
-
Save adrianmoses/80cfa0ba521e59b607b3 to your computer and use it in GitHub Desktop.
Uses HN Firebase API to make very simple command line interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import webbrowser | |
import requests | |
STORY_COUNT = 10 | |
def get_stories(story_ids): | |
stories = [] | |
for story_id in story_ids: | |
res = requests.get( | |
"https://hacker-news.firebaseio.com/v0/item/%s.json" \ | |
% story_id, | |
params={ | |
'print': 'pretty' | |
}) | |
if res.status_code == 200: | |
stories.append(res.json()) | |
return dict(enumerate(stories)) | |
def run(): | |
res = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json", | |
params={'print': 'pretty'}) | |
if res.status_code == 200: | |
top_stories = res.json() | |
stories = get_stories(top_stories[:STORY_COUNT]) | |
for index, story in stories.iteritems(): | |
print "%s) %s" % (index, story['title']) | |
while True: | |
story_index = raw_input("Choose story to open by number:") | |
try: | |
story_index = int(story_index) | |
except ValueError: | |
print "Must be a number" | |
story = stories.get(story_index, None) | |
if story: | |
webbrowser.open(story['url']) | |
else: | |
print "No story at index %s exists" % story_index | |
if __name__ == "__main__": | |
run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment