46
46
import os
47
47
import time
48
48
import gc
49
+ from micropython import const
49
50
import board
50
51
import busio
51
52
from digitalio import DigitalInOut
109
110
LOCALFILE = "local.txt"
110
111
# pylint: enable=line-too-long
111
112
113
+ CONTENT_TEXT = const (1 )
114
+ CONTENT_JSON = const (2 )
115
+ CONTENT_IMAGE = const (3 )
116
+
112
117
113
118
class Fake_Requests :
114
119
"""For faking 'requests' using a local file instead of the network."""
115
120
116
121
def __init__ (self , filename ):
117
122
self ._filename = filename
118
- with open (filename , "r" ) as file :
119
- self .text = file .read ()
120
123
121
124
def json (self ):
122
125
"""json parsed version for local requests."""
123
126
import json # pylint: disable=import-outside-toplevel
124
127
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 ()
126
136
127
137
128
138
class PyPortal :
@@ -744,14 +754,17 @@ def wget(self, url, filename, *, chunk_size=12000):
744
754
745
755
self .neo_status ((100 , 100 , 0 ))
746
756
r = requests .get (url , stream = True )
757
+ headers = {}
758
+ for title , content in r .headers .items ():
759
+ headers [title .lower ()] = content
747
760
748
761
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" ])
753
765
else :
754
- raise RuntimeError ("Content-length missing from headers" )
766
+ raise RuntimeError ("Content-Length missing from headers" )
767
+ remaining = content_length
755
768
print ("Saving data to " , filename )
756
769
stamp = time .monotonic ()
757
770
file = open (filename , "wb" )
@@ -886,6 +899,7 @@ def fetch(self, refresh_url=None, timeout=10):
886
899
json_out = None
887
900
image_url = None
888
901
values = []
902
+ content_type = CONTENT_TEXT
889
903
890
904
gc .collect ()
891
905
if self ._debug :
@@ -903,14 +917,41 @@ def fetch(self, refresh_url=None, timeout=10):
903
917
self .neo_status ((100 , 100 , 0 )) # yellow = fetching data
904
918
gc .collect ()
905
919
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
906
923
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
909
947
910
- if self ._debug and not self . _image_json_path and not self . _json_path :
948
+ if self ._debug and content_type == CONTENT_TEXT :
911
949
print (r .text )
912
950
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 :
914
955
try :
915
956
gc .collect ()
916
957
json_out = r .json ()
0 commit comments