You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB
Python

1 year ago
#!/usr/bin/env python3
import requests
import asyncio
1 year ago
from dataclasses import dataclass
@dataclass
class Story:
id: int
title: str
link: str
author: str
votes: int
comments: int
read: bool
1 year ago
def get_topstories():
# Query Hacker News API for top stories and their titles/links
# returns the top 500 stories, so quite enough.
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
r = requests.get(url)
if not r.ok:
raise Exception('Error fetching data from Hacker News API')
return r.json()
async def get_story(session, story_id):
async with session.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json') as resp:
s = await resp.json() #requests.get(story_url).json()
return Story(s['id'],
s['title'],
s['url'] if 'url' in s else 'No URL',
s['by'],
s['score'],
s['descendants'] if 'descendants' in s else 0, False)
1 year ago
def main():
ts = get_topstories()
s = get_story(ts[0])
print(s)
if __name__ == '__main__':
main()