Skip to content

Instantly share code, notes, and snippets.

@jameswburke
Created December 26, 2011 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswburke/1522220 to your computer and use it in GitHub Desktop.
Save jameswburke/1522220 to your computer and use it in GitHub Desktop.
Coldfusion Goolge Weather Caching
<!--- Try to read the file --->
<cftry>
<cffile
action = "read"
file = "C:\weatherCache.txt"
variable = "readWeather" />
<!--- In the event on an error (usually the file is missing) ping google weather and get the data --->
<cfcatch type="Any">
<cfhttp url="http://www.google.com/ig/api?weather=New+York" method="get" resolveURL="no" timeout="3" result="objGet">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<!--- Get data from XML --->
<cfset objRSS = xmlParse(#objGet.filecontent#)>
<cfset temp = lcase(#objRSS.XmlRoot.XmlChildren[1].current_conditions.temp_f.XmlAttributes.data#)>
<cfset condition = #objRSS.XmlRoot.XmlChildren[1].current_conditions.condition.XmlAttributes.data#>
<cfset conditionlc = replace(lcase(condition), " ", "")>
<cffile action = "write"
file = "C:\weatherCache.txt"
output = "#Now()#,#temp#,#condition#,#conditionlc#">
<cffile
action = "read"
file = "C:\weatherCache.txt"
variable = "readWeather" />
</cfcatch>
</cftry>
<!--- Explode into array --->
<cfset myArray = listToArray(#readWeather#) />
<!--- Set timeout to 30 minutes --->
<cfif DateAdd("n", 30, myArray[1]) lt Now()>
<!--- If True: Hit the google server, create new variables, save file --->
<!--- Duplicate code as above, too lazy to refactor right now --->
<cfhttp url="http://www.google.com/ig/api?weather=New+York" method="get" resolveURL="no" timeout="3" result="objGet">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset objRSS = xmlParse(#objGet.filecontent#)>
<cfset temp = lcase(#objRSS.XmlRoot.XmlChildren[1].current_conditions.temp_f.XmlAttributes.data#)>
<cfset condition = #objRSS.XmlRoot.XmlChildren[1].current_conditions.condition.XmlAttributes.data#>
<cfset conditionlc = replace(lcase(condition), " ", "")>
<cffile action = "write"
file = "C:\weatherCache.txt"
output = "#Now()#,#temp#,#condition#,#conditionlc#">
<cfelse>
<!--- Else: Use the cached information --->
<cfset temp = myArray[2]>
<cfset condition = myArray[3]>
<cfset conditionlc = myArray[4]>
</cfif>
<div id="weather">
<ul>
<li><i>Temp: </i><cfoutput>#temp#</cfoutput>&deg;</li>
<li><i>Condition: </i><cfoutput>#condition#</cfoutput></li>
<li><i>Image URL: </i><cfoutput>#conditionlc#</cfoutput>.png</li>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment