-- Weather.lua-- Script for /weather command-- Weather formatting and displaying. temp is a numberfunction displayWeather(location, temp, conditions) -- Ok, we have the temperature and weather, spew it out: local weatherString = ".me looks out the window at " .. location .. " and feels it's <-c00ff00><-b>" .. temp .. "'C<-0><-cffff00>, <-0><-b>" .. conditions .. " <-c0000ff><-b>O"; for i=1,temp do weatherString = weatherString .. "_"; end sendln(weatherString .. "O");end-- httpGet callbackfunction weatherCallback(addr, response, header, content, error) -- Check for errors if response ~= 200 then println("<-cff0000>Failed to get current weather. Error: " .. error); return; end -- Convert content to lower case local lowerContent = string.lower(content); -- Loop through the tags till we find one with the weather string in it local location, temp, conditions; local startPos = 1; local contentLength = string.len(lowerContent); repeat -- Find tag start and end pos local tagStart = string.find(lowerContent, "", startPos); if tagStart== nil then println("<-cff0000>Failed to get current weather. Could not find weather in returned data."); return; end local tagEnd = string.find(lowerContent, "", tagStart+7); if tagEnd == nil then println("<-cff0000>Failed to get current weather. Could not find weather in returned data."); return; end startPos = tagEnd+8; -- Is this the tag we're looking for? (Contains some data after the word Weather local tag = string.sub(lowerContent, tagStart+7, tagEnd-1); local weatherPos = string.find(tag, "weather"); if weatherPos ~= nil and string.len(tag)-(weatherPos+7) > 2 then -- Looks reasonable, grab location (Everything before first comma) local commaPos = string.find(tag, ","); if commaPos ~= nil then location = string.sub(content, tagStart+7, tagStart+7+commaPos-1); end -- Trim off everything up to and including "weather :: " local weatherString = string.sub(content, tagStart+7+weatherPos+10, tagEnd-1); local weatherStringLwr = string.lower(weatherString); -- Extract temp local tempPos = string.find(weatherStringLwr, "c"); if tempPos ~= nil then temp = tonumber(string.sub(weatherString, 1, tempPos-1)); conditions = string.sub(weatherString, tempPos+2); end end until temp ~= nill; -- Ok, we have the temperature and weather, spew it out displayWeather(location, temp, conditions);end-- Command to registerfunction cmdWeather(params) -- Grab page local addr = getConfigValue("WeatherURL"); httpGet(addr, "weatherCallback");endregister_command("weather", "cmdWeather");
Yeah, that could probably be neater, but it does the job, and it's a proof of concept. There's also a PHP-style date() function (Well, getTime()), and a getCurrSong() which returns your current Winamp track - which is in a plugin DLL, so I can easily swap in new versions to support stuff like WMP, etc.
Anyway, enough rambling. I'm off to bed.