forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_json.php
More file actions
165 lines (151 loc) · 5.76 KB
/
geo_json.php
File metadata and controls
165 lines (151 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
use \Tsugi\Util\Net;
use \Tsugi\Util\Mersenne_Twister;
$sanity = array(
'urllib' => '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?address=University+of+Michigan';
$sample_url = $api_url . '?address=' . urlencode($sample_location);
$actual_url = $api_url . '?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("<pre>\n");echo(LTI::jsonIndent(json_encode($sample_json)));echo("</pre>\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('<p class="alert alert-info">Your current grade on this assignment is: '.($RESULT->grade*100.0).'%</p>'."\n");
}
if ( $dueDate->message ) {
echo('<p style="color:red;">'.$dueDate->message.'</p>'."\n");
}
?>
<p>
<b>Calling a JSON API</b>
</p>
In this assignment you will write a Python program somewhat similar to
<a href="http://www.py4e.com/code3/geojson.py" target="_blank">http://www.py4e.com/code3/geojson.py</a>.
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
<b>place_id</b> from the JSON.
A place ID is a textual identifier that uniquely identifies a place as
within Google Maps.
</p>
<p>
<b>API End Points</b>
</p>
<p>
To complete this assignment, you should use this API endpoint that has a static subset
of the Google Data:
<pre>
<a href="<?= deHttps($api_url).'?' ?>" target="_blank"><?= deHttps($api_url) ?>?</a>
</pre>
This API uses the same parameter (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.
</p>
<p>
To call the API, you need to provide
address that you are requesting as the <b>address=</b> parameter that is
properly URL encoded using the <b>urllib.urlencode()</b> fuction as shown in
<a href="http://www.py4e.com/code3/geojson.py"
target="_blank">http://www.py4e.com/code3/geojson.py</a>
</p>
<!--
<p>
Just for fun, you can also test your program with the real Google API:
<pre>
<a href="<?= $google_api ?>" target="_blank"><?= $google_api ?></a>
</pre>
Since Google's data is always changing, the data returned from the Google API
could easily be different than from my local copy API. And the Google
API has rate limits. But your code should work with the Google API
with no modifications other than the base URL.
</p>
-->
<?php httpsWarning($api_url); ?>
<p><b>Test Data / Sample Execution</b></p>
<p>
You can test to see if your program is working with a
location of "<?= $sample_location ?>" which will have a
<b>place_id</b> of "<?= $sample_place ?>".
<pre>
$ python3 solution.py
Enter location: <?= $sample_location ?>
Retrieving http://...
Retrieved <?= $sample_count ?> characters
Place id <?= $sample_place ?>
</pre>
</p>
<p><b>Turn In</b></p>
<p>
Please run your program to find the <b>place_id</b> for this location:
<pre>
<?= $actual_location ?>
</pre>
Make sure to enter the name and case exactly as above
and enter the <b>place_id</b> and your Python code below.
Hint: The first seven characters of the <b>place_id</b>
are "<?= substr($actual_place,0,7) ?> ..."<br/>
</p>
<p>
Make sure to retreive the data from the URL specified above and <b>not</b> the
normal Google API. Your program should work with the Google API - but the
<b>place_id</b> may not match for this assignment.
</p>
<form method="post">
place_id: <input type="text" size="40" name="place_id">
<input type="submit" value="Submit Assignment"><br/>
Python code:<br/>
<textarea rows="20" style="width: 90%" name="code"></textarea><br/>
</form>