File: write_once.py

package info (click to toggle)
python-hug 2.6.0-2.4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,072 kB
  • sloc: python: 8,938; sh: 99; makefile: 17
file content (13 lines) | stat: -rw-r--r-- 515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
"""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]