City.py 559 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env python
  2. import urllib.request
  3. import urllib.parse
  4. import json
  5. class City(object):
  6. KEY = "5j1znBVAsnSf5xQyNQyq"
  7. AQI_URL = "http://www.pm25.in/api/querys/pm10.json?city=%s&token=%s&stations=no" % ("%s", KEY)
  8. def __init__(self, name):
  9. self._name = name
  10. @property
  11. def name(self):
  12. return self._name
  13. @property
  14. def aqi(self):
  15. resp = urllib.request.urlopen(self.AQI_URL % urllib.parse.quote(self._name))
  16. result = resp.read()
  17. return json.loads(result.decode("UTF-8"))[0]['aqi']