Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WA5ZNU: news.py bug fixes and enhancements #21

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
42 changes: 34 additions & 8 deletions badger_os/examples/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

state = {
"current_page": 0,
"feed": 2
"feed": 0
}

badger_os.state_load("news", state)
Expand Down Expand Up @@ -95,7 +95,14 @@ def parse_xml_stream(s, accept_tags, group_by, max_items=3):

else:
current_tag = read_until(s, b">")
tag += [next_char + current_tag.split(b" ")[0]]
# [klotz] fix short-form close XML
if current_tag[-1:] == b'/':
# <foo />: there's no text content, and don't process attributes, so ignore
# current_tag = next_char + current_tag.split(b" ")[0].split(b"/")[0]
current_tag = None
else:
current_tag = next_char + current_tag.split(b" ")[0]
tag += [current_tag]
text = b""
gc.collect()

Expand Down Expand Up @@ -124,18 +131,22 @@ def draw_qr_code(ox, oy, size, code):
def get_rss(url):
try:
stream = urequest.urlopen(url)
output = list(parse_xml_stream(stream, [b"title", b"description", b"guid", b"pubDate"], b"item"))
# [klotz] accommodate guid or link
output = list(parse_xml_stream(stream, [b"title", b"description", b"guid", b"link", b"pubDate"], b"item"))
return output

except OSError as e:
print(e)
# [klotz] better errors
print("error", url, e)
return False

finally:
stream.close()

# Connects to the wireless network. Ensure you have entered your details in WIFI_CONFIG.py :).
display.connect()

print(state["feed"])
print(f"feed: {state["feed"]}")
feed = get_rss(URL[state["feed"]])


Expand All @@ -151,7 +162,18 @@ def draw_page():
display.set_pen(0)
display.rectangle(0, 0, WIDTH, 20)
display.set_pen(15)
display.text("News", 3, 4)

# [klotz]
if feed:
def urlhostname(url):
"""return hostname of url, without external dependencies"""
return url.split('//')[-1].split('/')[0]
url = URL[state["feed"]]
hostname = urlhostname(url)
display.text("News " + hostname, 3, 4)
else:
display.text("News", 3, 4)

display.text("Page: " + str(state["current_page"] + 1), WIDTH - display.measure_text("Page: ") - 4, 4)
display.set_pen(0)

Expand All @@ -160,9 +182,9 @@ def draw_page():
# Draw articles from the feed if they're available.
if feed:
page = state["current_page"]
display.set_pen(0)
display.text(feed[page]["title"], 2, 30, WIDTH - 130, 2)
code.set_text(feed[page]["guid"])
# [klotz]
code.set_text(feed[page]["link"] or feed[page]["guid"])
draw_qr_code(WIDTH - 100, 25, 100, code)

else:
Expand Down Expand Up @@ -192,23 +214,27 @@ def draw_page():
if button_a.value():
state["feed"] = 0
state["current_page"] = 0
feed = None
feed = get_rss(URL[state["feed"]])
badger_os.state_save("news", state)
changed = True

if button_b.value():
state["feed"] = 1
state["current_page"] = 0
feed = None
feed = get_rss(URL[state["feed"]])
badger_os.state_save("news", state)
changed = True

if button_c.value():
state["feed"] = 2
state["current_page"] = 0
feed = None
feed = get_rss(URL[state["feed"]])
badger_os.state_save("news", state)
changed = True

if changed:
print(f"feed: {state["feed"]} page: {state["current_page"]} url: {URL[state["feed"]]}")
draw_page()