Created
December 12, 2015 05:25
-
-
Save Nia-TN1012/8b1a8546f689c4cff48f to your computer and use it in GitHub Desktop.
すぱこーRSSフィードを取得するライブラリ「すぱーダ」です。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author : Nia Tomonaka ( @nia_tn1012 ) | |
// ※まだ開発段階です。 | |
using System; | |
using System.Collections.Generic; | |
using System.Xml.Linq; | |
/// <summary> | |
/// すぱこーRSSフィードを取得するモジュール、 | |
/// すぱこーRSSリーダー、略して「すぱーダ」(SPADA)です。 | |
/// </summary> | |
namespace SPADA { | |
/// <summary> | |
/// すぱこーの各話のデータを表します。 | |
/// </summary> | |
public class SpacoRSSItem { | |
/// <summary> | |
/// タイトル | |
/// </summary> | |
public string Title { get; set; } | |
/// <summary> | |
/// 作者名 | |
/// </summary> | |
public string Author { get; set; } | |
/// <summary> | |
/// 作品のリンク | |
/// </summary> | |
public string Link { get; set; } | |
/// <summary> | |
/// 公開日 | |
/// </summary> | |
public DateTime PubDate { get; set; } | |
/// <summary> | |
/// 作品のあらすじ | |
/// </summary> | |
public string Description { get; set; } | |
/// <summary> | |
/// 作品の話数 | |
/// </summary> | |
public int Volume { get; set; } | |
/// <summary> | |
/// 更新日 | |
/// </summary> | |
public DateTime ModifiedDate { get; set; } | |
/// <summary> | |
/// 利用可能可能かどうか | |
/// </summary> | |
/// <value>true : 利用可能 / false : 利用不可能</value> | |
public bool IsAvailable { get; set; } | |
/// <summary> | |
/// 漫画の画像のURL | |
/// </summary> | |
public string MediaURL { get; set; } | |
/// <summary> | |
/// サムネイル画像のURL | |
/// </summary> | |
public string ThumbnailURL { get; set; } | |
/// <summary> | |
/// ID | |
/// </summary> | |
public string ID { get; set; } | |
} | |
/// <summary> | |
/// すぱこーRSSフィードを取得するクラスです | |
/// </summary> | |
/// <remarks>これがあれば、面倒なxmlnsの設定やXPathの読み取りの煩わしさが解消されます。</remarks> | |
public class SpacoRSSReader { | |
/// <summary> | |
/// 作品タイトル | |
/// </summary> | |
public string Title { get; private set; } | |
/// <summary> | |
/// リンクを取得します。 | |
/// </summary> | |
public string Link { get; private set; } | |
/// <summary> | |
/// 作品の説明を取得します。 | |
/// </summary> | |
public string Description { get; private set; } | |
/// <summary> | |
/// 最新話更新日を取得します。 | |
/// </summary> | |
public DateTime PubDate { get; private set; } | |
/// <summary> | |
/// バナー画像のURLを取得します。 | |
/// </summary> | |
public string BannerURL { get; private set; } | |
/// <summary> | |
/// すぱこーの各話を格納します | |
/// </summary> | |
private List<SpacoRSSItem> items; | |
/// <summary> | |
/// すぱこーの各話のデータコレクションを取得します。 | |
/// </summary> | |
public IEnumerable<SpacoRSSItem> Items => items; | |
// 名前空間を表します | |
private XNamespace p = @"http://pinga.mu/terms/"; | |
private XNamespace media = @"http://search.yahoo.com/mrss/"; | |
private XNamespace dcndl = @"http://ndl.go.jp/dcndl/terms/"; | |
private XNamespace dc = @"http://purl.org/dc/elements/1.1/"; | |
/// <summary> | |
/// SpacoFeedクラスの新しいインスタンスを生成します。 | |
/// </summary> | |
/// <param name="uri">すぱこーRSSフィードのURL</param> | |
public SpacoRSSReader( string uri ) { | |
// Linq to XMLですぱこーRSSフィードの読み込みます。 | |
XElement spx = XElement.Load( uri ); | |
// すぱこーのチャンネル情報を取得します。 | |
XElement spxChannel = spx.Element( "channel" ); | |
Title = spxChannel.Element( "title" ).Value; | |
Link = spxChannel.Element( "link" ).Value; | |
Description = spxChannel.Element( "description" ).Value; | |
PubDate = DateTime.Parse( spxChannel.Element( "pubDate" ).Value ); | |
BannerURL = spxChannel.Element( "image" ).Value; | |
// すぱこーの各話のデータを取得します。 | |
items = new List<SpacoRSSItem>(); | |
var spxItems = spxChannel.Elements( "item" ); | |
foreach( var item in spxItems ) { | |
SpacoRSSItem si = new SpacoRSSItem { | |
Title = item.Element( "title" ).Value, | |
Author = item.Element( dc + "creator" ).Value, | |
Link = item.Element( "link" ).Value, | |
PubDate = DateTime.Parse( item.Element( "pubDate" ).Value ), | |
Description = item.Element( "description" ).Value, | |
Volume = int.Parse( item.Element( dcndl + "volume" ).Value ), | |
ModifiedDate = DateTime.Parse( item.Element( dc + "modified" ).Value ), | |
IsAvailable = bool.Parse( item.Element( p + "isAvailable" ).Value ), | |
MediaURL = item.Element( media + "content" ).Attribute( "url" ).Value, | |
ThumbnailURL = item.Element( media + "thumbnail" ).Attribute( "url" ).Value, | |
ID = item.Element( "guid" ).Value | |
}; | |
items.Add( si ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment