From e3e01c0bc2519a08c77ad52ec7edc87ac91e4969 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:05:46 -0700 Subject: [PATCH 1/8] WA5ZNU: Allow XML empty tags Some RSS feeds have empty tags and that confused the parser. --- badger_os/examples/news.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index db08275..7e52b0d 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -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'/': + # : 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() From 3c82f7a14fe649a65003513714099c664a26ed00 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:09:01 -0700 Subject: [PATCH 2/8] Summary: WA5ZNU: accomondate 'guid' or 'link' Some RSS feeds provide both. Prefer link to guid --- badger_os/examples/news.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index 7e52b0d..a803be2 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -131,7 +131,8 @@ 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: @@ -169,7 +170,8 @@ def draw_page(): 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: From 740a75814d8340af7b602ec2ed39e36192ad62a2 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:09:49 -0700 Subject: [PATCH 3/8] WA5ZNU: Fix OOM by closing stream https://github.com/micropython/micropython-lib/issues/175 --- badger_os/examples/news.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index a803be2..ad53817 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -139,6 +139,8 @@ def get_rss(url): print(e) return False + finally: + stream.close() # Connects to the wireless network. Ensure you have entered your details in WIFI_CONFIG.py :). display.connect() From b48492f20210ef2934a2e289332baaae4abb1b33 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:10:39 -0700 Subject: [PATCH 4/8] WA5ZNU: log URL in get_rss error --- badger_os/examples/news.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index ad53817..c7a91a1 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -136,7 +136,8 @@ def get_rss(url): return output except OSError as e: - print(e) + # [klotz] better errors + print("error", url, e) return False finally: From 151d4c093ed239357091834af8a8104715d3a8f5 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:11:41 -0700 Subject: [PATCH 5/8] WA5ZNU: more print feed info on button push --- badger_os/examples/news.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index c7a91a1..3dd564c 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -146,7 +146,7 @@ def get_rss(url): # 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"]]) @@ -223,4 +223,5 @@ def draw_page(): changed = True if changed: + print(f"feed: {state["feed"]} page: {state["current_page"]} url: {URL[state["feed"]]}") draw_page() From 22f55ccf3e9d7c648a350e59f59f3ad0babf9cba Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:40:30 -0700 Subject: [PATCH 6/8] WA5ZNU: add hostname to RSS header --- badger_os/examples/news.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index 3dd564c..18faedd 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -162,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) @@ -171,7 +182,6 @@ 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) # [klotz] code.set_text(feed[page]["link"] or feed[page]["guid"]) From bc352062d1db90268d4d2b6d479794ef2adc3d74 Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:41:05 -0700 Subject: [PATCH 7/8] WA5ZNU: try to save memory - clear feed before reparsing --- badger_os/examples/news.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index 18faedd..85db117 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -214,6 +214,7 @@ def urlhostname(url): 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 @@ -221,6 +222,7 @@ def urlhostname(url): 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 @@ -228,6 +230,7 @@ def urlhostname(url): 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 From 6b32526888e4db479627740eaaddf003e44eca4c Mon Sep 17 00:00:00 2001 From: Leigh Klotz Date: Sat, 6 May 2023 14:47:02 -0700 Subject: [PATCH 8/8] WA5ZNU: start with feed 0 instead of feed 2 I'm unsure whether this is a fix or a preference, but I've chosen to start with 0. --- badger_os/examples/news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/badger_os/examples/news.py b/badger_os/examples/news.py index 85db117..66cb4d8 100644 --- a/badger_os/examples/news.py +++ b/badger_os/examples/news.py @@ -15,7 +15,7 @@ state = { "current_page": 0, - "feed": 2 + "feed": 0 } badger_os.state_load("news", state)