aboutsummarylogtreecommitdiffstats
path: root/rss_feed.pas
blob: 48215f0579f37027962315dc1a9ce3b1d2a05273 (plain)
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
{ Generates RSS feed for site }
unit rss_feed;

{$mode ObjFPC}{$H+}

interface

uses
  SysUtils, Classes, SQLite3Conn, SQLDB, DateUtils, url_constr ;

function selectByMode(Mode : String; ValuePlain : String; ValueTree : String) : String;
procedure writeRssFeed(Mode : String; Domain : String; feedXMLFileName: String; var konnect: TSQLite3Connection; var tranzact: TSQLTransaction);
function clearCuttedToPlainFromHtml(S: String; N: Integer): String;

implementation


function clearCuttedToPlainFromHtml(S: String; N: Integer): String;
var
  PlainText: String;
  i: Integer;
begin
  // Step 1: Remove HTML tags using a simple regex-like approach
  PlainText := '';
  i := 1;
  while i <= Length(S) do
  begin
    if S[i] = '<' then
    begin
      // Skip everything until '>'
      while (i <= Length(S)) and (S[i] <> '>') do
        i := i + 1;
      i := i + 1; // Move past the closing '>'
    end
    else
    begin
      // Append non-tag characters to PlainText
      PlainText := PlainText + S[i];
      i := i + 1;
    end;
  end;

  // Step 2: Take only the first N characters
  if Length(PlainText) > N then
    PlainText := Copy(PlainText, 1, N);

  // Step 3: Return the result
  clearCuttedToPlainFromHtml := PlainText;
end;

function selectByMode(Mode: String; ValuePlain: String; ValueTree: String
  ): String;
begin
  if mode = 'plain' then result := ValuePlain else Result:=ValueTree;
end;

procedure writeRssFeed(Mode : String; Domain : String; feedXMLFileName: String; var konnect: TSQLite3Connection; var tranzact: TSQLTransaction);
var
  Query: TSQLQuery;
  XMLContent: TStringList;
  rssHeader, rssFooter, rssItemTemplate, rssItem: String;
  itemCount: Integer;
  Log : TStringList;
begin
  // Initialize variables
  Query := TSQLQuery.Create(nil);
  XMLContent := TStringList.Create;
  Log := TStringList.Create;
  try
    // Configure SQLQuery
    Query.Database := konnect;
    Query.Transaction := tranzact;
    Query.SQL.Text := 'SELECT id, caption, content, section, dt FROM content ORDER BY dt DESC LIMIT 20'; // Fetch latest 20 entries
    Query.Open;

    // Define RSS header and footer
    rssHeader :=
      '<?xml version="1.0" encoding="UTF-8"?>' +
      '<rss version="2.0">' +
      '<channel>' +
      '<title>Latest News</title>' +
      '<link>http://' + Domain + '</link>' +
      '<description>Latest news feed from the database</description>';

    rssFooter := '</channel></rss>';

    // Define RSS item template
    rssItemTemplate :=
      '<item>' +
      '<title>%s</title>' + // Caption
      '<link>http://' + Domain + '/%s</link>' + // Section
      '<description>%s</description>' + // Content
      '<pubDate>%s</pubDate>' + // Publication date
      '</item>';

    // Add RSS header to XML content
    XMLContent.Add(rssHeader);

    // Loop through query results and add RSS items
    itemCount := 0;
    while not Query.EOF do
    begin
      rssItem := Format(rssItemTemplate, [
        Query.FieldByName('caption').AsString,
         selectByMode( Mode,
                       Query.FieldByName('id').AsString + '.html',
                       getTreeUrl(  Query.FieldByName('id').AsString, '.html', konnect, tranzact)
         ),
      { #note : need URLs construction for page by id }
        clearCuttedToPlainFromHtml( Query.FieldByName('content').AsString, 50) ,
        FormatDateTime('ddd, dd mmm yyyy hh:nn:ss', Query.FieldByName('dt').AsDateTime)
      ]);
      XMLContent.Add(rssItem);
      Query.Next;
      Inc(itemCount);
    end;

    // Add RSS footer to XML content
    XMLContent.Add(rssFooter);

    // Write XML content to file
    XMLContent.SaveToFile(feedXMLFileName);
    Log.Add(Format('RSS feed created successfully with %d items.', [itemCount]));
    Log.Add('Filename: '+ feedXMLFileName);
    Log.SaveToFile('rss.log');



  finally
    // Free resources
    Log.Free;
    Query.Free;
    XMLContent.Free;
  end;
end;

end.