chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
using api I need to get the 20 (configurable) top posts from hackernews with votes…
- User
using api I need to get the 20 (configurable) top posts from hackernews with votes descending
- 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
- Get the list of top story IDs.
- Fetch details for each ID in the list.
- Sort by
score(i.e., votes) in descending order. - Select the top
Nitems (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
scorefield in each item represents votes (upvotes). - Hacker News rank is not necessarily based on
scorealone; 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!
- Base URL:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.