Create a gist now

Instantly share code, notes, and snippets.

Creates a CSV file containing the latest data for all UK McDonald's restaurants
#!/usr/bin/env python
import csv, json
from urllib import request
#
# Script to automate the download and parsing of data for every McDonald's Restaurant in the UK.
#
# The restaurant data is downloaded from the Multimap API used by the official McDonald's Restaurant
# Locator (http://www.mcdonalds.co.uk/restaurants/restaurant-locator.shtml).
#
# Any improvements are welcome.
#
__author__ = 'Lucas'
__homepage__ = 'http://www.flyingtophat.co.uk/'
__version__ = '1.0'
__date__ = '2011/11/13'
# Datasource argument in URL that is needed when parsing JSON response
DATASOURCE = 'mm.clients.mcdonalds_01_new'
MCDONALDS_MULTIMAP_URL = 'http://clients.multimap.com/API/search/1.2/mcdonalds_01?output=json&dataSource=' + DATASOURCE + '&count=9999999999&lat=53&lon=-1&orderByFields=pc'
OUTPUT_FILENAME = 'restaurants.csv'
def extractRecords(jsonData, dataSource):
""" Extracts the JSON for each record """
records = []
for value in jsonData['record_sets'][dataSource]['records']:
records.append(value)
return records
def recordDetailHeaders():
""" Returns the header for each comma separated value """
# The order of the headers must match the order of record details
# returned by the extractRecordDetails(jsonRecord) function
return ('Store Name',
'Street',
'Town/City',
'Post Code',
'Phone Number',
'Latitude',
'Longitude')
def extractRecordDetails(jsonRecord):
""" Extracts the location from a JSON record """
# Any change to the order of the returned elements must be duplicated
# in the recordDetailHeaders() function
if 'point' in jsonRecord:
return (value['name'], # Store Name
value['street'], # Address
value['town'], # Town/City
value['pc'], # Postal Code
value['telephone'], # Phone Number
#value['info1'],# Monday opening time
#value['info2'],# Tuesday opening time
#value['info3'],# Wednesday opening time
#value['info4'],# Thursday opening time
#value['info5'],# Friday opening time
#value['info6'],# Saturday opening time
#value['info7'],# Sunday opening time
#value['meta1'],# Drive Through
#value['meta2'],# Unknown
#value['meta3'],# Unknown
#value['meta4'],# Baby-change or Free WIFI
#value['meta5'],# Baby-change or Free WIFI
#value['meta7'],# Accessibility
value['point']['lat'], # Latitude
value['point']['lon'] # Longitude
)
else:
return None
with request.urlopen(MCDONALDS_MULTIMAP_URL) as response:
data = response.read()
# Load record-set from Multimap API
jsonData = json.loads(data.decode('utf-8'))
if jsonData:
recordSet = extractRecords(jsonData, DATASOURCE);
# Extract location of each record
stores = []
for value in recordSet:
store = extractRecordDetails(value)
if store:
stores.append(store)
# Serialise extracted stores to CVS file
with open(OUTPUT_FILENAME, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(recordDetailHeaders())
writer.writerows(stores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment