import pandas as pd
import requests
r=requests.get('http://api.ratings.food.gov.uk/Regions/1/9999999',headers={"x-api-version":2})
j=json.loads(r.content)
import json
df=pd.DataFrame.from_dict(j['regions'])
df[:3]
df['links']=df['links'].apply(lambda x: x[0]['href'])
df
url='https://docs.google.com/a/okfn.org/spreadsheets/d/1M14S4hqG4F5P8H78VdOMMeoITOPBpVZEGoiCvXEFBQg/export?gid=0&format=csv'
df=pd.read_csv(url)
df
#http://api.ratings.food.gov.uk/help
#http://docs.python-requests.org/en/latest/user/quickstart/
params={'name':"McDonald's",'address':'SW12 9AU'}
r=requests.get('http://api.ratings.food.gov.uk/Establishments',
headers={"x-api-version":2},
params=params)
j=json.loads(r.content)
j
def getFoodRatingData(name,address):
params={'name':name,'address':address}
r=requests.get('http://api.ratings.food.gov.uk/Establishments',
headers={"x-api-version":2},
params=params)
return r
def parseFoodRatingData(jdata):
df=pd.DataFrame()
for establishment in jdata['establishments']:
info={}
for item in ['BusinessName','FHRSID','PostCode','RatingValue','RatingDate']:
info[item]= establishment[item]
for item in establishment['geocode']:
info[item]= establishment['geocode'][item]
for item in establishment['scores']:
info[item]= establishment['scores'][item]
df=df.append(info,ignore_index=True)
return df
def getAndParseFoodRatingData(name,address):
r=getFoodRatingData(name,address)
jdata=json.loads(r.content)
df=parseFoodRatingData(jdata)
return df
getinfo(j)
getAndParseFoodRatingData('Sacro Cuore','NW10 3NB')
adf=pd.DataFrame()
for place in df.iterrows():
adf=adf.append(getAndParseFoodRatingData(place[1]['Name'],place[1]['Postcode']))
adf
from IPython.display import HTML
import folium
def inline_map(map):
"""
Embeds the HTML source of the map directly into the IPython notebook.
This method will not work if the map depends on any files (json data). Also this uses
the HTML5 srcdoc attribute, which may not be supported in all browsers.
"""
map._build_map()
return HTML(''.format(srcdoc=map.HTML.replace('"', '"')))
def embed_map(map, path="map.html"):
"""
Embeds a linked iframe to the map into the IPython notebook.
Note: this method will not capture the source of the map into the notebook.
This method should work for all maps (as long as they use relative urls).
"""
map.create_map(path=path)
return HTML(''.format(path=path))
fmap=folium.Map(location=[51.5, 0], zoom_start=9)
for row in adf.iterrows():
latlon = [ row[1]['latitude'], row[1]['longitude'] ]
fmap.simple_marker( latlon, clustered_marker=True,
popup='Name: {name}
Score: {score}'.format(name=row[1]['BusinessName'],
score=row[1]['RatingValue']) )
inline_map(fmap)
from pandas.io.json import json_normalize