Skip to content

Commit 1bc85e6

Browse files
committed
Impersonate Chrome
1 parent ab80d94 commit 1bc85e6

4 files changed

Lines changed: 95 additions & 11 deletions

File tree

Justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
default:
2+
@just --list
3+
4+
server:
5+
uv run manage.py runserver 8003

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = "~=3.14.0"
77
dependencies = [
8+
"curl-cffi",
89
"django",
910
"django-admin-sortable2",
1011
"django-environ",

surfline/urls.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import json
33
from datetime import UTC, datetime
44

5-
import httpx
65
import stamina
6+
from curl_cffi import AsyncSession
7+
from curl_cffi.requests.exceptions import HTTPError, RequestException
78
from django.shortcuts import render
89

910
from 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

Comments
 (0)