Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

using api I need to get the 20 (configurable) top posts from hackernews with votes…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-sonnet-20241022’s answer.
  1. User

    using api I need to get the 20 (configurable) top posts from hackernews with votes descending

  2. Assistant

    To get the top 20 posts (configurable) from Hacker News in descending order by votes using the Hacker News API, you can follow this approach:


    🔧 API Overview

    Hacker News has a free public Firebase API:

    • Base URL: https://hacker-news.firebaseio.com/v0/

    Relevant endpoints:

    • topstories.json: Returns an array of IDs of the current top stories.
    • item/{id}.json: Returns the details of a story/item by its ID.

    ✅ Steps

    1. Get the list of top story IDs.
    2. Fetch details for each ID in the list.
    3. Sort by score (i.e., votes) in descending order.
    4. Select the top N items (e.g., 20).

    🧪 Example: Python Script using Requests

    import requests
    from concurrent.futures import ThreadPoolExecutor
    
    HN_API = 'https://hacker-news.firebaseio.com/v0'
    N = 20  # Number of top posts you want
    
    def get_top_story_ids():
        response = requests.get(f'{HN_API}/topstories.json')
        return response.json()
    
    def get_item(item_id):
        response = requests.get(f'{HN_API}/item/{item_id}.json')
        if response.status_code == 200:
            return response.json()
        return None
    
    def get_top_stories_by_votes(n=N):
        top_ids = get_top_story_ids()
        
        # Limit the number of fetched posts (e.g., fetch more than needed in case scores are similar)
        fetch_limit = min(len(top_ids), n * 4)
    
        # Fetch stories concurrently
        with ThreadPoolExecutor(max_workers=20) as executor:
            items = list(executor.map(get_item, top_ids[:fetch_limit]))
    
        # Filter out None responses and non-story types
        stories = [item for item in items if item and item.get('type') == 'story']
    
        # Sort by score descending
        top_stories = sorted(stories, key=lambda x: x.get('score', 0), reverse=True)
    
        return top_stories[:n]
    
    # Display output
    if __name__ == '__main__':
        top_posts = get_top_stories_by_votes()
        for idx, post in enumerate(top_posts, 1):
            print(f"{idx}. ({post['score']} votes) {post['title']} - {post['url']}")
    

    📌 Example Output

    1. (582 votes) ChatGPT has an API now - https://openai.com/blog/chatgpt-api
    2. (531 votes) Show HN: I built a terminal UI for GitHub - https://github.com/...
    ...
    

    ✅ Notes

    • The score field in each item represents votes (upvotes).
    • Hacker News rank is not necessarily based on score alone; age and activity play a role too. But since you want to sort by votes, this manual method works well.
    • Use concurrency/threading to speed up fetching multiple item details.

    🧩 Let me know if you'd like this example in another language (Node.js, Go, etc.) or need it wrapped in a REST API!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.