22import json
33from datetime import UTC , datetime
44
5- import httpx
65import stamina
6+ from curl_cffi import AsyncSession
7+ from curl_cffi .requests .exceptions import HTTPError , RequestException
78from django .shortcuts import render
89
910from cams .models import Cam
@@ -14,11 +15,11 @@ async def get_surfline_data(request, cam_id: int):
1415 cam = await Cam .objects .aget (id = cam_id )
1516 except Cam .DoesNotExist :
1617 return render (request , "surfline-error.html" , {"message" : "Cam not found" })
17- async with httpx . AsyncClient ( ) as client :
18+ async with AsyncSession ( impersonate = "chrome" ) as client :
1819 fetcher = SurflineFetcher (cam .spot_id , client )
1920 try :
2021 tides , sunlight , wind , waves = await fetcher .fetch_all ()
21- except httpx . HTTPError :
22+ except RequestException :
2223 return render (request , "surfline-error.html" , {"cam" : cam })
2324 # Group wind/wave data by day
2425 forecast_days = []
@@ -90,15 +91,15 @@ def __init__(self, spot_id: str, client):
9091 self .day_params = {"spotId" : spot_id , "days" : 3 }
9192 self .spot_id = spot_id
9293
93- @stamina .retry (on = httpx . HTTPError , attempts = 3 )
94+ @stamina .retry (on = RequestException , attempts = 3 )
9495 async def fetch_tides (self ):
9596 tide_response = await self .client .get (
9697 self .base_url + "tides" ,
9798 timeout = 5.0 ,
9899 params = {"spotId" : self .spot_id , "days" : 4 },
99100 )
100101 if tide_response .status_code != 200 :
101- raise httpx . HTTPError ("Non-200 response" )
102+ raise HTTPError ("Non-200 response" )
102103 unit = tide_response .json ()["associated" ]["units" ]["tideHeight" ].lower ()
103104 today = datetime .now (UTC ).date ()
104105 chart_points = []
@@ -136,15 +137,15 @@ async def fetch_tides(self):
136137 "unit" : unit ,
137138 }
138139
139- @stamina .retry (on = httpx . HTTPError , attempts = 3 )
140+ @stamina .retry (on = RequestException , attempts = 3 )
140141 async def fetch_sunlight (self ):
141142 sunlight_response = await self .client .get (
142143 self .base_url + "sunlight" ,
143144 timeout = 5.0 ,
144145 params = self .day_params ,
145146 )
146147 if sunlight_response .status_code != 200 :
147- raise httpx . HTTPError ("Non-200 response" )
148+ raise HTTPError ("Non-200 response" )
148149
149150 today = datetime .now (UTC ).date ()
150151 chart_data = []
@@ -181,15 +182,15 @@ def to_minutes(ts, offset):
181182 "chart_data" : chart_data ,
182183 }
183184
184- @stamina .retry (on = httpx . HTTPError , attempts = 3 )
185+ @stamina .retry (on = RequestException , attempts = 3 )
185186 async def fetch_wind (self ):
186187 wind_response = await self .client .get (
187188 self .base_url + "wind" ,
188189 timeout = 5.0 ,
189190 params = self .day_params ,
190191 )
191192 if wind_response .status_code != 200 :
192- raise httpx . HTTPError ("Non-200 response" )
193+ raise HTTPError ("Non-200 response" )
193194 res = []
194195 data = wind_response .json ()["data" ]["wind" ]
195196 prev_hour = 24
@@ -239,15 +240,15 @@ class Colors:
239240 )
240241 return res
241242
242- @stamina .retry (on = httpx . HTTPError , attempts = 3 )
243+ @stamina .retry (on = RequestException , attempts = 3 )
243244 async def fetch_waves (self ):
244245 wave_response = await self .client .get (
245246 self .base_url + "wave" ,
246247 timeout = 5.0 ,
247248 params = self .day_params ,
248249 )
249250 if wave_response .status_code != 200 :
250- raise httpx . HTTPError ("Non-200 response" )
251+ raise HTTPError ("Non-200 response" )
251252 res = []
252253 data = wave_response .json ()["data" ]["wave" ]
253254 prev_hour = 24
0 commit comments