-
Notifications
You must be signed in to change notification settings - Fork 2
/
easyrss.pas
executable file
·465 lines (413 loc) · 11.7 KB
/
easyrss.pas
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
(*
EasyRSS plugin.
Copyright (C) 2012-2014 Silvio Clecio.
Please see the LICENSE file.
*)
unit EasyRSS;
{$mode objfpc}{$H+}
interface
uses
DOM, XmlRead, FGL, HTTPDefs, FPHttpClient, Classes, SysUtils;
type
{ TRSSImage }
TRSSImage = class
private
FLink: string;
FTitle: string;
FUrl: string;
function GetContent: string;
protected
function FormatTag(const AProp, ATag: string): string;
public
property Content: string read GetContent;
property Title: string read FTitle write FTitle;
property Url: string read FUrl write FUrl;
property Link: string read FLink write FLink;
end;
{ TRSSItem }
TRSSItem = class
private
FAuthor: string;
FCategory: string;
FComments: string;
FDescription: string;
Fcontentencoded: string;
FGuid: string;
FIsPermaLink: Boolean;
FLink: string;
FPubDate: string;
FTitle: string;
function GetContent: string;
protected
function FormatTag(const AProp, ATag: string): string;
public
constructor Create;
property Content: string read GetContent;
property Title: string read FTitle write FTitle;
property Link: string read FLink write FLink;
property Guid: string read FGuid write FGuid;
property IsPermaLink: Boolean read FIsPermaLink write FIsPermaLink;
property Category: string read FCategory write FCategory;
property PubDate: string read FPubDate write FPubDate;
property Description: string read FDescription write FDescription;
property Contentencoded: string read Fcontentencoded write Fcontentencoded;
property Author: string read FAuthor write FAuthor;
property Comments: string read FComments write FComments;
end;
TRSSItems = specialize TFPGList<TRSSItem>;
{ TRSS }
TRSS = class
private
FCategory: string;
FCopyright: string;
FDocs: string;
FGenerator: string;
FImage: TRSSImage;
FItems: TRSSItems;
FLanguage: string;
FLastBuildDate: string;
FLink: string;
FManagingEditor: string;
FPubDate: string;
FTitle: string;
FUTF8: Boolean;
FWebMaster: string;
function GetContent: string;
procedure FreeItems;
function GetCount: Integer;
function GetElements(AIndex: Integer): TRSSItem;
procedure SetElements(AIndex: Integer; AValue: TRSSItem);
protected
function FormatTag(const AProp, ATag: string): string;
public
constructor Create;
destructor Destroy; override;
function Add: TRSSItem;
procedure Clear;
property Count: Integer read GetCount;
property Content: string read GetContent;
property Items: TRSSItems read FItems;
property Elements[AIndex: Integer]: TRSSItem read GetElements
write SetElements; default;
property Title: string read FTitle write FTitle;
property Link: string read FLink write FLink;
property Language: string read FLanguage write FLanguage;
property WebMaster: string read FWebMaster write FWebMaster;
property Copyright: string read FCopyright write FCopyright;
property PubDate: string read FPubDate write FPubDate;
property LastBuildDate: string read FLastBuildDate write FLastBuildDate;
property ManagingEditor: string read FManagingEditor write FManagingEditor;
property Category: string read FCategory write FCategory;
property Generator: string read FGenerator write FGenerator;
property Docs: string read FDocs write FDocs;
property Image: TRSSImage read FImage write FImage;
property UTF8: Boolean read FUTF8 write FUTF8;
end;
{ TRSSReader }
TRSSReader = class(TRSS)
protected
function GetTagValue(ANode: TDOMNode; const ATagName: string): string;
public
procedure LoadFromStream(AStream: TStream);
procedure LoadFromString(const S: string);
procedure LoadFromFile(const AFileName: TFileName);
procedure LoadFromHttp(const AUrl: string);
end;
{ TRSSWriter }
TRSSWriter = class(TRSS)
public
procedure SaveToStream(AStream: TStream);
procedure SaveToFile(const AFileName: TFileName);
end;
function DateTimeToGMT(const ADateTime: TDateTime): string;
function NewGuid: string;
implementation
type
THttp = class(TFPHTTPClient)
public
procedure GetFeed(const AUrl: string; out AFeed: TStream);
end;
function DateTimeToGMT(const ADateTime: TDateTime): string;
var
VYear, VMonth, VDay, VHour, VMinute, VSecond, M: Word;
begin
DecodeDate(ADateTime, VYear, VMonth, VDay);
DecodeTime(ADateTime, VHour, VMinute, VSecond, M);
Result := Format('%s, %.2d %s %d %.2d:%.2d:%.2d GMT',
[HTTPDays[DayOfWeek(ADateTime)], VDay, HTTPMonths[VMonth], VYear, VHour,
VMinute, VSecond]);
end;
function NewGuid: string;
var
VGuid: TGuid;
begin
CreateGUID(VGuid);
SetLength(Result, 36);
StrLFmt(PChar(Result), 36,'%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x',
[VGuid.D1, VGuid.D2, VGuid.D3, VGuid.D4[0], VGuid.D4[1], VGuid.D4[2],
VGuid.D4[3], VGuid.D4[4], VGuid.D4[5], VGuid.D4[6], VGuid.D4[7]]);
end;
{ THttp }
procedure THttp.GetFeed(const AUrl: string; out AFeed: TStream);
begin
AFeed := TMemoryStream.Create;
DoMethod('GET', AUrl, AFeed, [200]);
end;
{ TRSSImage }
function TRSSImage.GetContent: string;
begin
Result :=
' <image>'+#10+
FormatTag(FTitle, 'title')+
FormatTag(FUrl, 'url')+
FormatTag(FLink, 'link')+
' </image>'+#10;
end;
function TRSSImage.FormatTag(const AProp, ATag: string): string;
begin
if AProp <> '' then
Result := ' <'+ATag+'>'+AProp+'</'+ATag+'>'+#10
else
Result := '';
end;
{ TRSSItem }
constructor TRSSItem.Create;
begin
FPubDate := DateTimeToGMT(Now);
FGuid := NewGuid;
end;
function TRSSItem.FormatTag(const AProp, ATag: string): string;
begin
if AProp <> '' then
Result := ' <'+ATag+'>'+AProp+'</'+ATag+'>'+#10
else
Result := '';
end;
function TRSSItem.GetContent: string;
begin
Result :=
' <item>'+#10+
FormatTag(FTitle, 'title')+
FormatTag(FLink, 'link')+
FormatTag(FAuthor, 'author')+
FormatTag(FComments, 'comments')+
Format(' <guid ispermalink="%s">%s</guid>',
[BoolToStr(FIsPermaLink, 'true', 'false'), FGuid])+#10+
FormatTag(FCategory, 'category')+
FormatTag(FPubDate, 'pubdate')+
FormatTag(FDescription, 'description')+
FormatTag(Fcontentencoded, 'content:encoded')+
' </item>'+#10;
end;
{ TRSS }
constructor TRSS.Create;
begin
FItems := TRSSItems.Create;
FUTF8 := True;
FLanguage := 'en-us';
FGenerator := 'EasyRSS plugin';
FPubDate := DateTimeToGMT(Now);
FLastBuildDate := DateTimeToGMT(Now);
end;
destructor TRSS.Destroy;
begin
FreeAndNil(FImage);
FreeItems;
FItems.Free;
inherited Destroy;
end;
function TRSS.GetCount: Integer;
begin
Result := FItems.Count;
end;
function TRSS.GetElements(AIndex: Integer): TRSSItem;
begin
Result := FItems[AIndex];
end;
procedure TRSS.SetElements(AIndex: Integer; AValue: TRSSItem);
begin
FItems[AIndex] := AValue;
end;
function TRSS.GetContent: string;
var
I: Integer;
begin
Result :=
'<rss version="2.0">'+#10+
' <channel>'+#10+
' <title>'+FTitle+'</title>'+#10+
FormatTag(FGenerator, 'generator')+
FormatTag(FLink, 'link')+
FormatTag(FLanguage, 'language')+
FormatTag(FWebMaster, 'webmaster')+
FormatTag(FCopyright, 'copyright')+
FormatTag(FPubDate, 'pubdate')+
FormatTag(FLastBuildDate, 'lastbuilddate')+
FormatTag(FCategory, 'category')+
FormatTag(FDocs, 'docs')+
FormatTag(FManagingEditor, 'managingeditor');
if Assigned(FImage) then
Result += FImage.Content;
for I := 0 to Pred(FItems.Count) do
Result += TRSSItem(FItems[I]).Content;
Result +=
' </channel>'+#10+
'</rss>';
end;
procedure TRSS.FreeItems;
var
I: Integer;
begin
for I := 0 to Pred(FItems.Count) do
TObject(FItems[I]).Free;
end;
function TRSS.FormatTag(const AProp, ATag: string): string;
begin
if AProp <> '' then
Result := ' <'+ATag+'>'+AProp+'</'+ATag+'>'+#10
else
Result := '';
end;
function TRSS.Add: TRSSItem;
begin
Result := TRSSItem.Create;
FItems.Add(Result);
end;
procedure TRSS.Clear;
begin
FreeItems;
FItems.Clear;
end;
{ TRSSReader }
function TRSSReader.GetTagValue(ANode: TDOMNode; const ATagName: string): string;
var
VNode: TDOMNode;
begin
Result := '';
if Assigned(ANode) then
begin
VNode := ANode.FindNode(DOMString(ATagName));
if Assigned(VNode) then
begin
if FUTF8 then
Result := UTF8Encode(VNode.TextContent)
else
Result := string(VNode.TextContent);
end;
end;
end;
procedure TRSSReader.LoadFromStream(AStream: TStream);
var
VRssItem: TRSSItem;
VXml: TXMLDocument;
VXmlChannel, VXmlImage, VXmlItem, VPermaLink: TDOMNode;
begin
try
ReadXMLFile(VXml, AStream);
VXmlChannel := VXml.DocumentElement.FindNode('channel');
FTitle := GetTagValue(VXmlChannel, 'title');
FLink := GetTagValue(VXmlChannel, 'link');
FLanguage := GetTagValue(VXmlChannel, 'language');
FWebMaster := GetTagValue(VXmlChannel, 'webmaster');
FCopyright := GetTagValue(VXmlChannel, 'copyright');
FPubDate := GetTagValue(VXmlChannel, 'pubdate');
FLastBuildDate := GetTagValue(VXmlChannel, 'lastbuilddate');
FManagingEditor := GetTagValue(VXmlChannel, 'managingeditor');
FCategory := GetTagValue(VXmlChannel, 'category');
FGenerator := GetTagValue(VXmlChannel, 'generator');
FDocs := GetTagValue(VXmlChannel, 'docs');
VXmlImage := VXmlChannel.FindNode('image');
if Assigned(VXmlImage) then
begin
if not Assigned(FImage) then
FImage := TRSSImage.Create;
FImage.Title := GetTagValue(VXmlImage, 'title');
FImage.Url := GetTagValue(VXmlImage, 'url');
FImage.Link := GetTagValue(VXmlImage, 'link');
end;
VXmlItem := VXmlChannel.FindNode('item');
Clear;
while Assigned(VXmlItem) do
begin
VRssItem := TRSSItem.Create;
VRssItem.Title := GetTagValue(VXmlItem, 'title');
VRssItem.Link := GetTagValue(VXmlItem, 'link');
VRssItem.Guid := GetTagValue(VXmlItem, 'guid');
VPermaLink := VXmlItem.FindNode('guid');
if Assigned(VPermaLink) then
begin
VPermaLink := VPermaLink.Attributes.GetNamedItem('ispermalink');
if Assigned(VPermaLink) then
VRssItem.IsPermaLink := StrToBoolDef(
string(VPermaLink.TextContent), False);
end;
VRssItem.Category := GetTagValue(VXmlItem, 'category');
VRssItem.PubDate := GetTagValue(VXmlItem, 'pubdate');
VRssItem.Description := GetTagValue(VXmlItem, 'description');
VRssItem.Contentencoded := GetTagValue(VXmlItem, 'content:encoded');
VRssItem.Author := GetTagValue(VXmlItem, 'author');
VRssItem.Comments := GetTagValue(VXmlItem, 'comments');
FItems.Add(VRssItem);
VXmlItem := VXmlItem.NextSibling;
end;
finally
FreeAndNil(VXml);
end;
end;
procedure TRSSReader.LoadFromString(const S: string);
var
VString: TStringStream;
begin
VString := TStringStream.Create(S);
try
LoadFromStream(VString);
finally
VString.Free;
end;
end;
procedure TRSSReader.LoadFromFile(const AFileName: TFileName);
var
VFile: TFileStream;
begin
VFile := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(VFile);
finally
VFile.Free;
end;
end;
procedure TRSSReader.LoadFromHttp(const AUrl: string);
var
VHttp: THttp;
VFeed: TStream;
begin
VHttp := THttp.Create(nil);
try
VHttp.GetFeed(AUrl, VFeed);
VFeed.Position := 0;
LoadFromStream(VFeed);
finally
FreeAndNil(VFeed);
VHttp.Free;
end;
end;
{ TRSSWriter }
procedure TRSSWriter.SaveToStream(AStream: TStream);
var
S: String;
begin
S := GetContent;
AStream.Write(Pointer(S)^, Length(S));
end;
procedure TRSSWriter.SaveToFile(const AFileName: TFileName);
var
VFile: TFileStream;
begin
VFile := TFileStream.Create(AFileName, fmCreate);
try
SaveToStream(VFile);
finally
VFile.Free;
end;
end;
end.