Skip to content

Commit 7e0f9f9

Browse files
committed
Parser: Apple: Override openURL() function to provide more meaningful error messages to user
1 parent a31e3f7 commit 7e0f9f9

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

sdk-parsers/RMParserFramework/parsers/apple-premium-parser.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from RMUtilsFramework.rmLogging import log
77
from RMUtilsFramework.rmTimeUtils import *
88
from RMDataFramework.rmUserSettings import globalSettings
9+
import urllib, urllib2
910
import json
1011

1112
class AppleWeatherKit(RMParser):
@@ -21,6 +22,7 @@ class AppleWeatherKit(RMParser):
2122
def perform(self):
2223
lat = globalSettings.location.latitude
2324
lon = globalSettings.location.longitude
25+
2426
self.lastKnownError = ""
2527
URL = "https://weather.rainmachine.com/appleweather"
2628
try:
@@ -210,6 +212,38 @@ def __openURL(self, url, params, token):
210212
'Authorization': "Bearer " + token
211213
})
212214

215+
def openURL(self, url, params = None, encodeParameters = True, headers = {}):
216+
if params:
217+
if encodeParameters:
218+
query_string = urllib.urlencode(params)
219+
else:
220+
query_string = params
221+
222+
url = "?" . join([url, query_string])
223+
224+
log.debug("Parser '%s': downloading from %s" % (self.parserName, url))
225+
226+
try:
227+
req = urllib2.Request(url=url, headers=headers)
228+
res = urllib2.urlopen(url=req, timeout=60)
229+
return res
230+
except urllib2.HTTPError, e:
231+
if e.code == 403:
232+
self.lastKnownError = "Your account doesn't have a valid premium subscription."
233+
elif e.code == 429:
234+
self.lastKnownError = "Exceeded maximum daily requests calls."
235+
elif e.code == 400:
236+
self.lastKnownError = "Apple WeatherKit Server response error."
237+
elif e.code == 401:
238+
self.lastKnownError = "Apple WeatherKit Unauthorized. Please contact RainMachine Support"
239+
else:
240+
self.lastKnownError = "Retrying: URL open fail"
241+
except Exception, e:
242+
self.lastKnownError = "Retrying: URL open fail"
243+
244+
log.error(self.lastKnownError)
245+
return None
246+
213247
if __name__ == "__main__":
214248
parser = AppleWeatherKit()
215-
parser.perform()
249+
parser.perform()

0 commit comments

Comments
 (0)