def grab_best_comments(): comment_count = 5 if 'len' in request.args: comment_count = int(request.args.get("len")) articles = retrieve_newest_articles() # finds all comments and sorts them comments = Comment.find_comments(articles) sorted_comments = sorted(comments, key=lambda obj: (obj.ratio, obj.likes), reverse=True) return jsonify(Comment.as_dicts(sorted_comments[:comment_count]))
def as_dict(self): return { 'Link': self.link, 'Header': self.header, 'Description': self.description, 'Category': self.category, 'Author': self.author, 'Published_at': self.published_at, 'Modified_at': self.modified_at, 'Paragraphs': self.paragraphs, 'Paragraphs_count': len(self.paragraphs), 'Comments': json.dumps(Comment.as_dicts(self.comments), ensure_ascii=False) }