Skip to content

Commit 4e4b66c

Browse files
committed
Merge branch 'master' of https://github.com/adafruit/Adafruit_CircuitPython_PyPortal into terminalio
2 parents 7bb99b0 + 2271503 commit 4e4b66c

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

adafruit_pyportal.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import os
4747
import time
4848
import gc
49+
from micropython import const
4950
import board
5051
import busio
5152
from digitalio import DigitalInOut
@@ -109,20 +110,29 @@
109110
LOCALFILE = "local.txt"
110111
# pylint: enable=line-too-long
111112

113+
CONTENT_TEXT = const(1)
114+
CONTENT_JSON = const(2)
115+
CONTENT_IMAGE = const(3)
116+
112117

113118
class Fake_Requests:
114119
"""For faking 'requests' using a local file instead of the network."""
115120

116121
def __init__(self, filename):
117122
self._filename = filename
118-
with open(filename, "r") as file:
119-
self.text = file.read()
120123

121124
def json(self):
122125
"""json parsed version for local requests."""
123126
import json # pylint: disable=import-outside-toplevel
124127

125-
return json.loads(self.text)
128+
with open(self._filename, "r") as file:
129+
return json.load(file)
130+
131+
@property
132+
def text(self):
133+
"""raw text version for local requests."""
134+
with open(self._filename, "r") as file:
135+
return file.read()
126136

127137

128138
class PyPortal:
@@ -744,14 +754,17 @@ def wget(self, url, filename, *, chunk_size=12000):
744754

745755
self.neo_status((100, 100, 0))
746756
r = requests.get(url, stream=True)
757+
headers = {}
758+
for title, content in r.headers.items():
759+
headers[title.lower()] = content
747760

748761
if self._debug:
749-
print(r.headers)
750-
if "content-length" in r.headers:
751-
content_length = int(r.headers["content-length"])
752-
remaining = content_length
762+
print(headers)
763+
if "content-length" in headers:
764+
content_length = int(headers["content-length"])
753765
else:
754-
raise RuntimeError("Content-length missing from headers")
766+
raise RuntimeError("Content-Length missing from headers")
767+
remaining = content_length
755768
print("Saving data to ", filename)
756769
stamp = time.monotonic()
757770
file = open(filename, "wb")
@@ -886,6 +899,7 @@ def fetch(self, refresh_url=None, timeout=10):
886899
json_out = None
887900
image_url = None
888901
values = []
902+
content_type = CONTENT_TEXT
889903

890904
gc.collect()
891905
if self._debug:
@@ -903,14 +917,41 @@ def fetch(self, refresh_url=None, timeout=10):
903917
self.neo_status((100, 100, 0)) # yellow = fetching data
904918
gc.collect()
905919
r = requests.get(self._url, headers=self._headers, timeout=timeout)
920+
headers = {}
921+
for title, content in r.headers.items():
922+
headers[title.lower()] = content
906923
gc.collect()
907-
self.neo_status((0, 0, 100)) # green = got data
908-
print("Reply is OK!")
924+
if self._debug:
925+
print("Headers:", headers)
926+
if r.status_code == 200:
927+
print("Reply is OK!")
928+
self.neo_status((0, 0, 100)) # green = got data
929+
if "content-type" in headers:
930+
if "image/" in headers["content-type"]:
931+
content_type = CONTENT_IMAGE
932+
elif "application/json" in headers["content-type"]:
933+
content_type = CONTENT_JSON
934+
else:
935+
print(
936+
"HTTP Error {}: {}".format(r.status_code, r.reason.decode("utf-8"))
937+
)
938+
if self._debug:
939+
if "content-length" in headers:
940+
print(
941+
"Content-Length: {}".format(int(headers["content-length"]))
942+
)
943+
if "date" in headers:
944+
print("Date: {}".format(headers["date"]))
945+
self.neo_status((100, 0, 0)) # red = http error
946+
return None
909947

910-
if self._debug and not self._image_json_path and not self._json_path:
948+
if self._debug and content_type == CONTENT_TEXT:
911949
print(r.text)
912950

913-
if self._image_json_path or self._json_path:
951+
if self._debug:
952+
print("Detected Content Type", content_type)
953+
954+
if content_type == CONTENT_JSON:
914955
try:
915956
gc.collect()
916957
json_out = r.json()

0 commit comments

Comments
 (0)