Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # World Bank Climate API Example
- #
- #
- #
- #The MIT License (MIT)
- #
- #Copyright (c) 2014 Altoidnerd
- #
- #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- #
- #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- #
- #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- #
- #
- # World Bank Climate data fetch
- from sys import argv
- from urllib2 import Request, urlopen, URLError
- # argv unpacks three variables, the script name (e.g. USA.py), the country code, and the target file which will be overwritten
- # example: ~$ python USA.py USA USA.txt
- script, country_code, file_name = argv
- # overwrite warning
- print("Please note this will erase the contents of %r.\n") % file_name
- print "This returns %s data from the world bank climate API with an http request of the form http://climatedataapi.worldbank.org/climateweb/rest/v1/country/type/var/start/end/ISO3[.ext].\n" % country_code
- p = 'Hit enter to begin'
- xx = raw_input(p)
- print("""
- select type:\n
- mavg -> monthly avg\n
- annualavg -> annual average\n
- manom -> average monthly\n
- annualanom -> average annual change\n
- """)
- prompt2 = '<type=>'
- typ = raw_input(prompt2)
- print("""
- select var:\n
- pr -> preciptation\n
- tas -> temperature, in degrees Celsius\n
- """)
- prompt3 = '<var=>'
- varr = raw_input(prompt3)
- print('strat date?\n')
- prompt4 ='<start>'
- start = raw_input(prompt4)
- print('end date?\n')
- prompt5 ='<end>'
- end = raw_input(prompt5)
- key = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/%s/%s/%s/%s/%s' % (typ, varr, start, end, country_code)
- print('\n'+'key = %s \n' % key)
- # notice here the script name is unpacked as the ISO3 country code, so for example to get USA data, name the script USA.py
- target = open(file_name, 'w')
- request = Request(key)
- try:
- response = urlopen(request)
- climate_data = response.read()
- target.write(climate_data + '\nkey = ' + key + '\n')
- print('\n' + climate_data + '\n')
- except URLError, e:
- print 'API down', e
Advertisement
Add Comment
Please, Sign In to add comment