/// <summary> /// 指定したタイトル、説明、画像のアドレスで新しいインスタンスを初期化します。 /// </summary> /// <param name="title">テーマソングのタイトル。</param> /// <param name="description">テーマソングの説明。</param> /// <param name="imageUri">テーマソングが収録されているアルバムジャケット画像の URI。</param> public ThemeSong(string title, string description, Uri imageUri) { this = new ThemeSong(); this.Title = title; this.Description = description; this.ImageUri = imageUri; }
private async Task Initialize() { XDocument xml; var client = new WebClient(); client.Encoding = Encoding.UTF8; var html = await client.DownloadStringTaskAsync(Address); html = html.Replace(" ", " ").Replace("©", "(c)"); html = html.Insert(html.IndexOf("</body>"), "</div>"); var temp = Path.GetTempFileName(); File.WriteAllText(temp, html, Encoding.UTF8); using (var reader = new StreamReader(temp)) { xml = XDocument.Load(reader); } var ns = xml.Root.Name.Namespace; var div = ns + "div"; var span = ns + "span"; var h3 = ns + "h3"; var h4 = ns + "h4"; var p = ns + "p"; var a = ns + "a"; // 最新回の情報 var latest = xml.Descendants(div) .Where(e => e.Attribute("id") != null && e.Attribute("id").Value == "radio") .Descendants() .Where(e => e.Attribute("class") != null && e.Attribute("class").Value == "wbody") .First(); _Contents.Add(new Content(latest.Element(h3).Value, latest.Element(h3).Element(span).Value, latest.Element(p).Value)); // コーナーの情報 var corner = xml.Descendants(div) .Where(e => e.Attribute("id") != null && e.Attribute("id").Value == "corner") .Descendants(); _Corners = corner.Where(e => e.Name == h3) .Select(e => e.Value) .Zip(corner.Where(e => e.Name == p).Select(e => e.Value), (e1, e2) => new KeyValuePair<string, string>(e1, e2)) .ToDictionary(x => x.Key, x => x.Value); // テーマソングの情報 var theme = xml.Descendants(div) .Where(e => e.Attribute("id") != null && e.Attribute("id").Value == "theme") .Descendants() .Where(e => e.Name == a); _Opening = new ThemeSong(theme.First(), ns); _Ending = new ThemeSong(theme.Skip(1).First(), ns); // バックナンバーの情報 html = await client.DownloadStringTaskAsync(BackNumber); html = html.Replace(" ", " ").Replace("©", "(c)").Replace("</span></p>", "</span>"); html = html.Insert(html.IndexOf("</body>"), "</div>"); client.Dispose(); File.WriteAllText(temp, html, Encoding.UTF8); using (var reader = new StreamReader(temp)) { xml = XDocument.Load(reader); } var backNumber = xml.Descendants(div) .Where(e => e.Attribute("class") != null && e.Attribute("class").Value == "wbody"); _Contents.AddRange(backNumber.Descendants(h3) .Zip(backNumber.Descendants(p).Where(e => !e.HasAttributes), (e1, e2) => new KeyValuePair<XElement, XElement>(e1, e2)) .Select(x => new Content(x.Key.Value, x.Key.Element(span).Value, x.Value.Value))); }
/// <summary> /// XML 要素から抽出したテーマソングの情報を使用して新しいインスタスを初期化します。 /// </summary> /// <param name="element">テーマソングの情報を含む XML 要素</param> /// <param name="ns">抽出する XML 要素の XML 名前空間</param> public ThemeSong(XElement element, XNamespace ns) { this = new ThemeSong(); try { var h4 = ns + "h4"; var title = element.Element(h4); this.Title = title.Value; this.Description = title.NextNode.ToString(); var img = ns + "img"; this.ImageUri = new Uri(Minorhythm.Address, element.Element(img).Attribute("src").Value); } catch { throw new ArgumentException("XML 要素からテーマソング情報を抽出できません。"); } }