|
| 1 | +import urllib.parse |
1 | 2 | import logging
|
2 | 3 | from typing import List
|
3 |
| -from fastapi import APIRouter, Query, Response |
| 4 | +from fastapi import APIRouter, Query |
| 5 | +from fastapi.responses import HTMLResponse |
4 | 6 |
|
5 | 7 | import random
|
6 | 8 |
|
7 |
| -router = APIRouter(tags=["tool"], prefix="/api/tool") |
| 9 | +router = APIRouter(tags=["tool"], prefix="/tool") |
8 | 10 |
|
9 | 11 | logger = logging.getLogger(__file__)
|
10 | 12 |
|
11 | 13 |
|
12 | 14 | @router.get("/eat", summary="今天吃什么")
|
13 | 15 | def eat(choices: List[str] = Query(..., description="选择列表, 随机返回一个")):
|
14 |
| - return Response( |
15 |
| - content=f"今天吃 {random.choice(choices)} !", headers={"content-type": "text/plain; charset=utf-8"} |
16 |
| - ) |
| 16 | + select = random.choice(choices) |
| 17 | + meituanwaimai = urllib.parse.quote(f"meituanwaimai://waimai.meituan.com/search?query={select}") |
| 18 | + meituanwaimai_href = f"https://p.19940731.xyz/api/network/url/redirect?url={meituanwaimai}" |
| 19 | + |
| 20 | + eleme = f"eleme://search?keyword={select}" |
| 21 | + eleme_href = f"https://p.19940731.xyz/api/network/url/redirect?url={eleme}" |
| 22 | + |
| 23 | + dianping = f"dianping://searchshoplist?keyword={select}" |
| 24 | + dianping_href = f"https://p.19940731.xyz/api/network/url/redirect?url={dianping}" |
| 25 | + body = f""" |
| 26 | + <!DOCTYPE html> |
| 27 | + <html lang="zh"> |
| 28 | + <head> |
| 29 | + <meta charset="UTF-8"> |
| 30 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 31 | + <title></title> |
| 32 | + <style> |
| 33 | + .container {{ |
| 34 | + text-align: center; /* 居中对齐 */ |
| 35 | + margin-top: 50px; /* 上边距 */ |
| 36 | + }} |
| 37 | + .main-text {{ |
| 38 | + font-size: 24px; /* 大一点的文本 */ |
| 39 | + }} |
| 40 | + .link {{ |
| 41 | + font-size: 14px; /* 小一点的链接文本 */ |
| 42 | + display: inline-block; /* 使链接在一行显示 */ |
| 43 | + margin: 0 10px; /* 左右间距 */ |
| 44 | + }} |
| 45 | + </style> |
| 46 | + </head> |
| 47 | + <body> |
| 48 | + <div class="container"> |
| 49 | + <p class="main-text">今天吃 {select} !</p> |
| 50 | + <a href="{meituanwaimai_href}" class="link">点击跳转到美团外卖</a> |
| 51 | + <br> |
| 52 | + <br> |
| 53 | + <a href="{eleme_href}" class="link">点击跳转到饿了么</a> |
| 54 | + <br> |
| 55 | + <br> |
| 56 | + <a href="{dianping_href}" class="link">点击跳转到大众点评</a> |
| 57 | + </div> |
| 58 | + </body> |
| 59 | + </html> |
| 60 | + """ |
| 61 | + return HTMLResponse(content=body, headers={"content-type": "text/html; charset=UTF-8"}) |
0 commit comments