📄 urssfeed.pas
字号:
unit uRSSFeed;
interface
uses Classes, uW3CDTF, FastStringFuncs, uRssBase, MSXML2_TLB,
IniFiles, uConstants;
type
TRSSFeed = class(TInterfacedObject, IRSSBase)
private
FDoc:IXMLDOMDocument2;
fItems : IRSSItemBaseList;
FImage : IRSSImageBase;
FTitle, FDescription : string;
FVersion, FLink, FCreator : WideString;
protected
function Get_Version : WideString;
function Get_Title : string;
function Get_Link: WideString;
function Get_Description: string;
function Get_Creator: WideString;
function Get_Items: IRSSItemBaseList;
function Get_Image: IRSSImageBase;
public
constructor Create(theDoc : IXMLDOMDocument2);
procedure SaveXml(Destination: OleVariant);
property Items:IRSSItemBaseList read fItems write fItems;
property Image:IRSSImageBase read FImage write FImage;
property Version: WideString read Get_Version;
property Title: string read Get_Title;
property Link: Widestring read Get_Link;
property Description: string read Get_Description;
property Creator: WideString read Get_Creator;
end;
TRSSItems = class(TInterfacedObject, IRSSItemBaseList)
private
fItems : THashedStringList;
fNode:IXMLDOMNode;
protected
procedure Add(item : TInterfacedObject);overload;
function Add:IRSSItemBase;overload;
procedure Delete(Index:integer);
function Get_Item(index : integer): IRSSItemBase;
function Get_ItemByGuid(guid : string): IRSSItemBase;
function Get_Count(): integer;
function IndexOfLink(ALink:WideString):integer;
function IndexOfTitle(ATitle:String):integer;
public
constructor Create(theNode:IXMLDOMNode);
end;
TRSSItem = class(TInterfacedObject, IRSSItemBase)
private
FNode:IXMLDOMNode;
FFeedTitle:string;
FAuthor: string;
FTitle: string;
FPubDate: TW3CDTF;
FGuid: WideString;
FLink: WideString;
FDescription: string;
FCategory : string;
FCommentCount : integer;
FComment : Widestring;
FCommentRSS : Widestring;
FCommentURL : Widestring;
FReaded:Boolean;
protected
function Get_Author: string;
function Get_Title: string;
function Get_PubDate: TW3CDTF;
function Get_Guid: WideString;
function Get_Link: WideString;
function Get_Category: string;
function Get_Creator: WideString;
function Get_CommentCount: integer;
function Get_CommentRss: WideString;
function Get_CommentURL: WideString;
function Get_Comment: WideString;
function Get_Description: string;
procedure Set_PubDate(Value:TW3CDTF);
procedure Set_Title(Value:string);
procedure Set_Link(Value:WideString);
procedure Set_Guid(Value:Widestring);
procedure Set_Description(Value:string);
procedure Set_Author(Value:string);
procedure Set_Category(Value:string);
procedure Set_Creator(Value:Widestring);
procedure Set_Comment(Value:Widestring);
procedure Set_CommentCount(Value:Integer);
procedure Set_CommentURL(Value:Widestring);
procedure Set_CommentRSS(Value:Widestring);
function Get_Readed:Boolean;
procedure Set_Readed(Value:Boolean);
public
constructor Create(theNode : IXmlDOMNode);
property PubDate: TW3CDTF read Get_PubDate write Set_PubDate;
property Title: string read Get_Title write Set_Title;
property Link: WideString read Get_Link write Set_Link;
property Guid: WideString read Get_Guid write Set_Guid;
property Description: string read Get_Description write Set_Description;
property Author: string read Get_Author write Set_Author;
property Category: string read Get_Category write Set_Category;
property Creator: WideString read Get_Creator write Set_Creator;
property Comment: WideString read Get_Comment write Set_Comment;
property CommentCount: integer read Get_CommentCount write Set_CommentCount;
property CommentRSS: WideString read Get_CommentRSS write Set_CommentRSS;
property Readed:Boolean read Get_Readed write Set_Readed;
end;
function GetRSSFeed(Doc: IXMLDOMDocument2): IRSSBase;
implementation
uses SysUtils, ComObj;
function GetRSSFeed(Doc: IXMLDOMDocument2): IRSSBase;
begin
Result := TRSSFeed.Create(Doc) as IRSSBase;
end;
{ TRSSFeed }
constructor TRSSFeed.Create(theDoc: IXMLDOMDocument2);
var
i : integer;
channelNodes : IXMLDOMNodeList;
Node,theNode: IXmlDomNode;
begin
FDoc:=theDoc;
theNode:= FDoc.documentElement;
node := theNode.selectSingleNode('channel');
fItems := TRSSItems.Create(node);
Fimage := TRSSImageBase.Create(node.selectSingleNode('image'));
FCreator := getNodeVal(node, 'creator');
FDescription := getNodeVal(node, 'description');
FTitle := ExtractNonHTML(getNodeVal(node, 'title'));
FLink := getNodeVal(node, 'link');
FVersion := 'RSS ' + getNodeVal(thenode, '@version');
channelNodes := node.selectNodes('item');
for i := 0 to channelNodes.Length - 1 do
fitems.Add( TRSSItem.Create(channelNodes.item[i]) );
end;
procedure TRSSFeed.SaveXml(Destination: OleVariant);
begin
FDoc.save(Destination);
end;
function TRSSFeed.Get_Creator: WideString; begin Result := FCreator; end;
function TRSSFeed.Get_Description: string; begin Result := FDescription; end;
function TRSSFeed.Get_Image: IRSSImageBase; begin Result := FImage; end;
function TRSSFeed.Get_Items: IRSSItemBaseList; begin Result := fItems; end;
function TRSSFeed.Get_Link: WideString; begin Result := FLink; end;
function TRSSFeed.Get_Title: string; begin Result := FTitle; end;
function TRSSFeed.Get_Version: WideString; begin Result := FVersion; end;
{ TRSSItems }
constructor TRSSItems.Create(theNode:IXMLDOMNode);
begin
fNode:=theNode;
fItems := THashedStringList.Create;
end;
function TRssItems.IndexOfLink(ALink:WideString):integer;
var i:Integer;
begin
Result:=-1;
for i:= 0 to fItems.Count - 1 do
if TRSSItem(fItems.Objects[i]).FLink=ALink then
begin
Result:=i;
Exit;
end;
end;
function TRssItems.IndexOfTitle(ATitle:String):integer;
var i:Integer;
begin
Result:=-1;
for i:= 0 to fItems.Count - 1 do
if TRSSItem(fItems.Objects[i]).FTitle=ATitle then
begin
Result:=i;
Exit;
end;
end;
procedure TRSSItems.Add(item: TInterfacedObject);
begin
fItems.AddObject((item as TRSSItem).FGuid, item);
end;
function TRSSItems.Add:IRSSItemBase;
var Item:TRSSItem;
begin
item:=TRSSItem.Create(fNode.appendChild(fNode.ownerDocument.createElement('item')));
fItems.AddObject('',item);
Result:=item;
end;
function TRSSItems.Get_Count: integer; begin Result := fItems.Count; end;
procedure TRSSItems.Delete(Index:integer);
begin
fNode.removeChild(TRSSItem(fItems.Objects[Index]).FNode);
fItems.Delete(Index);
end;
function TRSSItems.Get_Item(Index: Integer): IRSSItemBase;
begin
Result := TRSSItem(fItems.Objects[index]);
end;
function TRSSItems.Get_ItemByGuid(guid: string): IRSSItemBase;
begin
Result := TRSSItem(fItems.Objects[fItems.IndexOf(guid)]);
end;
{ TRSSItem }
constructor TRSSItem.Create(theNode: IXmlDOMNode);
var
str : WideString;
begin
FAuthor:='';
FTitle:='';
FGuid:='';
FLink:='';
FDescription:='';
FCommentCount := 0;
FCategory := '';
FComment := '';
FCommentRSS := '';
FCommentURL := '';
FReaded:=False;
if assigned(theNode) then begin
FNode:=theNode;
if getNodeVal(thenode, 'readed')='true' then
FReaded:=True;
FAuthor := getNodeVal(thenode, 'author');
if FAuthor = '' then FAuthor := getNodeVal(thenode, 'dc:creator');
FCategory := getNodeVal(thenode, 'category');
FComment := getNodeVal(thenode, 'comment');
FCommentRSS := getNodeVal(thenode, 'wfw:commentRss');
str := getNodeVal(thenode, 'slash:comments');
if str = '' then FCommentCount := 0 else FCommentCount := StrToInt(str);
FCommentURL := getNodeVal(thenode, 'comments');
str := getNodeVal(thenode, 'title');
FTitle := ExtractNonHTML(str);
FLink := getNodeVal(thenode, 'link');
FGuid := getNodeVal(thenode, 'guid');
if FGuid = '' then FGuid := FLink;
if FGuid = '' then FGuid := FTitle;
try
str := getNodeVal(thenode, 'pubDate');
FPubDate := TW3CDTF.Create(str)
except
FPubDate := TW3CDTF.CreateDateTime(0);
end;
FDescription := getNodeVal(thenode, 'content');
if FDescription='' then
FDescription := getNodeVal(thenode, 'content:encoded');
if FDescription='' then
FDescription := getNodeVal(thenode, 'description');
end
else
FPubDate := TW3CDTF.CreateDatetime(0);
end;
function TRSSItem.Get_Author: string; begin Result := FAuthor; end;
function TRSSItem.Get_Category: string; begin Result := FCategory; end;
function TRSSItem.Get_Comment: WideString; begin Result := FComment; end;
function TRSSItem.Get_CommentRss: WideString; begin Result := FCommentRSS; end;
function TRSSItem.Get_CommentCount: integer; begin Result := FCommentCount; end;
function TRSSItem.Get_CommentURL: WideString; begin Result := FCommentURL; end;
function TRSSItem.Get_Creator: WideString; begin Result := Get_Author(); end;
function TRSSItem.Get_Description: string; begin Result := FDescription; end;
function TRSSItem.Get_Guid: WideString; begin Result := FGuid; end;
function TRSSItem.Get_Link: WideString; begin Result := FLink; end;
function TRSSItem.Get_PubDate: TW3CDTF; begin Result := FPubDate; end;
function TRSSItem.Get_Title: string; begin Result := FTitle; end;
procedure TRSSItem.Set_PubDate(Value:TW3CDTF);
begin
FPubDate:=Value;
SetNodeVal(FNode,'pubDate',Value.ToRFC822String);
end;
procedure TRSSItem.Set_Title(Value:string);
begin
FTitle:=Value;
SetNodeVal(FNode,'title',Value);
end;
procedure TRSSItem.Set_Link(Value:WideString);
begin
FLink:=Value;
SetNodeVal(FNode,'link',Value);
end;
procedure TRSSItem.Set_Guid(Value:Widestring);
begin
FGuid:=Value;
SetNodeVal(FNode,'guid',Value);
end;
procedure TRSSItem.Set_Description(Value:string);
begin
FDescription:=Value;
SetNodeVal(FNode,'description',Value);
end;
procedure TRSSItem.Set_Author(Value:string);
begin
FAuthor:=Value;
SetNodeVal(FNode,'author',Value);
end;
procedure TRSSItem.Set_Category(Value:string);
begin
FCategory:=Value;
SetNodeVal(FNode,'categroy',Value);
end;
procedure TRSSItem.Set_Creator(Value:Widestring);
begin
Set_Author(Value);
end;
procedure TRSSItem.Set_Comment(Value:Widestring);
begin
FComment:=Value;
SetNodeVal(FNode,'comment',Value);
end;
procedure TRSSItem.Set_CommentCount(Value:Integer);
begin
FCommentCount:=Value;
SetNodeVal(FNode,'commentCount',IntToStr(Value));
end;
procedure TRSSItem.Set_CommentURL(Value:Widestring);
begin
FCommentURL:=Value;
SetNodeVal(FNode,'commentUrl',Value);
end;
procedure TRSSItem.Set_CommentRSS(Value:Widestring);
begin
FCommentRSS:=Value;
SetNodeVal(FNode,'commentRss',Value);
end;
function TRSSItem.Get_Readed:Boolean;begin Result:=FReaded;end;
procedure TRSSItem.Set_Readed(Value:Boolean);
begin
FReaded:=Value;
if Value then
SetNodeVal(FNode,'readed','true')
else
SetNodeVal(FNode,'readed','false')
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -