db_router.py 3.4 KB

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