115
115
CONTENT_IMAGE = const (3 )
116
116
117
117
118
+ class HttpError (Exception ):
119
+ """HTTP Specific Error"""
120
+
121
+
118
122
class Fake_Requests :
119
123
"""For faking 'requests' using a local file instead of the network."""
120
124
@@ -758,10 +762,25 @@ def wget(self, url, filename, *, chunk_size=12000):
758
762
759
763
self .neo_status ((100 , 100 , 0 ))
760
764
r = requests .get (url , stream = True )
765
+
761
766
headers = {}
762
767
for title , content in r .headers .items ():
763
768
headers [title .lower ()] = content
764
769
770
+ if r .status_code == 200 :
771
+ print ("Reply is OK!" )
772
+ self .neo_status ((0 , 0 , 100 )) # green = got data
773
+ else :
774
+ if self ._debug :
775
+ if "content-length" in headers :
776
+ print ("Content-Length: {}" .format (int (headers ["content-length" ])))
777
+ if "date" in headers :
778
+ print ("Date: {}" .format (headers ["date" ]))
779
+ self .neo_status ((100 , 0 , 0 )) # red = http error
780
+ raise HttpError (
781
+ "Code {}: {}" .format (r .status_code , r .reason .decode ("utf-8" ))
782
+ )
783
+
765
784
if self ._debug :
766
785
print (headers )
767
786
if "content-length" in headers :
@@ -938,9 +957,6 @@ def fetch(self, refresh_url=None, timeout=10):
938
957
elif "application/javascript" in headers ["content-type" ]:
939
958
content_type = CONTENT_JSON
940
959
else :
941
- print (
942
- "HTTP Error {}: {}" .format (r .status_code , r .reason .decode ("utf-8" ))
943
- )
944
960
if self ._debug :
945
961
if "content-length" in headers :
946
962
print (
@@ -949,7 +965,9 @@ def fetch(self, refresh_url=None, timeout=10):
949
965
if "date" in headers :
950
966
print ("Date: {}" .format (headers ["date" ]))
951
967
self .neo_status ((100 , 0 , 0 )) # red = http error
952
- return None
968
+ raise HttpError (
969
+ "Code {}: {}" .format (r .status_code , r .reason .decode ("utf-8" ))
970
+ )
953
971
954
972
if self ._debug and content_type == CONTENT_TEXT :
955
973
print (r .text )
@@ -993,11 +1011,17 @@ def fetch(self, refresh_url=None, timeout=10):
993
1011
except KeyError :
994
1012
print (json_out )
995
1013
raise
996
- elif self ._regexp_path :
1014
+ elif content_type == CONTENT_TEXT and self ._regexp_path :
997
1015
for regexp in self ._regexp_path :
998
1016
values .append (re .search (regexp , r .text ).group (1 ))
999
1017
else :
1000
- values = r .text
1018
+ if content_type == CONTENT_JSON :
1019
+ # No path given, so return JSON as string for compatibility
1020
+ import json # pylint: disable=import-outside-toplevel
1021
+
1022
+ values = json .dumps (r .json ())
1023
+ else :
1024
+ values = r .text
1001
1025
1002
1026
if self ._image_json_path :
1003
1027
try :
0 commit comments