Skip to content

Instantly share code, notes, and snippets.

@sublimecoder
Created May 5, 2014 16:10
Show Gist options
  • Save sublimecoder/6089c38e27d6a7f860dc to your computer and use it in GitHub Desktop.
Save sublimecoder/6089c38e27d6a7f860dc to your computer and use it in GitHub Desktop.
A simple file for pulling weather data from an API
<cfscript>
zipcode = isDefined("form.q")?form.q:url.q;
zipcode = trim(zipcode);
path="#expandpath('.')#\weatherData\zip-#zipcode#.txt";
smlImgPath="#expandpath('.')#\weatherData\images\small\";
lrgImgPath="#expandpath('.')#\weatherData\images\large\";
// check the ages and existance of the current cached weather file.
if (fileExists(path)){
info = GetFileInfo(path);
if (dateDiff("n", info.lastmodified, now()) >= 60){
updateWeather(zipcode, path);
} else {
showWeather();
}
} else {
updateWeather(zipcode, path);
}
// Update weather function.
// This function checks for new weather updates from worldweatheronline.com's API
// if the call to the API is unsuccessful it will just show the cached weather data
// if the call is successfull it caches the json in a txt file with the coresponding zipcode and shows the updated data
// This function also caches the images for each type of weather to a local cache
function updateWeather(zip,path){
/* create new http service */
httpService = new http();
/* set attributes using implicit setters */
httpService.setMethod("get");
httpService.setCharset("UTF-8");
httpService.setUrl("http://api.worldweatheronline.com/free/v1/weather.ashx?q=#arguments.zip#&format=json&num_of_days=4&key=INSERT_API_KEY_HERE");
/* make the http call to the URL using send() */
result = httpService.send().getPrefix();
//writedump(result);
if(result.Responseheader.Status_Code == "200"){
jsonData = result.fileContent.toString();
wd = deserializeJSON(jsonData);
wd.data.current_condition[1].observation_time = TimeFormat(Now(), "hh:mm tt");
jsonData = serializeJSON(wd);
if (FileExists(arguments.path)){
FileDelete(arguments.path);
}
FileWrite(arguments.path, jsonData);
wcStruct = {};
structInsert(wcStruct,wd.data.current_condition[1].weatherCode,"",true);
structInsert(wcStruct,wd.data.weather[1].weatherCode,"",true);
structInsert(wcStruct,wd.data.weather[2].weatherCode,"",true);
structInsert(wcStruct,wd.data.weather[3].weatherCode,"",true);
structInsert(wcStruct,wd.data.weather[4].weatherCode,"",true);
// loop over the images and cache them locally.
for(weatherCode in wcStruct){
// Save Small Image
if(!fileExists(smlImgPath & weatherCode & "_day_sm.png")){
smallImg = ImageNew("http://c358489.r89.cf1.rackcdn.com/images/weather/small/" & weatherCode & "_day_sm.png");
ImageWrite(smallImg,smlImgPath & weatherCode & "_day_sm.png");
}
// Save Large Image
if(!fileExists(lrgImgPath & weatherCode & "_day_lg.png")){
largeImg = ImageNew("http://c358489.r89.cf1.rackcdn.com/images/weather/large/" & weatherCode & "_day_lg.png");
ImageWrite(largeImg,lrgImgPath & weatherCode & "_day_lg.png");
}
}
showWeather();
} else {
showWeather();
}
}
</cfscript>
<!---
the function for showing weather
--->
<cffunction name="showWeather">
<cfif fileExists(path)>
<cfheader name="Content-Type" value="application/json">
<cfprocessingdirective SUPPRESSWHITESPACE = "true"><cfscript> myfile = FileRead(path); WriteOutput(myfile);</cfscript></cfprocessingdirective>
</cfif>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment