Skip to content

Commit

Permalink
MythNews: improve media:content parsing in rss feeds
Browse files Browse the repository at this point in the history
* use the medium or type attributes to determine whether the content is an
  image or video. Previously we ignored the type and ended up trying to play
  an image.

* if we haven't found any thumbnail image then use any images found in the
  media:content as the thumbnail. Fixes #11161.

* parse all the media:content items not just the first one we find.
  • Loading branch information
Paul Harrison committed Apr 30, 2013
1 parent 7c31160 commit 7d9c51e
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions mythplugins/mythnews/mythnews/newssite.cpp
Expand Up @@ -286,6 +286,26 @@ void NewsSite::process(void)

}

static bool isImage(const QString &mimeType)
{
if (mimeType == "image/png" || mimeType == "image/jpeg" ||
mimeType == "image/jpg" || mimeType == "image/gif" ||
mimeType == "image/bmp")
return true;

return false;
}

static bool isVideo(const QString &mimeType)
{
if (mimeType == "video/mpeg" || mimeType == "video/x-ms-wmv" ||
mimeType == "application/x-troff-msvideo" || mimeType == "video/avi" ||
mimeType == "video/msvideo" || mimeType == "video/x-msvideo")
return true;

return false;
}

void NewsSite::parseRSS(QDomDocument domDoc)
{
QMutexLocker locker(&m_lock);
Expand Down Expand Up @@ -349,6 +369,7 @@ void NewsSite::parseRSS(QDomDocument domDoc)
//////////////////////////////////////////////////////////////
// From this point forward, we process RSS 2.0 media tags.
// Please put all other tag processing before this comment.
// See http://www.rssboard.org/media-rss for details
//////////////////////////////////////////////////////////////

// Some media: tags can be enclosed in a media:group item.
Expand Down Expand Up @@ -380,26 +401,41 @@ void NewsSite::parseRSS(QDomDocument domDoc)
if (!descNode.isNull())
description = descNode.toElement().text().simplified();

if (enclosure.isEmpty())
// parse any media:content items looking for any images or videos
QDomElement e = itemNode.toElement();
QDomNodeList mediaNodes = e.elementsByTagName("media:content");
for (int x = 0; x < mediaNodes.count(); x++)
{
QDomNode contentNode = itemNode.namedItem("media:content");
if (!contentNode.isNull())
{
QDomAttr enclosureURL = contentNode.toElement()
.attributeNode("url");
QString medium;
QString type;
QString url;

if (!enclosureURL.isNull())
enclosure = enclosureURL.value();
QDomElement mediaElement = mediaNodes.at(x).toElement();

QDomAttr enclosureType = contentNode.toElement()
.attributeNode("type");
if (mediaElement.isNull())
continue;

if (!enclosureType.isNull())
enclosure_type = enclosureType.value();
}
}
if (mediaElement.hasAttribute("medium"))
medium = mediaElement.attributeNode("medium").value();

if (mediaElement.hasAttribute("type"))
type = mediaElement.attributeNode("type").value();

(void) enclosure_type; // not currently used...
if (mediaElement.hasAttribute("url"))
url = mediaElement.attributeNode("url").value();

LOG(VB_GENERAL, LOG_DEBUG,
QString("parseRSS found media:content: medium: %1, type: %2, url: 3")
.arg(medium).arg(type).arg(url));

// if this is an image use it as the thumbnail if we haven't found one yet
if (thumbnail.isEmpty() && (medium == "image" || isImage(type)))
thumbnail = url;

// if this is a video use it as the enclosure if we haven't found one yet
if (enclosure.isEmpty() && (medium == "video" || isVideo(type)))
enclosure = url;
}

insertNewsArticle(NewsArticle(title, description, url,
thumbnail, mediaurl, enclosure));
Expand Down

0 comments on commit 7d9c51e

Please sign in to comment.