services-maps/php/library.php
2007-08-04 09:31:05 +00:00

200 lines
No EOL
6.8 KiB
PHP
Executable file

<?php
function movie2locations($id) {
$file = 'data/locations.data';
if (file_exists($file)) {
while (file_exists('data/locations.lock'))
usleep(100000);
$locations = unserialize(file_get_contents($file));
}
if (!$locations[$id]) {
$point = array();
if (strlen($id) == 7)
$html = file_get_contents('http://imdb.com/title/tt' . $id . '/locations');
$string = explode('&&locations=', $html);
for ($i = 1; $i < count($string); $i++) {
$key = count($point);
$substring = explode('&&', $string[$i]);
$point[$key]['location'] = urldecode($substring[0]);
if (strstr($point[$key]['location'], ' - '))
$point[$key]['location'] = substr($point[$key]['location'], strpos($point[$key]['location'], ' - ') + 3);
$point[$key]['level'] = 0;
$substring = explode('<dd>', $string[$i]);
$substring = explode('</dd>', $substring[1]);
$point[$key]['description'] = trim(strip_tags($substring[0]));
}
foreach ($point as $key => $value) {
$point[$key]['coordinates'] = location2coordinates($point[$key]['location']);
while ($point[$key]['coordinates']['latitude'] == 0 && $point[$key]['coordinates']['longitude'] == 0 && strstr($value['location'], ', ')) {
$point[$key]['level']++;
$value['location'] = substr($value['location'], strpos($value['location'], ', ') + 2);
$point[$key]['coordinates'] = location2coordinates($value['location']);
}
/*
if ($point[$key]['coordinates']['latitude'] != 0 || $point[$key]['coordinates']['longitude'] != 0) {
$point_ = $point;
foreach ($point_ as $key_ => $value_) {
if ($key != $key_ && $point[$key]['latitude'] == $point_[$key_]['latitude'] && $point[$key]['longitude'] == $point_[$key_]['longitude']) {
$md5 = md5($point[$key]['location']);
$offset[$key]['latitude'] = (hexdec(substr($md5, 0, 1)) - 8) * 0.00001;
$offset[$key]['longitude'] = (hexdec(substr($md5, 1, 1)) - 8) * 0.00001;
$md5 = md5($point[$key_]['location']);
$offset[$key_]['latitude'] = (hexdec(substr($md5, 0, 1)) - 8) * 0.00001;
$offset[$key_]['longitude'] = (hexdec(substr($md5, 1, 1)) - 8) * 0.00001;
}
}
}
*/
}
/*
foreach ($point as $key => $value) {
if ($offset[$key]['latitude'])
$point[$key]['coordinates']['latitude'] += $offset[$key]['latitude'];
if ($offset[$key]['longitude'])
$point[$key]['coordinates']['longitude'] += $offset[$key]['longitude'];
}
*/
$locations[$id] = $point;
// $locations_ = unserialize(file_get_contents($file));
// $locations_[$id] = $locations[$id];
// $locations = $locations_;
ksort($locations);
if (!file_exists('data/locations.lock')) {
touch('data/locations.lock');
file_put_contents($file, serialize($locations));
unlink('data/locations.lock');
}
}
return $locations[$id];
}
function location2coordinates($location) {
/*
translates a location name to coordinates (latitude/longitude)
*/
$file = 'data/coordinates.data';
if (file_exists($file)) {
while (file_exists('data/coordinates.lock'))
usleep(100000);
$coordinates = unserialize(file_get_contents($file));
}
if (!$coordinates[$location]) {
$query = urlencode($location);
$f = fsockopen('maps.google.com', 80, $err_num, $err_str, 60);
$string = 'GET /maps?q=' . $query . " HTTP/1.1\r\n";
$string .= "Accept: */*\r\n";
$string .= "Accept-Language: en\r\n";
$string .= "User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3\r\n";
$string .= "Connection: close\r\n";
$string .= "Host: maps.google.com\r\n";
$string .= "\r\n";
fwrite($f, $string);
$data = stream_get_contents($f);
fclose($f);
$code = substr($data, 9, 3);
if ($code == 200)
$html = substr($data, strpos($data, "\r\n\r\n") + 4);
else
$data = $code;
$substr = substr($html, strpos($html, '{center:{lat:') + 13);
$coordinates[$location]['latitude'] = substr($substr, 0, strpos($substr, ',lng:'));
$substr = substr($substr, strpos($substr, ',lng:') + 5);
$coordinates[$location]['longitude'] = substr($substr, 0, strpos($substr, '}'));
$substr = substr($html, strpos($html, ',span:{lat:') + 11);
$coordinates[$location]['spanLatitude'] = substr($substr, 0, strpos($substr, ',lng:'));
$substr = substr($substr, strpos($substr, ',lng:') + 5);
$coordinates[$location]['spanLongitude'] = substr($substr, 0, strpos($substr, '}'));
if ($location != 'USA' && $coordinates[$location]['latitude'] == 37.0625 && $coordinates[$location]['longitude'] == -95.677068) {
$coordinates[$location]['latitude'] = 0;
$coordinates[$location]['longitude'] = 0;
$coordinates[$location]['spanLatitude'] = 0;
$coordinates[$location]['spanLongitude'] = 0;
}
// /*
if (strstr($coordinates[$location]['latitude'] . $coordinates[$location]['longitude'] . $coordinates[$location]['spanLatitude'] . $coordinates[$location]['spanLongitude'], ' ')) {
$coordinates[$location]['latitude'] = 0;
$coordinates[$location]['longitude'] = 0;
$coordinates[$location]['spanLatitude'] = 0;
$coordinates[$location]['spanLongitude'] = 0;
}
// */
/*
foreach ($coordinates as $key => $value) {
echo $key;
if (strstr($value['latitude'] . $value['longitude'] . $value['spanLatitude'] . $value['spanLongitude'], ' ')) {
die($key);
unset($coordinates[$key]);
}
}
*/
ksort($coordinates);
while (file_exists('data/coordinates.lock'))
usleep(100000);
touch('data/coordinates.lock');
file_put_contents($file, serialize($coordinates));
if (filesize($file) > 100000)
copy($file, 'data/coordinates.backup.data');
unlink('data/coordinates.lock');
}
return $coordinates[$location];
}
function coordinates2string($latitude, $longitude) {
/*
translates coordinates (latitude/longitude) to string (DD°MM'SS"X)
*/
$latitudeDegrees = floor(abs($latitude));
$latitudeDecimals = abs($latitude) - $latitudeDegrees;
$latitudeMinutes = str_pad(floor($latitudeDecimals * 60), 2, '0', STR_PAD_LEFT);
$latitudeSeconds = str_pad(round(($latitudeDecimals - $latitudeMinutes / 60) * 3600), 2, '0', STR_PAD_LEFT);
$latitudeHemisphere = str_repeat('N', $latitude >= 0) . str_repeat('S', $latitude < 0);
$longitudeDegrees = floor(abs($longitude));
$longitudeDecimals = abs($longitude) - $longitudeDegrees;
$longitudeMinutes = str_pad(floor((abs($longitude) - $longitudeDegrees) * 60), 2, '0', STR_PAD_LEFT);
$longitudeSeconds = str_pad(round(($longitudeDecimals - $longitudeMinutes / 60) * 3600), 2, '0', STR_PAD_LEFT);
$longitudeHemisphere = str_repeat('W', $longitude <= 0) . str_repeat('E', $longitude > 0);
$string = $latitudeDegrees . '&deg;' . $latitudeMinutes . '&apos;' . $latitudeSeconds . '&quot;' . $latitudeHemisphere;
$string .= ' / ';
$string .= $longitudeDegrees . '&deg;' . $longitudeMinutes . '&apos;' . $longitudeSeconds . '&quot;' . $longitudeHemisphere;
return $string;
}
?>