Created
December 26, 2011 22:27
-
-
Save jameswburke/1522220 to your computer and use it in GitHub Desktop.
Coldfusion Goolge Weather Caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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>°</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