Created
April 4, 2009 22:44
-
-
Save satyr/90316 to your computer and use it in GitHub Desktop.
Weather Underground
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
prexml(); | |
const | |
Name = 'wunderground', | |
WU = 'http://www.wunderground.com/', | |
WUAPI = 'http://api.wunderground.com/auto/wui/geo/', | |
Base = '<style>'+ <![CDATA[ | |
dl, ol {margin:0 0 1em; padding:0} | |
ol {list-style:none} | |
dd {margin:0.4em 0 0.8em; min-height:50px} | |
dd img {float:left; padding-right:0.4em} | |
dd, .logo {clear:left} | |
ol {text-align:center} | |
li {display:inline-block} | |
.date {font:bolder 108% sans-serif} | |
.cond {display:none} | |
.icon:hover + .cond { | |
display:block; position:absolute; top:0; right:1ex; | |
z-index:2; font:bolder larger sans-serif} | |
.tmpr > .hi {color:#f99} | |
.tmpr > .lo {color:#99f} | |
.error {font-style:oblique; line-height:1.8} | |
.logo {display:inline-block} | |
.loading + .logo {opacity:0.4} | |
.error + .logo {opacity:0.7} | |
]]> +'</style>'+ | |
<div class={Name}><div id={Name}> </div | |
><a class="logo" accesskey="/" href={WU} | |
><img src="http://icons-pe.wxug.com/i/w/logo.gif" | |
width="120" height="70" border="0"/></a></div>; | |
function ok(c, forecast){ | |
var dl = <dl/>, ol = <ol/>; | |
for each(var {title, icon, fcttext} in forecast.txt_forecast.forecastday) | |
dl.appendChild( | |
(<dt class="date">{title +''}</dt>) + | |
(<dd><img src={wuicon(icon)}/>{fcttext +''}</dd>)); | |
for each(var {date, high, low, icon, conditions} | |
in forecast.simpleforecast.forecastday) | |
ol.appendChild( | |
<li/>.appendChild( | |
(<div class="date">{date.month +'/'+ date.day}</div>) + | |
(<img class="icon" src={wuicon(icon)}/>) + | |
(<div class="cond">{conditions +''}</div>) + | |
<div class="tmpr"/>.appendChild( | |
(<div class="hi">{wutmpr(high)}</div>) + | |
(<div class="lo">{wutmpr(low) }</div>)))); | |
if('dt' in dl || 'li' in ol){ | |
prexml(); | |
c.innerHTML = (dl + ol +'').replace(/amp;/g, ''); | |
c.className = ''; | |
} else ng(c, 'No results'); | |
} | |
//function wutmpr(t) t.celsius +'\u00b0C ('+ t.fahrenheit +'\u00b0F)'; | |
function wutmpr(t) t.celsius +'\u00b0C'; | |
function wuicon(i) 'http://icons-pe.wxug.com/i/c/a/'+ i +'.gif'; | |
function xml(x){ | |
XML.ignoreWhitespace = true; | |
return XML(x.replace(/^\s*<\?xml[^>]*>/, '')); | |
} | |
function prexml(){ | |
XML.prettyPrinting = XML.ignoreWhitespace = false; | |
} | |
function ng(c){ | |
c.textContent = Array.slice(arguments, 1).join(' '); | |
c.className = 'error'; | |
} | |
CmdUtils.CreateCommand({ | |
names: [Name, 'weather', 'forecast'], | |
description: 'Weather Underground'.link(WU), | |
help: | |
'To set your default location manually, hold <kbd>shift</kbd> and execute.', | |
icon: 'chrome://ubiquity/skin/icons/wunderground.ico', | |
argument: { | |
name: 'WUGeoLookup', | |
label: 'location', | |
rankLast: true, | |
default: function wug_default() CmdUtils.makeSugg( | |
Bin.defaultLocation() || | |
this.format(CmdUtils.getGeoLocation()) || | |
""), | |
suggest: function wug_suggest(txt, htm, cb, sx){ | |
if(!txt) return []; | |
var ss = [CmdUtils.makeSugg(txt, htm, null, .5, sx)]; | |
sx || let({format} = this) ss.push($.ajax({ | |
url: WUAPI +'GeoLookupXML/index.xml', | |
data: {query: txt}, | |
dataType: 'text', | |
success: function wug_success(x){ | |
if(/<wui_error>/.test(x)) return; | |
x = xml(x); | |
if(x.name() == 'location') x = <l/>.appendChild(x); | |
var dic = {__proto__: null}; | |
for each(let l in x) if([email protected](-4) === 'CITY') | |
dic[l.name +'' || format(l)] = 1; | |
for([] in dic){ | |
cb([CmdUtils.makeSugg(t, "", null, .4) for(t in dic)]); | |
break; | |
} | |
}})); | |
return ss; | |
}, | |
format: function wug_format(loc) | |
loc && loc.city +', '+ loc[loc.state +'' ? 'state' : 'country'], | |
}, | |
execute: function wu_execute({object: {text}}){ | |
if(context.chromeWindow.gUbiquity.lastKeyEvent.shiftKey){ | |
displayMessage('Default location: '+ Bin.defaultLocation(text), this); | |
return; | |
} | |
Utils.openUrlInBrowser(WU +'cgi-bin/findweather/getForecast?query='+ | |
encodeURIComponent(text)); | |
}, | |
preview: function wu_preview(pb, {object: {text}}){ | |
var cn; | |
while(!(cn = pb.ownerDocument.getElementById(Name))) pb.innerHTML = Base; | |
if(!text) return; | |
cn.className = 'loading'; | |
CmdUtils.previewAjax(pb, { | |
url: WUAPI +'ForecastXML/index.xml', | |
data: {query: text}, | |
dataType: 'text', | |
success: function wu_success(x){ | |
((x = xml(x)).name() == 'wui_error' | |
? ng(cn, x.title, x.description) | |
: ok(cn, x)); | |
}, | |
error: function wu_error(x, s) ng(cn, x.status, x.statusText), | |
}); | |
}, | |
author: 'satyr', license: 'MIT', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment