-
-
Save ilovejs/ffcdd969bf0fb5c506128b4ae394c196 to your computer and use it in GitHub Desktop.
Write once run every where
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""An example of writing an API to scrape hacker news once, and then enabling usage everywhere""" | |
import hug | |
import requests | |
@hug.local() | |
@hug.cli() | |
@hug.get() | |
def top_post(section: hug.types.one_of(('news', 'newest', 'show'))='news'): | |
"""Returns the top post from the provided section""" | |
content = requests.get('https://news.ycombinator.com/{0}'.format(section)).content | |
text = content.decode('utf-8') | |
return text.split('<tr class=\'athing\'>')[1].split("<a href")[1].split(">")[1].split("<")[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment