Skip to main content

Every web site provides APIs.

Project description

Toapi

Build Coverage Python Version License

Overview

Toapi give you the ability to make every web site provides APIs.

Features

  • Automatic converting HTML web site to API service.
  • Automatic caching every page of source site.
  • Automatic caching every request.
  • Support merging multiple web sites into one API service.

Get Started

Installation

$ pip install toapi

Usage

create app.py and copy the code:

from flask import request
from htmlparsing import Attr, Text
from toapi import Api, Item

api = Api()


@api.site('https://news.ycombinator.com')
@api.list('.athing')
@api.route('/posts?page={page}', '/news?p={page}')
@api.route('/posts', '/news?p=1')
class Post(Item):
    url = Attr('.storylink', 'href')
    title = Text('.storylink')


@api.site('https://news.ycombinator.com')
@api.route('/posts?page={page}', '/news?p={page}')
@api.route('/posts', '/news?p=1')
class Page(Item):
    next_page = Attr('.morelink', 'href')

    def clean_next_page(self, value):
        return api.convert_string('/' + value, '/news?p={page}', request.host_url.strip('/') + '/posts?page={page}')


api.run(debug=True, host='0.0.0.0', port=5000)

run python app.py

then open your browser and visit http://127.0.0.1:5000/posts?page=1

you will get the result like:

{
  "Page": {
    "next_page": "http://127.0.0.1:5000/posts?page=2"
  }, 
  "Post": [
    {
      "title": "Mathematicians Crack the Cursed Curve", 
      "url": "https://www.quantamagazine.org/mathematicians-crack-the-cursed-curve-20171207/"
    }, 
    {
      "title": "Stuffing a Tesla Drivetrain into a 1981 Honda Accord", 
      "url": "https://jalopnik.com/this-glorious-madman-stuffed-a-p85-tesla-drivetrain-int-1823461909"
    }
  ]
}

Contributing

Write code and test code and pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toapi-2.2.0.tar.gz (429.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toapi-2.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file toapi-2.2.0.tar.gz.

File metadata

  • Download URL: toapi-2.2.0.tar.gz
  • Upload date:
  • Size: 429.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for toapi-2.2.0.tar.gz
Algorithm Hash digest
SHA256 2472eee171226120664abea90286acfe4661cf0aa1ad82741432087d8b705de2
MD5 32f462a68857ea82d81e0344ad74ae62
BLAKE2b-256 f6629ec85b7b92981192ca3da2f157c0f04b89437e026ead92385cd99714fd62

See more details on using hashes here.

File details

Details for the file toapi-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: toapi-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for toapi-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 396eacd203fde9bbb78a70cb29c9c54d5f7afdb12e0348ad14f64ee1fb995ab6
MD5 6356c601e989107c4fef196d7280d023
BLAKE2b-256 3a28a8de4bab04860732b4a601f1cee8e6d334e3cb096c7c19cc66c530dbd016

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page