jimk72 Wrote:Anyway I am looking into accuweather more for private use though as there is a limit on the number of requests an account can have per day. Is there one that is more open to unlimited requests?
Google has a weather API that works extremely well - I've used it in the past. Some great things about it are:
- Picks up nearly any city you can think of
- Offers translated forecasts directly from Google
- Very simple to pull info
- Generous per-day request (my program gets somewhere in the neighbourhood of 10k unique requests a day)
Obviously, Team-XBMC would see a fairly significant increase in the per-day requests but it shouldn't be too much of a problem.
Here's the C# code I made to tap in the Google Weather API just to show how easy it is.
Code:
namespace GoogleWeatherAPI
{
class Weather
{
/// <summary>
/// The function that returns the current conditions for the specified location.
/// </summary>
/// <param name="location">City or ZIP code</param>
/// <returns></returns>
public static Conditions GetCurrentConditions(string location)
{
Conditions conditions = new Conditions();
using (StreamReader reader = new StreamReader(WebRequest.Create(string.Format("http://www.google.com/ig/api?weather={0}&hl={1}", location, GetLanguageCode())).GetResponse().GetResponseStream(), Encoding.Default, true))
{
XmlDocument xmlConditions = new XmlDocument();
xmlConditions.Load(reader);
if (xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
{
conditions = null;
}
else
{
try
{
conditions.City = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
if (conditions.City.Contains(',') && conditions.City.Length > 20)
conditions.City = conditions.City.Substring(0, conditions.City.IndexOf(','));
conditions.Time = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/current_date_time").Attributes["data"].InnerText;
conditions.Units = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/unit_system").Attributes["data"].InnerText;
conditions.Condition = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes["data"].InnerText;
conditions.TempC = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText;
conditions.TempF = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f").Attributes["data"].InnerText;
conditions.Humidity = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText;
conditions.Wind = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes["data"].InnerText;
conditions.Icon = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/icon").Attributes["data"].InnerText;
}
catch { }
}
reader.Close();
reader.Dispose();
}
return conditions;
}
/// <summary>
/// The function that gets the forecast for the next four days.
/// </summary>
/// <param name="location">City or ZIP code</param>
/// <returns></returns>
public static List<Conditions> GetForecast(string location)
{
List<Conditions> conditions = new List<Conditions>();
using (StreamReader reader = new StreamReader(WebRequest.Create(string.Format("http://www.google.com/ig/api?weather={0}&hl={1}", location, GetLanguageCode())).GetResponse().GetResponseStream(), Encoding.Default, true))
{
XmlDocument xmlConditions = new XmlDocument();
xmlConditions.Load(reader);
if (xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
{
conditions = null;
}
else
{
foreach (XmlNode node in xmlConditions.SelectNodes("/xml_api_reply/weather/forecast_conditions"))
{
Conditions condition = new Conditions();
condition.City = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
condition.Units = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/unit_system").Attributes["data"].InnerText;
if (condition.City.Contains(',') && condition.City.Length > 20)
condition.City = condition.City.Substring(0, condition.City.IndexOf(','));
condition.Condition = node.SelectSingleNode("condition").Attributes["data"].InnerText;
condition.High = node.SelectSingleNode("high").Attributes["data"].InnerText;
condition.Low = node.SelectSingleNode("low").Attributes["data"].InnerText;
condition.DayOfWeek = node.SelectSingleNode("day_of_week").Attributes["data"].InnerText;
condition.Icon = node.SelectSingleNode("icon").Attributes["data"].InnerText;
conditions.Add(condition);
}
}
reader.Close();
reader.Dispose();
}
return conditions;
}
private static string GetLanguageCode()
{
return System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
}
}
public class Conditions
{
string city = "No Data";
string time = DateTime.Now.ToString();
string units = "US";
string dayOfWeek = DateTime.Now.DayOfWeek.ToString();
string condition = "No Data";
string tempF = "No Data";
string tempC = "No Data";
string humidity = "No Data";
string wind = "No Data";
string high = "No Data";
string low = "No Data";
string icon = string.Empty;
public string City
{
get { return city; }
set { city = value; }
}
public string Time
{
get { return time; }
set { time = value; }
}
public string Units
{
get { return units; }
set { units = value; }
}
public string Condition
{
get { return condition; }
set { condition = value; }
}
public string TempF
{
get { return tempF; }
set { tempF = value; }
}
public string TempC
{
get { return tempC; }
set { tempC = value; }
}
public string Humidity
{
get { return humidity; }
set { humidity = value; }
}
public string Wind
{
get { return wind; }
set { wind = value; }
}
public string DayOfWeek
{
get { return dayOfWeek; }
set { dayOfWeek = value; }
}
public string High
{
get { return high; }
set { high = value; }
}
public string Low
{
get { return low; }
set { low = value; }
}
public string Icon
{
get { return icon; }
set { icon = value; }
}
}
}