poi.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """POI相关API路由"""
  2. from fastapi import APIRouter, HTTPException
  3. from pydantic import BaseModel, Field
  4. from typing import List, Optional
  5. from ...services.amap_service import get_amap_service
  6. from ...services.unsplash_service import get_unsplash_service
  7. router = APIRouter(prefix="/poi", tags=["POI"])
  8. class POIDetailResponse(BaseModel):
  9. """POI详情响应"""
  10. success: bool
  11. message: str
  12. data: Optional[dict] = None
  13. @router.get(
  14. "/detail/{poi_id}",
  15. response_model=POIDetailResponse,
  16. summary="获取POI详情",
  17. description="根据POI ID获取详细信息,包括图片"
  18. )
  19. async def get_poi_detail(poi_id: str):
  20. """
  21. 获取POI详情
  22. Args:
  23. poi_id: POI ID
  24. Returns:
  25. POI详情响应
  26. """
  27. try:
  28. amap_service = get_amap_service()
  29. # 调用高德地图POI详情API
  30. result = amap_service.get_poi_detail(poi_id)
  31. return POIDetailResponse(
  32. success=True,
  33. message="获取POI详情成功",
  34. data=result
  35. )
  36. except Exception as e:
  37. print(f"❌ 获取POI详情失败: {str(e)}")
  38. raise HTTPException(
  39. status_code=500,
  40. detail=f"获取POI详情失败: {str(e)}"
  41. )
  42. @router.get(
  43. "/search",
  44. summary="搜索POI",
  45. description="根据关键词搜索POI"
  46. )
  47. async def search_poi(keywords: str, city: str = "北京"):
  48. """
  49. 搜索POI
  50. Args:
  51. keywords: 搜索关键词
  52. city: 城市名称
  53. Returns:
  54. 搜索结果
  55. """
  56. try:
  57. amap_service = get_amap_service()
  58. result = amap_service.search_poi(keywords, city)
  59. return {
  60. "success": True,
  61. "message": "搜索成功",
  62. "data": result
  63. }
  64. except Exception as e:
  65. print(f"❌ 搜索POI失败: {str(e)}")
  66. raise HTTPException(
  67. status_code=500,
  68. detail=f"搜索POI失败: {str(e)}"
  69. )
  70. @router.get(
  71. "/photo",
  72. summary="获取景点图片",
  73. description="根据景点名称从Unsplash获取图片"
  74. )
  75. async def get_attraction_photo(name: str):
  76. """
  77. 获取景点图片
  78. Args:
  79. name: 景点名称
  80. Returns:
  81. 图片URL
  82. """
  83. try:
  84. unsplash_service = get_unsplash_service()
  85. # 搜索景点图片
  86. photo_url = unsplash_service.get_photo_url(f"{name} China landmark")
  87. if not photo_url:
  88. # 如果没找到,尝试只用景点名称搜索
  89. photo_url = unsplash_service.get_photo_url(name)
  90. return {
  91. "success": True,
  92. "message": "获取图片成功",
  93. "data": {
  94. "name": name,
  95. "photo_url": photo_url
  96. }
  97. }
  98. except Exception as e:
  99. print(f"❌ 获取景点图片失败: {str(e)}")
  100. raise HTTPException(
  101. status_code=500,
  102. detail=f"获取景点图片失败: {str(e)}"
  103. )