forked from Napafilia/napafilia-forecast
/
yql.php
57 lines (46 loc) · 1.3 KB
/
yql.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
<?php
/* Proxy-cache for Yahoo Weather queries... */
$cachetime = 10800; // 3h
function sendWeather ($cachefile, $callback)
{
// Get weather data from local cache
if (file_exists ($cachefile))
{
$content = file_get_contents ("$cachefile");
$content = preg_replace('/^[^\(]+/', "$callback", $content);
echo $content;
return true;
}
return false;
}
// Try to update the 'cachefile'
function updateCache ($cachefile)
{
// URL to access
$query = $_SERVER['QUERY_STRING'];
$url = "http://query.yahooapis.com/v1/public/yql?$query";
system ("echo \"$url\" >> /tmp/debug");
system ("wget -T3 -q -O $cachefile.tmp '$url' && mv $cachefile.tmp $cachefile 2>&1 &");
return true;
}
// Configure Timeout
$context = stream_context_create(array(
'http' => array(
'timeout' => 5, // 5 seconds
)
));
// Get callback ID
$callback = isset ($_GET['callback']) ? $_GET['callback'] : 'undefined';
$hash = "/tmp/". md5 ($_SERVER['HTTP_REFERER']);
$displayed = sendWeather ("$hash.weather", $callback);
$updated = false;
if (!file_exists ("$hash.weather") ||
(time() - filemtime("$hash.weather") > $cachetime))
{
$updated = updateCache ("$hash.weather");
}
if (! $displayed && $updated)
{
sendWeather("$hash.weather", $callback);
}
?>