8000 Optimize JSON data reading for chonky sites · Issue #3703 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content
Optimize JSON data reading for chonky sites #3703
Closed
@ladyada

Description

@ladyada

example for magtag of reading 'large' json files and parsing them

code.py output:
Connecting to AP adafruit
Reading data
Took 1.380859 seconds to get response
Took 40.878906 seconds to parse
Thoughts 5
import time
import random
from adafruit_magtag.magtag import MagTag

# Set up where we'll be fetching data from
DATA_SOURCE = "https://www.reddit.com/r/Showerthoughts/top.json?limit=5"

magtag = MagTag()
magtag.network.connect()

# thought in bold text, with text wrapping
magtag.add_text(
    text_font="Arial-Bold-12.bdf",
    text_wrap=33,
    text_maxlen=160,
    text_position=(10, 10),
    text_anchor_point=(0, 0),
    line_spacing=0.75,
)


timestamp = None
while True:
    if not timestamp or (time.monotonic() - timestamp) > 5:  # once every 60 seconds...
        try:
            print("Reading data")

            t = time.monotonic()
            response = magtag.network.requests.get(DATA_SOURCE)
            print("Took %f seconds to get response" % (time.monotonic() - t))
            t = time.monotonic()
            value = response.json()
            print("Took %f seconds to parse" % (time.monotonic() - t))

            thoughts = value["data"]["dist"]
            print("Thoughts", thoughts)
            randthought = random.randint(0, thoughts-1)
            thought = value["data"]["children"][randthought]["data"]["title"]
            author = value["data"]["children"][randthought]["data"]["author"]
            print("Random thought from", author, ":", thought)
            magtag.set_text(thought)
        except (ValueError, RuntimeError) as e:
            print("Some error occured, retrying! -", e)
        timestamp = time.monotonic()
5E86

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0