[중간완성품]
[소스]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Diagnostics;
using System.Xml;
using System.Xml.Linq;
namespace Mantomangag
{
public partial class Page : UserControl
{
public readonly string[] weather_code = new string[] { “tornado”, “tropical storm”, “hurricane”, “severe thunderstorms”,
“thunderstorms”, “mixed rain and snow”, “mixed rain and sleet”, “mixed snow and sleet”, “freezing drizzle”, “drizzle”,
“freezing rain”, “showers”, “showers”, “snow flurries”, “light snow showers”, “blowing snow”, “snow”, “hail”, “sleet”,
“dust”, “foggy”, “haze”, “smoky”, “blustery”, “windy”, “cold”, “cloudy”, “mostly cloudy (night)”, “mostly cloudy (day)”,
“partly cloudy (night)”, “partly cloudy (day)”, “clear (night)”, “sunny”, “fair (night)”, “fair (day)”,
“mixed rain and hail”, “hot”, “isolated thunderstorms”, “scattered thunderstorms”, “scattered thunderstorms”,
“scattered showers”, “heavy snow”, “scattered snow showers”, “heavy snow”, “partly cloudy”, “thundershowers”,
“snow showers”, “isolated thundershowers” };
public readonly string APP_ID = “2704f87b1fdfa783b429adf96d026015”;
public readonly string[] people = new string[] {“people”, “singer”, “foreignsinger”,”talent”, “actor”, “foreignactor”, “entertainer”
, “broadcaster”, “comedian”,”sportsstar”, “soccer”, “foreignsoccer”};
public readonly string[] value = new string[] {“movie”, “schedulemovie”,”drama”, “broadcast”, “game”,”perform”, “book” };
public readonly string RANK = “ranktheme”;
public int i = 0;
public int j = 0;
static Random rand = new Random();
public Page()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
text.Text = “”;
text.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, (byte)rand.Next(50, 255), (byte)rand.Next(50, 255)));
Load_weather();
for (i = 0; i < 3; i++)
{
Load_rank(i);
}
}
private void Load_rank(int i)
{
if (i == 0 || i == 1)
{
string rankUrlTemplate = “http://openapi.naver.com/search?key={0}&query={1}&target={2}”;
string rankUrl = string.Format(rankUrlTemplate, APP_ID, people[rand.Next(12)], RANK);
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(rankUrl, UriKind.Absolute));
}
if (i == 2)
{
string rankUrlTemplate = “http://openapi.naver.com/search?key={0}&query={1}&target={2}”;
string rankUrl = string.Format(rankUrlTemplate, APP_ID, value[rand.Next(7)], RANK);
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(rankUrl, UriKind.Absolute));
}
}
private void XmlToRank(string xml)
{
int randomNumber = rand.Next(1, 11);
string entityName = “R” + randomNumber.ToString();
XDocument xDoc = XDocument.Parse(xml);
var query = from result in xDoc.Descendants(“result”)
select new
{
data = result.Element(“item”).Element(entityName).Element(“K”).Value
};
foreach (var item in query)
{
DisplayGag(item.data);
}
}
private void XmlToWeather(string xml)
{
XNamespace yweather = “http://xml.weather.yahoo.com/ns/rss/1.0“;
XDocument xDoc = XDocument.Parse(xml);
var query = from result in xDoc.Descendants(“channel”)
select new
{
data = result.Element(“item”).Element(yweather + “condition”).Attribute(“code”).Value
};
var query2 = from result in xDoc.Descendants(“channel”)
select new
{
data2 = result.Element(“item”).Element(yweather + “condition”).Attribute(“temp”).Value
};
foreach (var item in query)
{
DisplayWeather(item.data);
}
foreach (var item2 in query2)
{
DisplayTemp(item2.data2);
}
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled == true)
return;
if (e.Error != null)
{
Debug.WriteLine(e.Error.Message);
return;
}
XmlToRank(e.Result);
}
void wc_DownloadStringCompleted2(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled == true)
return;
if (e.Error != null)
{
Debug.WriteLine(e.Error.Message);
return;
}
XmlToWeather(e.Result);
}
public void DisplayGag(string gag)
{
text.Text = text.Text + gag;
if (j == 0) {
text.Text += “는 “;
}
if (j == 1)
{
if (rand.Next(1, 3) == 1)
{
text.Text += “를 “;
}
else
{
text.Text += “과 “;
}
}
if (j == 2)
{
text.Text += “했다.”;
}
j++;
if (j == 3)
{
Ganji.Begin();
}
}
private void DisplayWeather(string weather)
{
text_Weather.Text = weather_code[Convert.ToInt32(weather)];
}
private void DisplayTemp(string temp)
{
//double temp_cal = Convert.ToDouble(temp);
double temp_cal = Convert.ToDouble(Convert.ToInt32((Convert.ToDouble(temp) – 32) / 1.8 * 1000)) / 1000;
//double temp_cal = 10;
string temp_cal2 = temp_cal.ToString();
text_Temp.Text = temp_cal2 + “℃ “;
if (temp_cal < -10)
{
Color_Set(0, 255);
}
else if (temp_cal < 0)
{
Color_Set(127, 255);
}
else if (temp_cal < 10)
{
Color_Set(255, 255);
}
else if (temp_cal < 20)
{
Color_Set(255, 127);
}
else if (temp_cal > 20)
{
Color_Set(255, 0);
}
}
private void Color_Set(byte col1, byte col2)
{
Gra_1.Color = Color.FromArgb(100, col1, 0, col2);
Gra_2.Color = Color.FromArgb(0, col1, 0, col2);
}
private void Ganji_Completed(object sender, EventArgs e)
{
j = 0;
text.Text = “”;
Load_weather();
text.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(50,255), (byte)rand.Next(50,255), 255));
for (i = 0; i < 3; i++)
{
Load_rank(i);
}
}
private void text_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
text.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, (byte)rand.Next(50, 255), (byte)rand.Next(50, 255)));
}
private void Load_weather()
{
string weatherUrlTemplate = “http://weather.yahooapis.com/forecastrss?p={0}&u=f“;
string weatherUrl = string.Format(weatherUrlTemplate, “KSXX0037”);
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted2);
wc.DownloadStringAsync(new Uri(weatherUrl, UriKind.Absolute));
}
private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
BGM.Stop();
BGM.Play();
}
}
}
이름도 거창한! 맨투맨 개그 업데이트 시켜보았습니다. 일단 야후의 날씨 API를 이용해서 배경화면 색상을 변하게 하였구요. 위에 현재 날씨 정보가 나타납니다. 이전 한번만 나오던 배경음을 계속 나오도록 변경하였습니다. 음… google의 날씨 API를 사용하려고 했는데 잘 안되서 야후의 API를 사용하였는데, 쓸만하네요!
야후 API를 사용할 때 약간 문제가 있었던 부분이 Namespace를 설정하는 부분이었는데, 이 포스팅을 보면서 해결 완료했습니다! 역시 -_-b 멘토님이 최고!
앞으로 현재 “했다” 라고만 나오는 것 등을 “보다, 사랑하다” 등으로 재미있게 만들어서 더 가다듬는 건 매시업경진대회 이후에 해야 될 것 같습니다.
+ 보영이 주목!
+ 한번 수정! 온도가 바뀌지 않는 버그!
답글 남기기