def get_all_stories(): """hackernews json data""" data = list() # get the page (every page has 30 stories) try: page = int(request.GET.get('page', '').strip()) except ValueError: page = 1 # how many stories we want for the page try: counter = int(request.GET.get('stories', '').strip()) except ValueError: counter = 30 # we check if we want to see the new stories if request.GET.get('new', '').strip() == 'true': new = True else: new = False # we finally fetch the stories stories = get_stories(page, new) # iterate over the stories and add them to the list for story in stories[:counter]: data.append(story.__dict__) # needed to do this, because data is a list and bottle # supports autojson with dicts only response.content_type = 'application/json' return json.dumps(data)
def run(self): self.running = True while self.running: try: retr_queue_out.put(StartGettingData()); stories = get_stories(self.pages, self.new) retr_queue_out.put(FinishedGettingData()) retr_queue_out.put(Story(stories)) if self.old_pages != self.pages: self.old_pages = self.pages retr_queue_out.put(ChangedPages()) except RedesignError, e: retr_queue_out.put(FinishedGettingData()) retr_queue_out.put(DisplayError("Hacker News might have redesigned:", str(e))) return except SeriousError, e: retr_queue_out.put(FinishedGettingData()) retr_queue_out.put(DisplayError("Serious error:", str(e))) return