Skip to content

Instantly share code, notes, and snippets.

@pierrelorioux
Created July 22, 2013 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrelorioux/6055782 to your computer and use it in GitHub Desktop.
Save pierrelorioux/6055782 to your computer and use it in GitHub Desktop.
#To put this into a CSV, csv code adapted from this recipe
#(http://www.palewire.com/posts/2009/03/03/django-recipe-dump-your-queryset-out-as-a-csv-file/)
#of Ben Welsh at the LAT, who helped me do my first work with APIs:
# IMPORTS
#Make Python understand how to read things on the Internet
import urllib2
#Make Python understand the stuff in a page on the Internet is JSON
import json
# Make Python understand csvs
import csv
# Make Python know how to take a break so we don't hammer API and exceed rate limit
from time import sleep
def getHosp(country,iso2):
print country
print iso2
#print out what number loop we are on, which will make it easier to track down problems when they appear print i
#create the URL of the JSON file we want. We search for 'egypt', want English tweets,
#and set the number of tweets per JSON file to the max of 100, so we have to do as little looping as possible
req = str(country)
print req
url = urllib2.Request('http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];area[name=%22'+country+'%22];(node[%22amenity%22=%22hospital%22](area););out;')
#use the JSON library to turn this file into a Pythonic data structure
#print url
parsed_json = json.load(urllib2.urlopen(url))
#run through each item in results, and jump to an item in that dictionary, ex: the text of the tweet
for tweet in parsed_json['elements']:
print 'in'
row = []
row.append(country)
row.append(iso2)
try:
print tweet['tags']['name'].encode('utf-8')
row.append(tweet['tags']['name'].encode('utf-8'))
except KeyError:
row.append('no-data')
row.append(str(tweet['lat']))
row.append(str(tweet['lon']))
try:
row.append(tweet['tags']['emergency'].encode('utf-8'))
except KeyError:
row.append('no-data')
#once you have all the cells in there, write the row to your csv
#print row
writer.writerow(row)
print 'country complete'
outfile_path='C://Users//Pierrot//Documents//file3.csv'
# open it up, the w means we will write to it
writer = csv.writer(open(outfile_path, 'w'))
#create a list with headings for our columns
headers = ['country','iso2','name', 'lat' , 'lon','emergency']
#write the row of headings to our CSV file
writer.writerow(headers)
# GET JSON AND PARSE IT INTO DICTIONARY
# We need a loop because we have to do this for every JSON file we grab
urlCountry = urllib2.Request('http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];node[%22place%22=%22country%22][%22int_name%22!~%22%22];out;')
parsed_country = json.load(urllib2.urlopen(urlCountry))
#print parsed_country
for country in parsed_country['elements']:
name=str(country['tags']['name'].encode('utf-8'))
try: iso2=country['tags']['country_code_iso3166_1_alpha_2'].encode('utf-8')
except KeyError: iso2='no_iso2'
#if name not 'null'
try: getHosp(name,iso2)
except KeyError: print 'NoOo0o int_name'
sleep(5)
#getHosp('Syria')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment