Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added language code support #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions pyccuweather/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class Connection(object):
:param API_KEY: API key
:param dev: whether the dev mode api (apidev.accuweather.com) or the production api (api.accuweather.com) is used
:param retry: number of retries of failed operations - TODO: implement
:param LANGUAGE: 2-digit language code
:raise errors.MalformattedAPIKeyError: if the API key is not a 32-character string, an error is thrown
"""

def __init__(self, API_KEY: str=None, dev: bool=True, retry: int=3):
def __init__(self, API_KEY: str=None, dev: bool=True, retry: int=3, LANGUAGE: str='EN'):

# TODO: implement retries

Expand All @@ -48,6 +49,13 @@ def __init__(self, API_KEY: str=None, dev: bool=True, retry: int=3):

self.API_ROOT = "http://apidev.accuweather.com" if dev is True else "http://api.accuweather.com"
self.API_VERSION = "v1"

try:
assert len(LANGUAGE) is 2
except AssertionError:
raise errors.BadLanguageInput()

self.LANGUAGE = LANGUAGE
self.retries = retry

def __str__(self):
Expand All @@ -60,6 +68,16 @@ def wipe_api_key(self):
"""
self.API_KEY = None

def set_language(self, LANGUAGE: str='EN'):
"""
sets the languague with the code input
"""
try:
assert len(LANGUAGE) is 2
except AssertionError:
raise errors.BadLanguageInput()
self.LANGUAGE = LANGUAGE

########################################################
# Location resolvers #
########################################################
Expand All @@ -84,7 +102,8 @@ def loc_geoposition(self, lat: float, lon: float):
raise errors.RangeError(lat, lon)

payload = {"q": u"{0:.4f},{1:.4f}".format(lat, lon),
"apikey": self.API_KEY}
"apikey": self.API_KEY,
"language": self.LANGUAGE}

resp = requests.get(url=froot("loc_geoposition"),
params=payload).json()
Expand Down Expand Up @@ -113,12 +132,14 @@ def loc_string(self, search_string: str, country_code: str=None):

url = froot("loc_search_country", country_code=country_code)
payload = {"q": search_string,
"apikey": self.API_KEY}
"apikey": self.API_KEY,
"language": self.LANGUAGE}

else:
url = froot("loc_search")
payload = {"q": search_string,
"apikey": self.API_KEY}
"apikey": self.API_KEY,
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload).json()
Expand Down Expand Up @@ -160,7 +181,8 @@ def loc_postcode(self, country_code: str, postcode: str):

url = froot("loc_postcode", country_code=country_code)
payload = {"q": postcode,
"apikey": self.API_KEY}
"apikey": self.API_KEY,
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload).json()
Expand All @@ -182,7 +204,8 @@ def loc_ip(self, ip_address:str):

url = froot("loc_ip_address")
payload = {"q": ip_address,
"apikey": self.API_KEY}
"apikey": self.API_KEY,
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload).json()
Expand All @@ -205,7 +228,8 @@ def loc_lkey(self, lkey:int):
assert isinstance(lkey, int)

url = froot("loc_lkey", location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload).json()
Expand Down Expand Up @@ -240,7 +264,8 @@ def get_current_wx(self, lkey:int=None, location:Location=None, current:int=0, d
url = froot("currentconditions_{current}".format(current=current), location_key=lkey)

payload = {"apikey": self.API_KEY,
"details": "true" if details is True else "false"}
"details": "true" if details is True else "false",
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload)
Expand All @@ -261,7 +286,8 @@ def get_forecast(self, forecast_type:str, lkey:int, details:bool=True, metric:bo
url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY,
"details": "true" if details == True else "false",
"metric": "true" if metric == True else "false"}
"metric": "true" if metric == True else "false",
"language": self.LANGUAGE}

resp = requests.get(url=url,
params=payload)
Expand All @@ -284,7 +310,8 @@ def get_airquality(self, lkey:int, current:bool=True):
fkeyid = "airquality_yesterday"

url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

return requests.get(url=url,
params=payload)
Expand All @@ -303,13 +330,15 @@ def get_actuals(self, lkey:int, start_date:str, end_date:str=None):
url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY,
"start": start_date,
"end": end_date}
"end": end_date,
"language": self.LANGUAGE}
else:
fkeyid = "climo_actuals_date"
url = froot(fkeyid,
date=start_date,
location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

return requests.get(url=url,
params=payload)
Expand All @@ -324,13 +353,15 @@ def get_records(self, lkey, start_date, end_date=None):
url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY,
"start": start_date,
"end": end_date}
"end": end_date,
"language": self.LANGUAGE}
else:
fkeyid = "climo_records_date"
url = froot(fkeyid,
date=start_date,
location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

return requests.get(url=url,
params=payload)
Expand All @@ -345,13 +376,15 @@ def get_normals(self, lkey, start_date, end_date=None):
url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY,
"start": start_date,
"end": end_date}
"end": end_date,
"language": self.LANGUAGE}
else:
fkeyid = "climo_normals_date"
url = froot(fkeyid,
date=start_date,
location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

return requests.get(url=url,
params=payload)
Expand All @@ -368,7 +401,8 @@ def get_alerts(self, lkey, forecast_range):
assert isinstance(forecast_range, int)
fkeyid = u"alarms_{0:d}d".format(forecast_range)
url = froot(fkeyid, location_key=lkey)
payload = {"apikey": self.API_KEY}
payload = {"apikey": self.API_KEY,
"language": self.LANGUAGE}

return requests.get(url=url,
params=payload)
params=payload)
16 changes: 15 additions & 1 deletion pyccuweather/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
http://www.github.com/chrisvoncsefalvay/pyccuweather/
"""

class BadLanguageInput(BaseException):
"""
Raised when the Language input is not correctly formed
"""
def __str__(self):
return "Language selection must a 2 digit language code compliant with ISO 639-1"


class NoAPIKeyProvided (BaseException):
"""
Raised when the API key is not provided or not set with the environment variable
"""
def __str__(self):
return "No API key provided or set with the environment variable ACCUWEATHER_APIKEY"

class MalformattedAPIKeyError(BaseException):
"""
Expand Down Expand Up @@ -110,4 +124,4 @@ def __init__(self, query):
self.query = query

def __str__(self):
return u"Your query for '{0:s}' yielded no results.".format(self.query)
return u"Your query for '{0:s}' yielded no results.".format(self.query)
3 changes: 1 addition & 2 deletions pyccuweather/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def __init__(self,
self.name = name
self.gmt_offset = gmt_offset
self.is_daylight_saving = is_daylight_saving
self.next_offset_change = strptime(next_offset_change, "%Y-%m-%dT%H:%M:%SZ")

self.next_offset_change = strptime(next_offset_change, "%Y-%m-%dT%H:%M:%SZ") if next_offset_change else None

class Location(object):
"""
Expand Down