Wednesday, February 10, 2010
Free Weather Web part
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
public partial class Weather : System.Web.UI.UserControl
{
protected int _HttpTimeout = 30;
protected DateTime _InstallDate;
protected string _LicenseKey = "";
protected string _Location = "USGA0029;INXX0102;INXX0130";
protected bool _ShowDetails;
protected bool _Units = false;
public static string licenseInfo = "";
protected void Page_Load(object sender, EventArgs e)
{
weathercontrol();
}
protected void weathercontrol()
{
string str = "";
string str2 = "";
int num = 1;
string str3 = this._LicenseKey;
if (num == 0)
{
}
else
{
if (!string.IsNullOrEmpty(this._Location))
{
string[] strArray2 = this._Location.Split(new char[] { ';' });
string str4 = "f";
if (this._Units)
{
str4 = "c";
}
foreach (string str5 in strArray2)
{
string key = "YahooWeather" + str5 + str4;
string str7 = null;
try
{
str7 = (string)HttpContext.Current.Cache[key];
}
catch (Exception exception)
{
Label1.Text=("Cache Fetch Error:" + exception.Message);
}
if (str7 == null)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weather.yahooapis.com/forecastrss?p=" + str5 + "&u=" + str4);
request.Timeout = this._HttpTimeout * 0x3e8;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
str7 = reader.ReadToEnd();
reader.Close();
response.Close();
}
catch (Exception exception2)
{
Label1.Text=("Error accessing the Yahoo weather service:" + exception2.Message);
return;
}
try
{
HttpContext.Current.Cache.Add(key, str7, null, DateTime.Now.AddHours(1.0), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
catch (Exception exception3)
{
Label1.Text=("Cache Add Error:" + exception3.Message);
}
}
try
{
XmlDocument document = new XmlDocument();
document.LoadXml(str7);
XmlNodeList list = document.SelectNodes("rss/channel/item/title");
string innerText = "";
string str9 = "";
foreach (XmlNode node in list)
{
innerText = node.InnerText;
if (innerText.StartsWith("Conditions for"))
{
innerText = innerText.Substring(14);
}
int index = innerText.IndexOf(" at");
if (index > 0)
{
str9 = innerText.Substring(index + 4);
innerText = innerText.Remove(index);
}
}
XmlNodeList list2 = document.SelectNodes("rss/channel/item/link");
string str10 = "";
foreach (XmlNode node2 in list2)
{
str10 = node2.InnerText;
}
string attribute = "";
foreach (XmlElement element in document.GetElementsByTagName("yweather:units"))
{
attribute = element.GetAttribute("temperature");
}
string str12 = "";
foreach (XmlElement element2 in document.GetElementsByTagName("yweather:condition"))
{
str12 = element2.GetAttribute("code");
}
string str13 = "";
string str14 = "";
if (this._ShowDetails)
{
foreach (XmlElement element3 in document.GetElementsByTagName("yweather:atmosphere"))
{
str13 = element3.GetAttribute("humidity");
}
foreach (XmlElement element4 in document.GetElementsByTagName("yweather:wind"))
{
str14 = element4.GetAttribute("speed");
}
}
string str16 = str + "<table border=0 cellpadding=2 cellspacing=0 width='20%'>";
//str = ((str16 + "<tr bgcolor=#f3f3f3><td colspan=2><strong><a href='" + str10 + "' target=_blank>" + innerText + "</a></strong></td>") + "<td align=right><font color=#2797C7>" + str9 + "</font></td></tr>") + "<tr bgcolor=#bcbcbc height=1><td colspan=3></td></tr>" + "<tr bgcolor=#fbfbfb>";
str = ((str16 + "<tr bgcolor=#f3f3f3><td colspan=3><strong>" + innerText + " </strong></td>") + "</tr>") + "<tr bgcolor=#bcbcbc height=1><td colspan=3></td></tr>" + "<tr bgcolor=#fbfbfb>";
foreach (XmlElement element5 in document.GetElementsByTagName("yweather:condition"))
{
str = str + "<td valign=top width=110 align=center style='border-right:1px dotted #D0D0D0;color:#808080'><br />";
string strimage = "http://l.yimg.com/a/i/us/we/52/" + str12 + ".gif";
str = str + "<img src='" + strimage + "'><br />";
//str = str + "<img src='" + this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "WeatherWebpart.Gif." + str12 + ".gif") + "'><br />";
str16 = str;
str = str16 + "<span style='font-size:14pt; font-family:arial,helvetica; line-height:18pt; color:#333333; font-weight:bold'>" + element5.GetAttribute("temp") + "°" + attribute + "</span><br />";
str = str + "<span style='color:gray'>" + element5.GetAttribute("text") + "</span>";
if (this._ShowDetails)
{
str = str + "<br />Humidity:" + str13 + "%";
str = str + "<br />Wind:" + str14 + " mph";
}
str = str + "</td>";
}
//foreach (XmlElement element6 in document.GetElementsByTagName("yweather:forecast"))
//{
// str = str + "<td width=110 valign=top align=center style='border-right:1px dotted #D0D0D0'>";
// string str15 = element6.GetAttribute("day").Replace("Sun", "Sunday").Replace("Mon", "Monday").Replace("Tue", "Tuesday").Replace("Wed", "Wednesday").Replace("Thu", "Thursday").Replace("Fri", "Friday").Replace("Sat", "Saturday");
// str = str + "<font color=#808080><b>" + str15 + "</b></font><br />";
// string strimage = "http://l.yimg.com/a/i/us/we/52/" + element6.GetAttribute("code") + ".gif";
// str = str + "<img src='" + strimage + "'><br />";
// //str = str + "<img src='" + this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "WeatherWebpart.Gif." + element6.GetAttribute("code") + ".gif") + "'><br />";
// str16 = str;
// str = str16 + "<span style='font-size:8pt; line-height:12pt; color:black; font-weight:bold'>" + element6.GetAttribute("low") + "°" + attribute + ".." + element6.GetAttribute("high") + "°" + attribute + "</span><br />";
// str = str + "<span style='color:gray'>" + element6.GetAttribute("text") + "</span>";
// str = str + "</td>";
//}
str = str + "</tr></table><br/>";
}
catch (Exception exception4)
{
Label1.Text=("Error parsing the Yahoo XML document:" + exception4.Message);
return;
}
}
}
else
{
str = "Please specify a location";
}
Label1.Text = str;
//if (str2 != "")
//{
// Response.Write("<br>" + str2);
//}
}
}
}