Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsRSS.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsRSS.cpp
4  *
5  * Description: latest news Wittyshare plugin
6  *
7  * Created: 03/07/12 11:07:15
8  *
9  * Author: Guy Deleeuw, Guy.Deleeuw@gmail.com
10  *
11  */
12 
13 #include <boost/filesystem.hpp>
14 
15 #include <stdio.h>
16 
17 #include <uuid/uuid.h>
18 
19 #include <Wt/WDateTime>
20 #include <Wt/WEnvironment>
21 
22 #include <gdcore/gdCore.h>
23 
24 #include <gdwtcore/gdWTextDelegate.h>
25 
27 #include <User/WsUser.h>
29 
30 #include <WsModule/WsModule.h>
31 
32 #include "WsSearchRSS.h"
33 #include "WsRSS.h"
34 
35 #define WSMODLATESTNEWS "wsmodlatestnews"
36 #define NB_NEWS "nb_news"
37 #define ROOT "root"
38 
39 using namespace Wt;
40 
41 WsRSS::WsRSS(WContainerWidget* parent)
42  : WContainerWidget(parent)
43 {
44  addStyleClass("WsRSS");
45 }
46 
48 { }
49 
51 {
52  WContainerWidget::load();
53 }
54 
56 {
57  WDateTime dt(WDateTime::currentDateTime());
58  std::string sDateTime = dt.toString("yyyy-MM-dd hh:mm:ss").narrow();
60  std::string sServerUrl = WsGlobalProperties::instance()->get("global", "serverUrl", "http://localhost");
61  // std::string serverUrl = asString(option("serverUrl")).toUTF8();
62  std::string serverDesc = asString(option("serverDesc")).toUTF8();
63  std::string serverTitle = asString(option("serverTitle")).toUTF8();
64  std::string rssFile = asString(option("rssFile")).toUTF8();
65  std::string path2RSS = user->getRootPath() + rssFile;
66  FILE* xmlFile = fopen(path2RSS.c_str(), "w+");
67  if ( !xmlFile ) return;
68  fprintf(xmlFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
69  fprintf(xmlFile, "<rss version=\"2.0\">\n");
70  fprintf(xmlFile, " <channel>\n");
71  fprintf(xmlFile, " <title>%s</title>\n", serverTitle.c_str());
72  fprintf(xmlFile, " <description>%s</description>\n", serverDesc.c_str());
73  fprintf(xmlFile, " <lastBuildDate>%s</lastBuildDate>\n", sDateTime.c_str());
74  fprintf(xmlFile, " <link>%s</link>\n", sServerUrl.c_str());
75  std::string rootPath = asString(option("rootPath")).narrow();
76  boost::algorithm::replace_all(rootPath, "&amp;", "&");
77  NodePtr root;
78  if ( rootPath.size() < 1 )
79  root = user->getAccessRoot();
80  else {
81  root = user->getAccessRoot();
82  root = root.get()->eatPath(rootPath);
83  }
84  if (root == 0) {
85  LOG(ERROR) << "WsRSS::setTable cannot get root !";
86  return;
87  }
88  WsSearchRSS searchRSS(root);
89  int rowsCount = asNumber(option("rowsCount"));
90  std::vector<NodePtr> news = searchRSS.getRSS(rowsCount);
91  /* Exemple le soir.be
92  <item>
93  <category><![CDATA[Monde]]></category>
94  <title><![CDATA[Jean-Luc Melenchon aux Chypriotes: «Bienvenue en enfer»]]></title>
95  <link>http://www.lesoir.be/209787/article/actualite/monde/2013-03-18/jean-luc-melenchon-aux-chypriotes-«bienvenue-en-enfer»</link>
96  <description><![CDATA[<img src="http://www.lesoir.be/sites/default/files/imagecache/flowpublish_preset/2013/03/18/1745967359_B97244034Z.1_20130318105823_000_GT9K6204.2-0.jpg" width="120" align="left" hspace="4" vspace="4" />
97  Chypre a dû accepter d’instaurer une taxe exceptionnelle sur les dépôts bancaires en contrepartie d’un prêt européen de 10 milliards d’euros. Le début de l’enfer, selon Jean-Luc Mélenchon. Qui est touché, et comment? Quelques repères sur le plan de sauvetage qui doit être soumis au Parlement.
98  ]]></description>
99  <comments>http://www.lesoir.be/209787/article/actualite/monde/2013-03-18/jean-luc-melenchon-aux-chypriotes-«bienvenue-en-enfer»#ancre_commentaires</comments>
100  <pubDate>Mon, 18 Mar 2013 09:49:47 +0100</pubDate>
101  <author><![CDATA[ Afp]]></author>
102  <guid isPermaLink="false">209787 at http://www.lesoir.be</guid>
103  <media:content url="http://www.lesoir.be/sites/default/files/imagecache/flowpublish_preset/2013/03/18/1745967359_B97244034Z.1_20130318105823_000_GT9K6204.2-0.jpg" type="image/jpeg" fileSize="1503654">
104  <media:title><![CDATA[]]></media:title>
105  <media:thumbnail url="http://www.lesoir.be/sites/default/files/imagecache/flowpublish_thumb_preset/2013/03/18/1745967359_B97244034Z.1_20130318105823_000_GT9K6204.2-0.jpg"/>
106  </media:content>
107  </item>
108  */
109  for (int i = 0; i < news.size(); ++i) {
110  if ( !(
111  news[i].get()->getPath().extension().string() == ".fhtml" ||
112  news[i].get()->getPath().extension().string() == ".pdf" ||
113  news[i].get()->getPath().extension().string() == ".rpg" ||
114  news[i].get()->getPath().extension().string() == ".zip"
115  ) )
116  continue;
117  if ( news[i].get()->getProperties().get()->get("global", "in_view", "true") == "false" ) continue;
118  boost::filesystem::path wsRes = news[i]->getPath();
119  bool bRes = false;
120  while (wsRes.string() != "/") {
121  wsRes = wsRes.parent_path();
122  if ( wsRes.filename().string() == "ws.res" ) {
123  bRes = true;
124  break;
125  }
126  if ( wsRes.filename().string() == "ws" ) {
127  bRes = true;
128  break;
129  }
130  continue;
131  }
132  if ( bRes ) continue;
133  fprintf(xmlFile, " <item>\n");
134  WDateTime dt(WDateTime::currentDateTime());
135  std::string sTitle = news[i].get()->getDisplayName(true);
136  std::string sDesc = news[i].get()->getProperties().get()->get("global", "short_description", "");
137  std::string sPubDate = news[i].get()->getProperties().get()->get("global", "publish_date", boost::lexical_cast<std::string>(dt.toTime_t()));
138  dt.setTime_t(boost::lexical_cast<time_t>(sPubDate));
139  sPubDate = dt.toString("yyyy-MM-dd hh:mm:ss").narrow();
140  // dateTime.setTime_t(news[i]->getModifyDate());
141  std::string sLink = sServerUrl + news[i]->getPath().string();
142  // boost::algorithm::replace_all(sTitle, "&", "&amp;");
143  // boost::algorithm::replace_all(sDesc, "&", "&amp;");
144  boost::algorithm::replace_all(sLink, "&", "&amp;");
145  char uuidBuff[36];
146  uuid_t uuidGenerated;
147  uuid_generate_random(uuidGenerated);
148  uuid_unparse(uuidGenerated, uuidBuff);
149  std::string sGuid = news[i]->getProperties().get()->get("global", "guid", uuidBuff);
150  fprintf(xmlFile, " <title><![CDATA[%s]]></title>\n", sTitle.c_str());
151  fprintf(xmlFile, " <description><![CDATA[%s]]></description>\n", sDesc.c_str());
152  fprintf(xmlFile, " <pubDate>%s</pubDate>\n", sPubDate.c_str());
153  fprintf(xmlFile, " <link>%s</link>\n", sLink.c_str());
154  fprintf(xmlFile, " <guid>%s</guid>\n", sGuid.c_str());
155  fprintf(xmlFile, " </item>\n");
156  }
157  fprintf(xmlFile, " </channel>\n");
158  fprintf(xmlFile, "</rss>\n");
159  fclose(xmlFile);
160 }
161 
Global properties.
WsUser * wsUser()
boost::shared_ptr< WsAbstractNode > NodePtr
static WsApplication * wsInstance()
Facility function to access the application object.
Definition: WsApplication.h:39
void buildRSS()
Definition: WsRSS.cpp:55
WsRSS(Wt::WContainerWidget *parent=0)
Definition: WsRSS.cpp:41
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
#define LOG
Definition: WsLogger.h:22
const boost::any & option(const std::string &attribute) const
Get an options value.
Definition: WsOption.cpp:62
std::string get(const std::string &section, const std::string &id, const std::string &def)
const std::string getRootPath()
return the root path of the filesystem tree, example : /var/www/demo_site
Definition: WsUser.cpp:55
vector< NodePtr > getRSS(int nb)
Definition: WsSearchRSS.cpp:21
static WsGlobalProperties * instance()
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
~WsRSS()
Definition: WsRSS.cpp:47
Abstract Node class.
#define ERROR
Definition: WsLogger.h:42
User class.
void load()
Definition: WsRSS.cpp:50