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