v.0.6.1 URLEncoder.encode_utf8
This commit is contained in:
@@ -13,9 +13,8 @@ ACTUAL_WEATHER_URL = 'http://api.weatherapi.com/v1/current.json?key={API_KEY}&q=
|
||||
|
||||
class Weather_Checker():
|
||||
|
||||
def __init__(self, city: str, display: NeoPixel_64x64):
|
||||
def __init__(self, display: NeoPixel_64x64):
|
||||
self.display = display
|
||||
self.city = URLEncoder.encode(city) # url_encode
|
||||
self.display.set_font( font=fonts.font_5x7)
|
||||
|
||||
def mock_weather_data(self):
|
||||
@@ -34,7 +33,7 @@ class Weather_Checker():
|
||||
return None
|
||||
|
||||
|
||||
def actual_weather(self, city='grosshansdorf', lang='de', test_mode=False) -> Weather:
|
||||
def actual_weather(self, city, lang, test_mode=False) -> Weather:
|
||||
weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang)
|
||||
|
||||
if not test_mode:
|
||||
@@ -59,11 +58,12 @@ class Weather_Checker():
|
||||
return weather
|
||||
|
||||
|
||||
def check(self, test_mode: bool = False):
|
||||
def check(self, city:str, lang:str, test_mode: bool = False):
|
||||
try:
|
||||
self.display.clear()
|
||||
|
||||
w_resp: Weather = self.actual_weather(city=self.city, lang='de', test_mode=test_mode)
|
||||
city = URLEncoder.encode_utf8(city) # url_encode
|
||||
w_resp: Weather = self.actual_weather(city=city, lang=lang, test_mode=test_mode)
|
||||
|
||||
ypos = 0
|
||||
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
|
||||
|
||||
@@ -12,8 +12,29 @@ class URLEncoder:
|
||||
else:
|
||||
# result.append('%' + format(ord(char), '02X'))
|
||||
# Converted to % formatting:
|
||||
result.append("%%%02X" % ord(char))
|
||||
special_char = "%%%02X" % ord(char)
|
||||
|
||||
print( f"{char} --> {special_char}")
|
||||
|
||||
result.append(special_char)
|
||||
|
||||
return ''.join(result)
|
||||
|
||||
@staticmethod
|
||||
def encode_utf8(string):
|
||||
"""URL encode a string with proper UTF-8 handling"""
|
||||
result = []
|
||||
safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
|
||||
|
||||
for char in string:
|
||||
if char in safe_chars:
|
||||
result.append(char)
|
||||
else:
|
||||
# UTF-8 encoding for proper URL encoding
|
||||
utf8_bytes = char.encode('utf-8')
|
||||
for byte in utf8_bytes:
|
||||
result.append(f"%{byte:02X}")
|
||||
|
||||
return ''.join(result)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user