-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib_enplacify_dopplr.php
127 lines (88 loc) · 2.29 KB
/
lib_enplacify_dopplr.php
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
<?php
#
# $Id$
#
######################################################
function enplacify_dopplr_uri($uri){
$parts = enplacify_dopplr_uri_to_id($uri);
if (! $parts){
return array(
'ok' => 0,
'error' => 'failed to recognize place id',
);
}
list($place_type, $place_id) = $parts;
$rsp = enplacify_dopplr_get_place($place_type, $place_id);
if (! $rsp['ok']){
return $rsp;
}
$place = $rsp['place'];
$place['derived_from'] = 'dopplr';
$place['derived_from_id'] = "{$place_type}:{$place_id}";
return array(
'ok' => 1,
'place' => $place,
);
}
######################################################
function enplacify_dopplr_uri_to_id($uri){
$place_type = null;
$place_id = null;
$uris = $GLOBALS['cfg']['enplacify']['dopplr']['uris'];
foreach ($uris as $pattern){
if (preg_match($pattern, $uri, $m)){
$place_type = $m[1];
$place_id = $m[2];
break;
}
}
if ($place_type && $place_id){
return array($place_type, $place_id);
}
return null;
}
######################################################
function enplacify_dopplr_get_place($place_type, $place_id){
$cache_key = "enplacify_dopplr_{$place_type}_{$place_id}";
$cache = cache_get($cache_key);
if ($cache['ok']){
return $cache['data'];
}
$url = "http://dplr.it/" . urlencode($place_type) . "/" . urlencode($place_id);
$headers = array();
$more = array('follow_redirects' => 1);
$rsp = http_get($url, $headers, $more);
if (! $rsp['ok']){
return $rsp;
}
# https://github.com/mncaudill/flickr-machinetag-geo/blob/master/src/parsers/dopplr.php
if (preg_match('#pointLat:(-?\d.*),#U', $rsp['body'], $m)){
$latitude = floatval($m[1]);
}
if (preg_match('#pointLng:(-?\d.*),#U', $rsp['body'], $m)){
$longitude = floatval($m[1]);
}
if ((! $latitude) || (! $longitude)){
return array(
'ok' => 0,
'error' => 'failed to locate lat,lon data'
);
}
if (preg_match('#<title>(.*)\|#U', $rsp['body'], $m)){
$name = trim($m[1]);
}
$place = array(
'latitude' => $latitude,
'longitude' => $longitude,
'url' => $url,
'name' => $name,
);
$rsp = array(
'ok' => 1,
'place' => $place,
);
cache_set($cache_key, $rsp);
return $rsp;
}
######################################################
?>