Skip to content
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
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# data-gc-ca-api 0.2.4 README
# data-gc-ca-api 2.0.0 README

## Introduction
data-gc-ca-api: a simple python api for the Canada Open Data Portal
Expand All @@ -15,19 +15,7 @@ Environment Canada provides a [description](http://goo.gl/XD7w4) of what can be
accessed but it's far easier to look at an example [city
XML](http://goo.gl/vyL7r).

## Installation

This package is available from the [Python Package Index](http://pypi.python.org). It
can be easily installed using pip as follows

$ pip install data-gc-ca-api

Tarballs of are available from the [project website](https://github.com/igable/data-gc-ca-api).
To install from a tarball:

$ tar xzvf data-gc-ca-api-X.Y.Z.tar.gz
$ cd data-gc-ca-api-X.Y.Z
$ python setup.py install
Updated to work with Python 3.

## Command line tool: weatherca

Expand Down
25 changes: 11 additions & 14 deletions data_gc_ca_api/cityweather.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Auth.: Ian Gable

import urllib
import urllib.request
import sys


Expand All @@ -29,21 +29,20 @@ def __init__(self):
cityListTree = ElementTree()

try:
urlhandle = urllib.urlopen(self.city_list_url)
urlhandle = urllib.request.urlopen(self.city_list_url)
except IOError:
raise IOError("Unable to open the data url: " + self.city_list_url)

cityListTree.parse(urlhandle)
siteList = cityListTree.findall("site")

for site in siteList:
cityNameEnglish = site.findtext("nameEn").encode('utf-8')

cityNameEnglish = site.findtext("nameEn")
city_name_english=cityNameEnglish.encode('utf-8')
self.cities[cityNameEnglish] = {\
'sitecode': site.attrib['code'].encode('utf-8'),\
'provincecode': site.findtext("provinceCode").encode('utf-8'),\
'nameFr': site.findtext("nameFr").encode('utf-8') }

'sitecode': site.attrib['code'],\
'provincecode': site.findtext("provinceCode"),\
'nameFr': site.findtext("nameFr")}

def is_city(self, name):
"""
Expand All @@ -55,9 +54,7 @@ def data_url(self,name):
"""
Returns resource URL for the city denoted by name
"""
if self.is_city(name):
return self.base_url + self.province(name) + "/" + self.site_code(name) + "_e.xml"
return None
return self.base_url + self.province(name) + "/" + self.site_code(name) + "_e.xml"

def province(self,name):
"""
Expand Down Expand Up @@ -97,9 +94,9 @@ def __init__(self, dataurl):
self.tree = ElementTree()

try:
urlhandle = urllib.urlopen(dataurl)
urlhandle = urllib.request.urlopen(dataurl)
except IOError:
print "[Error] Unable to open the data url: " + dataurl
print("[Error] Unable to open the data url: " + dataurl)
sys.exit(1)

self.tree.parse(urlhandle)
Expand Down Expand Up @@ -143,7 +140,7 @@ def _get_all_xpaths(self, pathlist, path, element):

def _make_attribute_list(self, attrib):
xpathattrib = ""
for attribute, value in attrib.iteritems():
for attribute, value in attrib.items():
xpathattrib = xpathattrib + "[@" + attribute + "='" + value + "']"
return xpathattrib

Expand Down
24 changes: 9 additions & 15 deletions weatherca
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sys
import difflib
from optparse import OptionParser
from difflib import SequenceMatcher
from data_gc_ca_api.cityweather import City, CityIndex
from data_gc_ca_api.cityweather import City, CityIndex
from data_gc_ca_api.__version__ import version


Expand Down Expand Up @@ -45,8 +45,6 @@ def get_best_guess(city, cityList):
return None




def main():


Expand Down Expand Up @@ -81,12 +79,11 @@ def main():
sys.exit(1)
if options.list:
for cityname in cityindex.english_city_list():
print cityname
print(str(cityname))
sys.exit(1)


if not options.city:
print "No City given, using, Victoria, BC (the best city)."
print("No city specified, using default: " + (city))
else:
city = options.city

Expand All @@ -97,25 +94,22 @@ def main():
if options.quantity:
quantity = options.quantity



if cityindex.is_city(city):
cityObject = City(cityindex.data_url(city))
if options.list_quantities:
for q in cityObject.get_available_quantities():
print q
print(q)
else:
if not options.quantity:
print "No requested quantity given, using temperature"
print quantity + " is: " + cityObject.get_quantity(quantity)
print("No requested quantity given, using temperature")
print(quantity + " is: " + cityObject.get_quantity(quantity))
else:
print city + " is not available"
print(city + " is not available")
bestGuess = get_best_guess(city,cityindex.english_city_list())
if bestGuess:
print "Did you mean: " + bestGuess
print("Did you mean: " + bestGuess)
else:
print "Do 'weatherca --list' to get a list of cities"

print("Do 'weatherca --list' to get a list of cities")

if __name__ == "__main__":
main()
Expand Down