2011-06-02 10:00:16 +04:00
|
|
|
# Get weather info for given address (or for the default one, "Mountain View")
|
|
|
|
|
2011-09-13 00:17:54 +04:00
|
|
|
page = require('webpage').create()
|
2011-06-02 10:00:16 +04:00
|
|
|
address = 'Mountain View' #< default value
|
|
|
|
|
|
|
|
# Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
|
|
|
|
page.onConsoleMessage = (msg) ->
|
|
|
|
console.log msg
|
|
|
|
|
|
|
|
# Print usage message, if no twitter ID is passed
|
|
|
|
if phantom.args.length < 1
|
|
|
|
console.log 'Usage: weather.coffee [address]'
|
2011-04-22 19:39:53 +04:00
|
|
|
else
|
2011-06-02 10:00:16 +04:00
|
|
|
address = phantom.args.join ' '
|
|
|
|
|
|
|
|
# Heading
|
|
|
|
console.log "*** Loading weather information for '#{address}' ***\n"
|
|
|
|
|
|
|
|
# Open Google "secret" Weather API and, onPageLoad, do...
|
|
|
|
page.open encodeURI("http://www.google.com/ig/api?weather=#{address}"), (status) ->
|
|
|
|
# Check for page load success
|
|
|
|
if status isnt 'success'
|
2011-04-22 19:39:53 +04:00
|
|
|
console.log 'Unable to access network'
|
|
|
|
else
|
2011-06-02 10:00:16 +04:00
|
|
|
# Execute some DOM inspection within the page context
|
|
|
|
page.evaluate ->
|
|
|
|
if document.querySelectorAll('problem_cause').length > 0
|
|
|
|
console.log "No data available for #{address}"
|
|
|
|
else
|
|
|
|
data = (s, e) ->
|
|
|
|
e = e or document
|
|
|
|
el = e.querySelector s
|
|
|
|
if el then el.attributes.data.value else undefined
|
2011-04-22 19:39:53 +04:00
|
|
|
|
2011-06-03 02:32:14 +04:00
|
|
|
console.log """City: #{data 'weather > forecast_information > city'}
|
|
|
|
Current condition: #{data 'weather > current_conditions > condition'}
|
|
|
|
Temperature: #{data 'weather > current_conditions > temp_f'} F
|
|
|
|
#{data 'weather > current_conditions > humidity'}
|
|
|
|
#{data 'weather > current_conditions > wind_condition'}\n
|
|
|
|
"""
|
2011-04-22 19:39:53 +04:00
|
|
|
|
2011-06-02 10:00:16 +04:00
|
|
|
forecasts = document.querySelectorAll 'weather > forecast_conditions'
|
|
|
|
for i in forecasts
|
|
|
|
console.log "#{ data 'day_of_week', i }: " +
|
|
|
|
"#{ data 'low', i }-" +
|
|
|
|
"#{ data 'high', i } F " +
|
|
|
|
"#{ data 'condition', i }"
|
2011-04-22 19:39:53 +04:00
|
|
|
phantom.exit()
|