MythTV  master
mythrssmanager.cpp
Go to the documentation of this file.
1 // qt
2 #include <QDir>
3 #include <QFile>
4 #include <QString>
5 
6 #include "mythdate.h"
7 #include "mythdirs.h"
8 #include "mythcontext.h"
9 #include "mythlogging.h"
10 
11 #include "mythrssmanager.h"
12 #include "netutils.h"
13 #include "rssparse.h"
14 #include "mythsorthelper.h"
15 
16 using namespace std;
17 
18 #define LOC QString("RSSSite: ")
19 
20 // ---------------------------------------------------
21 
23 {
24  m_updateFreq = (gCoreContext->GetNumSetting(
25  "rss.updateFreq", 6) * 3600 * 1000);
26 
27  m_timer = new QTimer();
28 
29  connect( m_timer, SIGNAL(timeout()),
30  this, SLOT(doUpdate()));
31 }
32 
34 {
35  delete m_timer;
36 }
37 
39 {
40  m_timer->start(m_updateFreq);
41 }
42 
44 {
45  m_timer->stop();
46 }
47 
49 {
50  m_sites = findAllDBRSS();
51 
52  for (const auto *site : qAsConst(m_sites))
53  {
54  LOG(VB_GENERAL, LOG_INFO, LOC +
55  QString("Updating RSS Feed %1") .arg(site->GetTitle()));
56 
57  connect(site, SIGNAL(finished(RSSSite*)),
58  this, SLOT(slotRSSRetrieved(RSSSite*)));
59  }
60 
61  slotRefreshRSS();
62 
63  m_timer->start(m_updateFreq);
64 }
65 
67 {
68  if (m_sites.empty())
69  {
70  emit finished();
71  return;
72  }
73 
74  // NOLINTNEXTLINE(modernize-loop-convert)
75  for (auto i = m_sites.begin(); i != m_sites.end(); ++i)
76  {
77  (*i)->retrieve();
78  m_inprogress.append(*i);
79  }
80 }
81 
83 {
84  if (!site)
85  return;
86 
87  clearRSSArticles(site->GetTitle(), site->GetType());
88 
90  for (auto *video : qAsConst(rss))
91  {
92  // Insert in the DB here.
93  insertRSSArticleInDB(site->GetTitle(), video, site->GetType());
94  }
95 
96  m_inprogress.removeOne(site);
97  if (m_inprogress.isEmpty())
98  emit finished();
99 }
100 
102 {
103  markUpdated(site);
104  processAndInsertRSS(site);
105 }
106 
107 
109  QString sortTitle,
110  QString image,
111  const ArticleType& type,
112  QString description,
113  QString url,
114  QString author,
115  const bool& download,
116  QDateTime updated) :
117  m_title(std::move(title)), m_sortTitle(std::move(sortTitle)),
118  m_image(std::move(image)), m_type(type),
119  m_description(std::move(description)), m_url(std::move(url)),
120  m_author(std::move(author)),
121  m_download(download), m_updated(std::move(updated))
122 {
123  std::shared_ptr<MythSortHelper>sh = getMythSortHelper();
124  if (m_sortTitle.isEmpty() and not m_title.isEmpty())
125  m_sortTitle = sh->doTitle(m_title);
126 }
127 
129 {
130  QMutexLocker locker(&m_lock);
131  m_articleList.append(item);
132 }
133 
135 {
136  QMutexLocker locker(&m_lock);
137  m_articleList.clear();
138 }
139 
141 {
142  QMutexLocker locker(&m_lock);
143  m_data.resize(0);
144  m_articleList.clear();
145  m_urlReq = QUrl(m_url);
146  if (!m_manager)
147  {
148  m_manager = new QNetworkAccessManager();
149  connect(m_manager, SIGNAL(finished(QNetworkReply*)), this,
150  SLOT(slotCheckRedirect(QNetworkReply*)));
151  }
152 
153  m_reply = m_manager->get(QNetworkRequest(m_urlReq));
154 }
155 
156 QUrl RSSSite::redirectUrl(const QUrl& possibleRedirectUrl,
157  const QUrl& oldRedirectUrl)
158 {
159  QUrl redirectUrl;
160  if(!possibleRedirectUrl.isEmpty() && possibleRedirectUrl != oldRedirectUrl)
161  redirectUrl = possibleRedirectUrl;
162  return redirectUrl;
163 }
164 
165 void RSSSite::slotCheckRedirect(QNetworkReply* reply)
166 {
167  QVariant possibleRedirectUrl =
168  reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
169 
170  QUrl urlRedirectedTo;
171  urlRedirectedTo = redirectUrl(possibleRedirectUrl.toUrl(),
172  urlRedirectedTo);
173 
174  if(!urlRedirectedTo.isEmpty())
175  {
176  m_manager->get(QNetworkRequest(urlRedirectedTo));
177  }
178  else
179  {
180  m_data = m_reply->readAll();
181  process();
182  }
183 
184  reply->deleteLater();
185 }
186 
188 {
189  QMutexLocker locker(&m_lock);
190  return m_articleList;
191 }
192 
193 unsigned int RSSSite::timeSinceLastUpdate(void) const
194 {
195  QMutexLocker locker(&m_lock);
196 
197  QDateTime curTime(MythDate::current());
198  unsigned int min = m_updated.secsTo(curTime)/60;
199  return min;
200 }
201 
203 {
204  QMutexLocker locker(&m_lock);
205 
206  m_articleList.clear();
207 
208  if (!m_data.size())
209  {
210  emit finished(this);
211  return;
212  }
213 
214  QDomDocument domDoc;
215 
216  if (!domDoc.setContent(m_data, true))
217  {
218  LOG(VB_GENERAL, LOG_ERR, LOC +
219  "Failed to set content from downloaded XML");
220  emit finished(this);
221  return;
222  }
223 
224  //Check the type of the feed
225  QString rootName = domDoc.documentElement().nodeName();
226  if (rootName == "rss" || rootName == "rdf:RDF")
227  {
229  Parse parser;
230  items = parser.parseRSS(domDoc);
231 
232  for (const auto *item : qAsConst(items))
233  {
235  item->GetTitle(), item->GetSortTitle(),
236  item->GetSubtitle(), item->GetSortSubtitle(),
237  item->GetDescription(), item->GetURL(),
238  item->GetThumbnail(), item->GetMediaURL(),
239  item->GetAuthor(), item->GetDate(),
240  item->GetTime(), item->GetRating(),
241  item->GetFilesize(), item->GetPlayer(),
242  item->GetPlayerArguments(),
243  item->GetDownloader(),
244  item->GetDownloaderArguments(),
245  item->GetWidth(),
246  item->GetHeight(),
247  item->GetLanguage(),
248  item->GetDownloadable(),
249  item->GetCountries(),
250  item->GetSeason(),
251  item->GetEpisode(), false));
252  }
253  }
254  else
255  LOG(VB_GENERAL, LOG_ERR, LOC + "Data is not valid RSS-feed");
256 
257  emit finished(this);
258 }
RSSSite::slotCheckRedirect
void slotCheckRedirect(QNetworkReply *reply)
Definition: mythrssmanager.cpp:165
RSSSite::insertRSSArticle
void insertRSSArticle(ResultItem *item)
Definition: mythrssmanager.cpp:128
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
RSSManager::doUpdate
void doUpdate()
Definition: mythrssmanager.cpp:48
RSSSite::RSSSite
RSSSite(QString title, QString sortTitle, QString image, const ArticleType &type, QString description, QString url, QString author, const bool &download, QDateTime updated)
Definition: mythrssmanager.cpp:108
RSSSite::m_reply
QNetworkReply * m_reply
Definition: mythrssmanager.h:97
getMythSortHelper
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
Definition: mythsorthelper.cpp:126
RSSSite::timeSinceLastUpdate
unsigned int timeSinceLastUpdate(void) const
Definition: mythrssmanager.cpp:193
mythrssmanager.h
RSSManager::stopTimer
void stopTimer()
Definition: mythrssmanager.cpp:43
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:633
markUpdated
void markUpdated(RSSSite *site)
Definition: netutils.cpp:708
rssparse.h
RSSSite::GetType
const ArticleType & GetType() const
Definition: mythrssmanager.h:56
RSSSite::m_lock
QMutex m_lock
Definition: mythrssmanager.h:90
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
RSSSite::finished
void finished(RSSSite *item)
LOC
#define LOC
Definition: mythrssmanager.cpp:18
build_compdb.parser
parser
Definition: build_compdb.py:7
insertRSSArticleInDB
bool insertRSSArticleInDB(const QString &feedtitle, ResultItem *item, ArticleType type)
Definition: netutils.cpp:742
mythdirs.h
mythsorthelper.h
RSSSite::m_title
QString m_title
Definition: mythrssmanager.h:79
RSSManager::slotRefreshRSS
void slotRefreshRSS(void)
Definition: mythrssmanager.cpp:66
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
video
QDomElement video
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:658
Parse
Definition: rssparse.h:189
RSSSite::m_urlReq
QUrl m_urlReq
Definition: mythrssmanager.h:85
RSSSite::m_url
QString m_url
Definition: mythrssmanager.h:84
mythdate.h
RSSSite::m_data
QByteArray m_data
Definition: mythrssmanager.h:91
mythlogging.h
RSSSite::m_manager
QNetworkAccessManager * m_manager
Definition: mythrssmanager.h:98
RSSManager::startTimer
void startTimer()
Definition: mythrssmanager.cpp:38
RSSManager::~RSSManager
~RSSManager() override
Definition: mythrssmanager.cpp:33
RSSSite::redirectUrl
static QUrl redirectUrl(const QUrl &possibleRedirectUrl, const QUrl &oldRedirectUrl)
Definition: mythrssmanager.cpp:156
netutils.h
RSSManager::processAndInsertRSS
void processAndInsertRSS(RSSSite *site)
Definition: mythrssmanager.cpp:82
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:930
ArticleType
ArticleType
Definition: rssparse.h:20
RSSManager::RSSManager
RSSManager()
Definition: mythrssmanager.cpp:22
RSSSite::m_articleList
ResultItem::resultList m_articleList
Definition: mythrssmanager.h:95
findAllDBRSS
RSSSite::rssList findAllDBRSS()
Definition: netutils.cpp:611
ResultItem
Definition: rssparse.h:110
RSSSite::retrieve
void retrieve(void)
Definition: mythrssmanager.cpp:140
RSSSite::GetTitle
const QString & GetTitle() const
Definition: mythrssmanager.h:53
RSSSite::m_updated
QDateTime m_updated
Definition: mythrssmanager.h:88
RSSSite::GetVideoList
ResultItem::resultList GetVideoList(void) const
Definition: mythrssmanager.cpp:187
mythcontext.h
ResultItem::resultList
QList< ResultItem * > resultList
Definition: rssparse.h:114
RSSManager::slotRSSRetrieved
void slotRSSRetrieved(RSSSite *site)
Definition: mythrssmanager.cpp:101
clearRSSArticles
bool clearRSSArticles(const QString &feedtitle, ArticleType type)
Definition: netutils.cpp:722
RSSSite::m_sortTitle
QString m_sortTitle
Definition: mythrssmanager.h:80
RSSSite::clearRSSArticles
void clearRSSArticles(void)
Definition: mythrssmanager.cpp:134
RSSSite::process
void process(void)
Definition: mythrssmanager.cpp:202
RSSSite
Definition: mythrssmanager.h:20