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():
|
class Weather_Checker():
|
||||||
|
|
||||||
def __init__(self, city: str, display: NeoPixel_64x64):
|
def __init__(self, display: NeoPixel_64x64):
|
||||||
self.display = display
|
self.display = display
|
||||||
self.city = URLEncoder.encode(city) # url_encode
|
|
||||||
self.display.set_font( font=fonts.font_5x7)
|
self.display.set_font( font=fonts.font_5x7)
|
||||||
|
|
||||||
def mock_weather_data(self):
|
def mock_weather_data(self):
|
||||||
@@ -34,7 +33,7 @@ class Weather_Checker():
|
|||||||
return None
|
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)
|
weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang)
|
||||||
|
|
||||||
if not test_mode:
|
if not test_mode:
|
||||||
@@ -59,11 +58,12 @@ class Weather_Checker():
|
|||||||
return weather
|
return weather
|
||||||
|
|
||||||
|
|
||||||
def check(self, test_mode: bool = False):
|
def check(self, city:str, lang:str, test_mode: bool = False):
|
||||||
try:
|
try:
|
||||||
self.display.clear()
|
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
|
ypos = 0
|
||||||
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
|
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
|
||||||
|
|||||||
@@ -12,8 +12,29 @@ class URLEncoder:
|
|||||||
else:
|
else:
|
||||||
# result.append('%' + format(ord(char), '02X'))
|
# result.append('%' + format(ord(char), '02X'))
|
||||||
# Converted to % formatting:
|
# 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)
|
return ''.join(result)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
6
main.py
6
main.py
@@ -17,7 +17,7 @@ if __name__ == '__main__':
|
|||||||
# emoji_checker.check()
|
# emoji_checker.check()
|
||||||
|
|
||||||
# tryout.weather_check(display, test_mode=False)
|
# tryout.weather_check(display, test_mode=False)
|
||||||
weather_checker: Weather_Checker = Weather_Checker( city="München", display=display)
|
weather_checker: Weather_Checker = Weather_Checker( display=display)
|
||||||
weather_checker.check(test_mode=False)
|
weather_checker.check(city="miami", lang="de", test_mode=False)
|
||||||
|
|
||||||
show_system_load()
|
# show_system_load()
|
||||||
|
|||||||
Reference in New Issue
Block a user