/
Blog.cs
115 lines (100 loc) · 3.09 KB
/
Blog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Xml;
namespace OitAntenna
{
public class Blog
{
private Category category;
private string rssUri;
private string uri;
private string title;
private SortedSet<Article> articleSet;
internal Blog(Category category, string rssUri)
{
this.category = category;
this.rssUri = rssUri;
articleSet = new SortedSet<Article>(Article.IDOrder);
try
{
Reload();
}
catch (Exception e)
{
Log.WriteLine("RSS[" + rssUri + "]の取得に失敗", false);
Log.WriteException(e);
}
}
public ICollection<Article> Reload()
{
XmlDocument document = new XmlDocument();
document.Load(rssUri);
uri = document.GetElementsByTagName("link")[0].InnerText;
title = document.GetElementsByTagName("title")[0].InnerText;
List<Article> newArticleList = new List<Article>();
bool isRss2 = document.DocumentElement.Name == "rss";
XmlNodeList itemNodes = document.GetElementsByTagName("item");
foreach (XmlNode itemNode in itemNodes)
{
XmlElement itemElement = (XmlElement)itemNode;
string articleUri = itemElement.GetElementsByTagName("link")[0].InnerText;
XmlNodeList dateNodes = itemElement.GetElementsByTagName(isRss2 ? "pubDate" : "dc:date");
DateTime articleDate = DateTime.Parse(dateNodes[0].InnerText);
string articleTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
Article newArticle = new Article(this, articleUri, articleDate, articleTitle);
if (articleSet.Count < Settings.BlogMaxNumArticles || newArticle.ID > articleSet.Max.ID)
{
if (articleSet.Add(newArticle))
{
newArticleList.Add(newArticle);
}
}
}
category.BlogUpdated(newArticleList);
DiscardOldArticles();
return newArticleList;
}
private void DiscardOldArticles()
{
while (articleSet.Count > Settings.BlogMaxNumArticles)
{
articleSet.Remove(articleSet.Max);
}
}
public Category Category
{
get
{
return category;
}
}
public string Uri
{
get
{
return uri;
}
}
public string Title
{
get
{
return title;
}
}
public ICollection<Article> Articles
{
get
{
return articleSet;
}
}
public Article NewestArticle
{
get
{
return articleSet.Min;
}
}
}
}