bastard
This commit is contained in:
28
display.py
28
display.py
@@ -59,11 +59,13 @@ _weather: dict = {} # keys: 'week' (list), 'hours' (list), 'location' (
|
|||||||
_weather_lock = threading.Lock()
|
_weather_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def _postcode_to_latlon(postcode: str) -> tuple[float, float]:
|
def _postcode_to_latlon(postcode: str) -> tuple[float, float, str]:
|
||||||
url = 'https://api.postcodes.io/postcodes/' + urllib.parse.quote(postcode.replace(' ', ''))
|
url = 'https://api.postcodes.io/postcodes/' + urllib.parse.quote(postcode.replace(' ', ''))
|
||||||
with urllib.request.urlopen(url, timeout=10) as r:
|
with urllib.request.urlopen(url, timeout=10) as r:
|
||||||
data = json.loads(r.read())
|
data = json.loads(r.read())
|
||||||
return data['result']['latitude'], data['result']['longitude']
|
result = data['result']
|
||||||
|
town = result.get('admin_district', postcode).split(',')[0].strip()
|
||||||
|
return result['latitude'], result['longitude'], town
|
||||||
|
|
||||||
|
|
||||||
def _fetch_weather(lat: float, lon: float) -> None:
|
def _fetch_weather(lat: float, lon: float) -> None:
|
||||||
@@ -187,7 +189,7 @@ def draw_clock(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
|||||||
# ── Weather panel ─────────────────────────────────────────────────────────────
|
# ── Weather panel ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
_WEATHER_COLS = 5
|
_WEATHER_COLS = 5
|
||||||
_WEATHER_COL_W = 66 # px per column — 5 cols = 330px total
|
_WEATHER_COL_W = 84 # px per column — 5 cols = 420px total
|
||||||
|
|
||||||
|
|
||||||
def draw_weather(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
def draw_weather(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
||||||
@@ -197,8 +199,9 @@ def draw_weather(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
|||||||
|
|
||||||
th = fonts['tiny'].get_height()
|
th = fonts['tiny'].get_height()
|
||||||
row = th + 2
|
row = th + 2
|
||||||
|
town = _weather.get('town', '')
|
||||||
|
|
||||||
def _trunc(s, n=11):
|
def _trunc(s, n=13):
|
||||||
return s if len(s) <= n else s[:n-1] + '…'
|
return s if len(s) <= n else s[:n-1] + '…'
|
||||||
|
|
||||||
def _col(items, start_y, label_fn, row2_fn, row3_fn):
|
def _col(items, start_y, label_fn, row2_fn, row3_fn):
|
||||||
@@ -208,13 +211,17 @@ def draw_weather(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
|||||||
surface.blit(fonts['tiny'].render(row2_fn(item), True, WHITE), (cx, start_y + row))
|
surface.blit(fonts['tiny'].render(row2_fn(item), True, WHITE), (cx, start_y + row))
|
||||||
surface.blit(fonts['tiny'].render(row3_fn(item), True, DIM_GRAY), (cx, start_y + 2*row))
|
surface.blit(fonts['tiny'].render(row3_fn(item), True, DIM_GRAY), (cx, start_y + 2*row))
|
||||||
|
|
||||||
|
# Town name header
|
||||||
|
if town:
|
||||||
|
surface.blit(fonts['tiny'].render(town, True, GRAY), (x, y))
|
||||||
|
|
||||||
if not week:
|
if not week:
|
||||||
surface.blit(fonts['tiny'].render('Weather loading…', True, DIM_GRAY), (x, y))
|
surface.blit(fonts['tiny'].render('Loading…', True, DIM_GRAY), (x, y + row))
|
||||||
return
|
return
|
||||||
|
|
||||||
# 5-day section
|
# 5-day section
|
||||||
_col(week,
|
_col(week,
|
||||||
y,
|
y + row + 2,
|
||||||
lambda d: 'Today' if d['day'] == 'Today' else d['day'][:3],
|
lambda d: 'Today' if d['day'] == 'Today' else d['day'][:3],
|
||||||
lambda d: f"{d['high']}°/{d['low']}°",
|
lambda d: f"{d['high']}°/{d['low']}°",
|
||||||
lambda d: _trunc(d['desc']))
|
lambda d: _trunc(d['desc']))
|
||||||
@@ -222,7 +229,7 @@ def draw_weather(surface: pygame.Surface, fonts: dict, x: int, y: int) -> None:
|
|||||||
# 5-hour section
|
# 5-hour section
|
||||||
if hours:
|
if hours:
|
||||||
_col(hours[:_WEATHER_COLS],
|
_col(hours[:_WEATHER_COLS],
|
||||||
y + 3*row + 8,
|
y + row + 2 + 3*row + 8,
|
||||||
lambda h: h['label'],
|
lambda h: h['label'],
|
||||||
lambda h: f"{h['temp']}°",
|
lambda h: f"{h['temp']}°",
|
||||||
lambda h: _trunc(h['desc']))
|
lambda h: _trunc(h['desc']))
|
||||||
@@ -278,7 +285,9 @@ def main() -> None:
|
|||||||
|
|
||||||
# Resolve postcode and start weather thread
|
# Resolve postcode and start weather thread
|
||||||
try:
|
try:
|
||||||
lat, lon = _postcode_to_latlon(_args.location)
|
lat, lon, town = _postcode_to_latlon(_args.location)
|
||||||
|
with _weather_lock:
|
||||||
|
_weather['town'] = town
|
||||||
threading.Thread(target=_weather_worker, args=(lat, lon), daemon=True).start()
|
threading.Thread(target=_weather_worker, args=(lat, lon), daemon=True).start()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -311,7 +320,8 @@ def main() -> None:
|
|||||||
draw_wireframe_face(screen, face_cx, face_cy, face_rx, face_ry)
|
draw_wireframe_face(screen, face_cx, face_cy, face_rx, face_ry)
|
||||||
|
|
||||||
draw_clock(screen, fonts, 20, clock_y)
|
draw_clock(screen, fonts, 20, clock_y)
|
||||||
draw_weather(screen, fonts, weather_x, clock_y)
|
draw_weather(screen, fonts, weather_x,
|
||||||
|
clock_y - fonts['tiny'].get_height() - 2)
|
||||||
draw_dot(screen, W // 2, dot_y)
|
draw_dot(screen, W // 2, dot_y)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|||||||
Reference in New Issue
Block a user