-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonFetch.php
executable file
·164 lines (147 loc) · 4.58 KB
/
jsonFetch.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
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
<?php
$jsonCondFile = "jsonCondResponse.txt";
$jsonForeFile = "jsonForeResponse.txt";
$jsonAstrFile = "jsonAstrResponse.txt";
$conditions = null;
$forecast = null;
$astronomy = null;
include('config.php');
// withinLastNMinutes
// Time Number -> Boolean
// Returns true if the given time is
// within the last n minutes
function withinLastNMinutes($t, $n){
$comp = (time() - ($n * 60));
return ($comp < $t);
}
// fileOkay
// File -> Boolean
// Returns true if the file does not
// need updating
function fileOkay($f){
return (file_exists($f) && withinLastNMinutes(filemtime($f), 10));
}
// timeToGet
// [None] -> Boolean
// Returns true if it is time to fetch
// the JSON from Weather Underground
function timeToGet(){
return !(fileOkay($GLOBALS['jsonCondFile'])
&& fileOkay($GLOBALS['jsonForeFile'])
&& fileOkay($GLOBALS['jsonAstrFile']));
}
// updateJSONFile
// [Void]
// Re-syncs the JSON File with Weather
// Underground Data
function updateJSONFile(){
$ch = curl_init("http://api.wunderground.com/api/".$GLOBALS['API_KEY']."/conditions/q/MA/Boston.json");
$json_string = "to-json";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string,
CURLOPT_HEADER => false
);
curl_setopt_array( $ch, $options );
$req = curl_exec($ch);
//$file = fopen($GLOBALS['jsonCondFile'], "w");
file_put_contents($GLOBALS['jsonCondFile'], $req);
curl_close($ch);
//fclose($file);
$ch = curl_init("http://api.wunderground.com/api/".$GLOBALS['API_KEY']."/forecast/q/MA/Boston.json");
$json_string = "to-json";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string,
CURLOPT_HEADER => false
);
curl_setopt_array( $ch, $options );
$req = curl_exec($ch);
//$file = fopen($GLOBALS['jsonForeFile'], "w");
file_put_contents($GLOBALS['jsonForeFile'], $req);
curl_close($ch);
//fclose($file);
$ch = curl_init("http://api.wunderground.com/api/".$GLOBALS['API_KEY']."/astronomy/q/MA/Boston.json");
$json_string = "to-json";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string,
CURLOPT_HEADER => false
);
curl_setopt_array( $ch, $options );
$req = curl_exec($ch);
//echo $GLOBALS['jsonAstrFile'];
//$file = fopen($GLOBALS['jsonAstrFile'], "w");
file_put_contents($GLOBALS['jsonAstrFile'], $req);
curl_close($ch);
//fclose($file);
}
// updateVars:
// [Void]
// Updates the Global Variables $conditions,
// $forecast, and $astronomy
function updateVars(){
$fCond = file_get_contents($GLOBALS['jsonCondFile']);
$fFore = file_get_contents($GLOBALS['jsonForeFile']);
$fAstr = file_get_contents($GLOBALS['jsonAstrFile']);
$GLOBALS['conditions'] = json_decode($fCond);
$GLOBALS['forecast'] = json_decode($fFore);
$GLOBALS['astronomy'] = json_decode($fAstr);
}
// setup
// [Void]
// Runs Initial Setup
function setup(){
if (timeToGet()){
updateJSONFile();
}
updateVars();
}
// getNDateString
// Number -> String
// Returns the Date String from the n'th
// forecast day
function getNDateString($n){
$fday = $GLOBALS['forecast']->forecast->simpleforecast->forecastday;
return join(" ",array($fday[$n]->date->weekday_short,
$fday[$n]->date->day,
$fday[$n]->date->monthname_short));
}
// getNHighString
// Number -> String
// Returns the Weather High String from the
// n'th forecast day
function getNHighString($n){
$fday = $GLOBALS['forecast']->forecast->simpleforecast->forecastday;
return join("",array("High: ",$fday[$n]->high->fahrenheit," F (",
$fday[$n]->high->celsius," C)"));
}
// getNLowString
// Number -> String
// Returns the Weather Low String from the
// n'th forecast day
function getNLowString($n){
$fday = $GLOBALS['forecast']->forecast->simpleforecast->forecastday;
return join("",array("Low: ",$fday[$n]->low->fahrenheit," F (",
$fday[$n]->low->celsius," C)"));
}
// getNImageString
// Number -> ImageString
// Returns the icon string for the given
// forecast day
function getNImageString($n){
$fday = $GLOBALS['forecast']->forecast->simpleforecast->forecastday;
return $fday[$n]->icon;
}
// getNWeatherString
// Number -> String
// Returns the weather string for the given
// forecast day
function getNWeatherString($n){
$fday = $GLOBALS['forecast']->forecast->simpleforecast->forecastday;
return $fday[$n]->conditions;
}
?>