You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "5ed19dc5-4b2a-45b8-8b73-8ad1f787ad84", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"from flask import request\n", |
|
"from htmlparsing import Attr, Text\n", |
|
"from toapi import Api, Item\n", |
|
"\n", |
|
"api = Api()\n", |
|
"\n", |
|
"\n", |
|
"@api.site(\"https://news.ycombinator.com\")\n", |
|
"@api.list(\".athing\")\n", |
|
"@api.route(\"/posts?page={page}\", \"/news?p={page}\")\n", |
|
"@api.route(\"/posts\", \"/news?p=1\")\n", |
|
"class Post(Item):\n", |
|
" url = Attr(\".storylink\", \"href\")\n", |
|
" title = Text(\".storylink\")\n", |
|
"\n", |
|
"\n", |
|
"@api.site(\"https://news.ycombinator.com\")\n", |
|
"@api.route(\"/posts?page={page}\", \"/news?p={page}\")\n", |
|
"@api.route(\"/posts\", \"/news?p=1\")\n", |
|
"class Page(Item):\n", |
|
" next_page = Attr(\".morelink\", \"href\")\n", |
|
"\n", |
|
" def clean_next_page(self, value):\n", |
|
" return api.convert_string(\n", |
|
" \"/\" + value,\n", |
|
" \"/news?p={page}\",\n", |
|
" request.host_url.strip(\"/\") + \"/posts?page={page}\",\n", |
|
" )\n", |
|
"\n", |
|
"\n", |
|
"api.run(debug=True, host=\"0.0.0.0\", port=5123)" |
|
] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "Python 3 (ipykernel)", |
|
"language": "python", |
|
"name": "python3" |
|
}, |
|
"language_info": { |
|
"codemirror_mode": { |
|
"name": "ipython", |
|
"version": 3 |
|
}, |
|
"file_extension": ".py", |
|
"mimetype": "text/x-python", |
|
"name": "python", |
|
"nbconvert_exporter": "python", |
|
"pygments_lexer": "ipython3", |
|
"version": "3.9.10" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
}
|
|
|