class CacheManager(object): def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: self.config = Config() self.cache_path = self.config.parser.get('settings', 'cache') self.cache_age = int(self.config.parser.get('settings', 'cache_age')) self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh() def is_outdated(self, which="top"): if not os.path.exists(self.cache_path): return True cache = pickle.load(open(self.cache_path, 'rb')) if not cache.get(which, False): return True cache_age = datetime.datetime.today() - cache[which]['date'] if cache_age.seconds > self.cache_age * 60: return True else: return False def refresh(self, which="top"): if which == "top": stories = self.api.getTopStories() elif which == "newest": stories = self.api.getNewestStories() elif which == "best": stories = self.api.getBestStories() else: raise Exception('Bad value: top, newest and best stories') cache = {} if os.path.exists(self.cache_path): cache = pickle.load(open(self.cache_path, 'rb')) cache[which] = {'stories': stories, 'date': datetime.datetime.today()} pickle.dump(cache, open(self.cache_path, 'wb')) def get_stories(self, which="top"): cache = [] if os.path.exists(self.cache_path): cache = pickle.load(open(self.cache_path, 'rb')) if not cache.get(which, False): return [] else: return cache[which]['stories']
def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: self.config = Config() self.cache_path = self.config.parser.get('settings', 'cache') self.cache_age = int(self.config.parser.get('settings', 'cache_age')) self.extra_page = int(self.config.parser.get('settings', 'extra_page')) self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh()
def refreshKarma(self): """ Gets the karma count of a user from the source of their 'user' page. """ hn = HackerNewsAPI() source = hn.getSource(self.userPageURL) karmaStart = source.find('<td valign=top>karma:</td><td>') + 30 karmaEnd = source.find('</td>', karmaStart) karma = source[karmaStart:karmaEnd] if karma is not '': self.karma = int(karma) else: raise HNException("Error getting karma for user " + self.name)
class cacheManager(object): def __init__(self, path=cache_path): self.path = path self.api = HackerNewsAPI() if not os.path.exists(self.path): self.refresh() def is_outdated(self, which="top"): if not os.path.exists(self.path): return True cache = pickle.load(open(self.path)) if not cache.get(which, False): return True cache_age = datetime.datetime.today() - cache[which]['date'] if cache_age.seconds > 5*60: return True else: return False def refresh(self, which="top"): if which == "top": stories = self.api.getTopStories() elif which == "newest": stories = self.api.getNewestStories() elif which == "best": stories = self.api.getBestStories() else: raise Exception('Bad choice, can only refresh: top, newest and best stories') cache = {} if os.path.exists(self.path): cache = pickle.load(open(self.path)) cache[which] = {'stories':stories, 'date':datetime.datetime.today()} pickle.dump(cache, open(self.path, 'w')) def get_stories(self, which="top"): cache = [] if os.path.exists(self.path): cache = pickle.load(open(self.path)) if not cache.get(which, False): return [] else: return cache[which]['stories']
def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: config = Config() self.cache_path = config.parser.get('settings', 'cache') self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh()
def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: self.config = Config() self.cache_path = self.config.parser.get("settings", "cache") self.cache_age = int(self.config.parser.get("settings", "cache_age")) self.extra_page = int(self.config.parser.get("settings", "extra_page")) self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh()
class CacheManager(object): def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: self.config = Config() self.cache_path = self.config.parser.get('settings', 'cache') self.cache_age = int(self.config.parser.get('settings', 'cache_age')) self.extra_page = int(self.config.parser.get('settings', 'extra_page')) self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh() def is_outdated(self, which="top"): if not os.path.exists(self.cache_path): return True try: cache = pickle.load(open(self.cache_path, 'rb')) except: cache = {} if not cache.get(which, False): return True cache_age = datetime.datetime.today() - cache[which]['date'] if cache_age.seconds > self.cache_age * 60: return True else: return False def refresh(self, which="top"): if which == "top": stories = self.api.getTopStories(extra_page=self.extra_page) elif which == "newest": stories = self.api.getNewestStories(extra_page=self.extra_page) elif which == "best": stories = self.api.getBestStories(extra_page=self.extra_page) else: raise Exception('Bad value: top, newest and best stories') cache = {} if os.path.exists(self.cache_path): try: cache = pickle.load(open(self.cache_path, 'rb')) except: pass cache[which] = {'stories': stories, 'date': datetime.datetime.today()} pickle.dump(cache, open(self.cache_path, 'wb')) def get_stories(self, which="top"): cache = [] if os.path.exists(self.cache_path): try: cache = pickle.load(open(self.cache_path, 'rb')) except: cache = {} if not cache.get(which, False): return [] else: return cache[which]['stories']
(widget, foo) = self._listbox.get_focus() (text, foo) = widget.get_text() self.selected = text[1:] # Get rid of the leading space... else: return self._listbox.keypress(size, key) ########NEW FILE######## __FILENAME__ = get_comments #!/usr/bin/env python # -*- coding: utf-8 -*- import os import json from hnapi import HackerNewsAPI hn = HackerNewsAPI() stories = hn.getTopStories() story = stories[0] if not os.path.exists('comments.data'): comments = story.getComments() open('comments.data', 'w').write(json.dumps(comments)) else: comments = json.load(open('comments.data')) ########NEW FILE######## __FILENAME__ = treesample #!/usr/bin/python # # Trivial data browser # This version:
class CacheManager(object): def __init__(self, cache_path=None): self.cache_path = cache_path if cache_path is None: self.config = Config() self.cache_path = self.config.parser.get("settings", "cache") self.cache_age = int(self.config.parser.get("settings", "cache_age")) self.extra_page = int(self.config.parser.get("settings", "extra_page")) self.api = HackerNewsAPI() if not os.path.exists(self.cache_path): self.refresh() def is_outdated(self, which="top"): if not os.path.exists(self.cache_path): return True try: cache = pickle.load(open(self.cache_path, "rb")) except: cache = {} if not cache.get(which, False): return True cache_age = datetime.datetime.today() - cache[which]["date"] if cache_age.seconds > self.cache_age * 60: return True else: return False def refresh(self, which="top"): if which == "top": stories = self.api.getTopStories(extra_page=self.extra_page) elif which == "newest": stories = self.api.getNewestStories(extra_page=self.extra_page) elif which == "best": stories = self.api.getBestStories(extra_page=self.extra_page) else: raise Exception("Bad value: top, newest and best stories") cache = {} if os.path.exists(self.cache_path): try: cache = pickle.load(open(self.cache_path, "rb")) except: pass cache[which] = {"stories": stories, "date": datetime.datetime.today()} pickle.dump(cache, open(self.cache_path, "wb")) def get_stories(self, which="top"): cache = [] if os.path.exists(self.cache_path): try: cache = pickle.load(open(self.cache_path, "rb")) except: cache = {} if not cache.get(which, False): return [] else: return cache[which]["stories"]
if key == "enter": (widget, foo) = self._listbox.get_focus() (text, foo) = widget.get_text() self.selected = text[1:] # Get rid of the leading space... else: return self._listbox.keypress(size, key) ########NEW FILE######## __FILENAME__ = get_comments #!/usr/bin/env python # -*- coding: utf-8 -*- import os import json from hnapi import HackerNewsAPI hn = HackerNewsAPI() stories = hn.getTopStories() story = stories[0] if not os.path.exists('comments.data'): comments = story.getComments() open('comments.data', 'w').write(json.dumps(comments)) else: comments = json.load(open('comments.data')) ########NEW FILE######## __FILENAME__ = treesample #!/usr/bin/python # # Trivial data browser
def __init__(self, path=cache_path): self.path = path self.api = HackerNewsAPI() if not os.path.exists(self.path): self.refresh()