X Tutup
'You should use urllib to retrieve the data from the API', 'urlencode' => 'You should use urlencode add parameters to the API url', 'json' => 'You should use the json library to parse the API data' ); // Compute the stuff for the output $code = 42; $MT = new Mersenne_Twister($code); $sample = $MT->shuffle($LOCATIONS); $sample_location = $sample[0]; $code = $USER->id+$LINK->id+$CONTEXT->id; $MT = new Mersenne_Twister($code); $actual = $MT->shuffle($LOCATIONS); $actual_location = $actual[0]; // Retrieve the data $api_url = dataUrl('geojson'); $google_api = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=University+of+Michigan'; $sample_url = $api_url . '?sensor=false&address=' . urlencode($sample_location); $actual_url = $api_url . '?sensor=false&address=' . urlencode($actual_location); $sample_data = Net::doGet($sample_url); $sample_count = strlen($sample_data); $response = Net::getLastHttpResponse(); $sample_json = json_decode($sample_data); if ( $response != 200 || $sample_json == null || ( !isset($sample_json->results[0])) ) { error_log("DIE: Sample response=$response url=$sample_url json_error=".json_last_error_msg()); die("Sample response=$response url=$sample_url json_error=".json_last_error_msg()); } // echo("
\n");echo(LTI::jsonIndent(json_encode($sample_json)));echo("
\n"); $sample_place = $sample_json->results[0]->place_id; $actual_data = Net::doGet($actual_url); $actual_count = strlen($actual_data); $response = Net::getLastHttpResponse(); $actual_json = json_decode($actual_data); if ( $response != 200 || $actual_json == null || ( !isset($actual_json->results[0])) ) { error_log("DIE: Actual response=$response url=$actual_url json_error=".json_last_error_msg()); die("Actual response=$response url=$actual_url json_error=".json_last_error_msg()); } $actual_place = $actual_json->results[0]->place_id; // echo("sample_place=$sample_place actual_place=$actual_place"); $oldgrade = $RESULT->grade; if ( isset($_POST['place_id']) && isset($_POST['code']) ) { $RESULT->setJsonKey('code', $_POST['code']); if ( $_POST['place_id'] != $actual_place ) { $_SESSION['error'] = "Your place_id did not match"; header('Location: '.addSession('index.php')); return; } $val = validate($sanity, $_POST['code']); if ( is_string($val) ) { $_SESSION['error'] = $val; header('Location: '.addSession('index.php')); return; } LTIX::gradeSendDueDate(1.0, $oldgrade, $dueDate); // Redirect to ourself header('Location: '.addSession('index.php')); return; } // echo($goodsha); if ( $RESULT->grade > 0 ) { echo('

Your current grade on this assignment is: '.($RESULT->grade*100.0).'%

'."\n"); } if ( $dueDate->message ) { echo('

'.$dueDate->message.'

'."\n"); } ?>

Calling a JSON API

In this assignment you will write a Python program somewhat similar to http://www.pythonlearn.com/code/geojson.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.

API End Points

To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:


This API uses the same parameters (sensor and address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get a list of all of the address values which can be used with this API.

To call the API, you need to provide a sensor=false parameter and the address that you are requesting as the address= parameter that is properly URL encoded using the urllib.urlencode() fuction as shown in http://www.pythonlearn.com/code/geojson.py

Test Data / Sample Execution

You can test to see if your program is working with a location of "" which will have a place_id of "".

$ python solution.py
Enter location:  
Retrieving http://...
Retrieved  characters
Place id  

Turn In

Please run your program to find the place_id for this location:


Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint: The first seven characters of the place_id are " ..."

Make sure to retreive the data from the URL specified above and not the normal Google API. Your program should work with the Google API - but the place_id may not match for this assignment.

place_id:
Python code:

X Tutup