db_router.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from fastapi import APIRouter
  2. from supabase import create_client, Client
  3. from dotenv import load_dotenv
  4. import os
  5. from datetime import datetime
  6. from random import choice
  7. from openai import OpenAI
  8. client = OpenAI()
  9. load_dotenv()
  10. # supaspace 連線
  11. url: str = os.environ.get('SUPABASE_URL')
  12. key: str = os.environ.get('SUPABASE_KEY')
  13. supabase: Client = create_client(url, key)
  14. dbRouter = APIRouter()
  15. @dbRouter.get("/click")
  16. def add_click_time():
  17. try:
  18. response = supabase.table('click_time').select("*").execute()
  19. click_time = response.data[0]['click_time'] + 1
  20. data, count = supabase.table('click_time') \
  21. .update({'click_time':click_time,'update_time':str(datetime.now())})\
  22. .eq('id', 1)\
  23. .execute()
  24. return {"state":"success","click_time" : click_time}
  25. except Exception as e:
  26. return {"state":str(e)}
  27. @dbRouter.get("/find_brand")
  28. def find_brand(keyword:str = None,language :str = "ch"):
  29. if keyword is None :
  30. return {"state":"fail","message" : "no keyword"}
  31. try :
  32. data, count = supabase.table('101_brand')\
  33. .select('*')\
  34. .eq("language", language)\
  35. .like('tags', f'%{keyword}%')\
  36. .execute()
  37. result = []
  38. for shop in data[1] :
  39. json = {
  40. "type" : shop["type"],
  41. "info" : shop
  42. }
  43. result.append(json)
  44. return {"state":"success","data" : result}
  45. except Exception as e:
  46. return {"state":"fail","message" :str(e)}
  47. @dbRouter.get("/arviews")
  48. def arviews(start:str,end:str):
  49. try :
  50. data, count = supabase.table('101_arviews')\
  51. .select('*')\
  52. .eq('start_loc', start) \
  53. .like('tour_place', f'%{end}%') \
  54. .execute()
  55. result :str
  56. words :str
  57. if len(data[1]) != 0:
  58. result = data[1][0]["url"]
  59. words = data[1][0]["words"]
  60. else :
  61. result = "no this route"
  62. return {"state":"success","url" : result,"words" : words}
  63. except Exception as e:
  64. return {"state":"fail","message" :str(e)}
  65. @dbRouter.get("/static_tickets")
  66. async def static_tickets(is_Chinese : int = None):
  67. try:
  68. data =None
  69. if is_Chinese :
  70. data, count = supabase.table('101_ticket')\
  71. .select('*')\
  72. .in_('id', [1,3,6,7])\
  73. .execute()
  74. else :
  75. data, count = supabase.table('101_ticket')\
  76. .select('*')\
  77. .in_('id', [182,183,180])\
  78. .execute()
  79. result = []
  80. for shop in data[1] :
  81. json = {
  82. "type" : shop["type"],
  83. "info" : shop
  84. }
  85. result.append(json)
  86. return {"state":"success","result" : result}
  87. except Exception as e:
  88. return {"state":"fail","message" :str(e)}
  89. @dbRouter.get("/ad/{type}")
  90. def read_root(type:str,language :str = "ch"):
  91. keyword1 :str
  92. keyword2 :str
  93. if type == "美食伴手禮":
  94. keyword1 = "餐飲"
  95. keyword2 = "伴手禮"
  96. else :
  97. keyword1 = "住宿"
  98. keyword2 = "伴手禮"
  99. data, count = supabase.table('101_brand')\
  100. .select('*')\
  101. .eq("floor","館外")\
  102. .eq("language", language)\
  103. .or_(f"tags.ilike.%{keyword1}%,tags.ilike.%{keyword2}%")\
  104. .execute()
  105. result = data[1]
  106. # 從結果中隨機選擇一筆資料
  107. random_row = choice(result)
  108. #print(random_row)
  109. return {"data": random_row}