使用Google Feeds API(JavaScript)获取DeviantArt媒体RSS

时间:2012-10-20 12:57:32

标签: javascript rss media google-feed-api mediarss

我正在尝试使用Google的Feed API从DeviantArt提供的RSS源中获取媒体,以JSON格式返回。提要如下:

http://backend.deviantart.com/rss.xml?type=deviation&q=boost%3Apopular%20tarutaru

Google提供了许多示例,但很少涉及媒体供稿,而那些涉及媒体供稿的内容都使用了媒体组。但是看着我想读的这个特定的饲料,我没有看到任何媒体组。每个Feed条目只有一个与之关联的“媒体”项目。

<item>
<title>Expectant Tarutaru</title>
<link>http://requiem-shade.deviantart.com/art/Expectant-Tarutaru-38312114</link>
<guid isPermaLink="true">http://requiem-shade.deviantart.com/art/Expectant-Tarutaru-38312114</guid>
<pubDate>Sat, 19 Aug 2006 17:20:08 PDT</pubDate>
<media:title type="plain">Expectant Tarutaru</media:title>
<media:keywords/>
<media:rating>nonadult</media:rating>
<media:category label="Games">fanart/traditional/drawings/games</media:category>
<media:credit role="author" scheme="urn:ebu">Requiem-Shade</media:credit>
<media:credit role="author" scheme="urn:ebu">http://a.deviantart.net/avatars/r/e/requiem-shade.jpg</media:credit>
<media:copyright url="http://requiem-shade.deviantart.com">Copyright 2006-2012 !Requiem-Shade</media:copyright>        
<media:description type="html"><![CDATA[ A pregnant Tarutaru White Mage.  Was a request from another board.  Yes, I took liberties with Square-Enix's original design.  Yes, Tarutaru look like that as adults, except the females are flat-chested.  Had to augment her form to reflect the development of her body as her children grow inside her. ]]></media:description>
<media:thumbnail url="http://th06.deviantart.net/fs11/150/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="150" width="140"/>            
<media:thumbnail url="http://th03.deviantart.net/fs11/300W/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="322" width="300"/>
<media:content url="http://fc06.deviantart.net/fs11/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="463" width="431" medium="image"/>        
<media:content url="http://www.deviantart.com/download/38312114/" medium="document"/>
<description><![CDATA[ A pregnant Tarutaru White Mage.  Was a request from another board.  Yes, I took liberties with Square-Enix's original design.  Yes, Tarutaru look like that as adults, except the females are flat-chested.  Had to augment her form to reflect the development of her body as her children grow inside her.<br /><div><img src="http://th03.deviantart.net/fs11/300W/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" alt="thumbnail" /></div> ]]></description>
</item>

我现在用来阅读Feed的代码就是这样,现在只想读取media:title值,但我想阅读所有media:数据。

/*
*  How to load a feed via the Feeds API.
*/

google.load("feeds", "1");

// Our callback function, for when a feed is loaded.
function feedLoaded(result) {
    if (!result.error) {
        // Grab the container we will put the results into
        var container = document.getElementById("content");
        container.innerHTML = '';

        // Loop through the feeds, putting the titles onto the page.
        // Check out the result object for a list of properties returned in each entry.
        // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
        for (var i = 0; i < result.feed.entries.length; i++) {
            var entry = result.feed.entries[i];
            var div = document.createElement("div");
            div.appendChild(document.createTextNode(entry.mediaTitle));
            container.appendChild(div);
        }
    }
}

function OnLoad() {
    // Create a feed instance that will grab Digg's feed.
    var feed = new google.feeds.Feed("http://backend.deviantart.com/rss.xml?type=deviation&q=boost%3Apopular%20tarutaru");

    // Calling load sends the request off.  It requires a callback function.
    feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);​

取自此处提供的示例:http://code.google.com/apis/ajax/playground/#load_feed

问题是,我不知道达到这些价值的正确语法是什么,而且我已经尝试了几个小时,所以你现在是我唯一的希望。

任何帮助将不胜感激。这是我为我哥哥做的生日礼物,我希望尽快摆脱这种滋扰,以便送礼。 :)

提前谢谢。

1 个答案:

答案 0 :(得分:0)

查找用于读取XML提要的示例(我没有方便的URL)。这就是我做的事情: 我在调用feed.load()之前将格式设置为XML: feed.setResultFormat(google.feeds.Feed.XML_FORMAT); 然后,在回调中,我遍历Feed中的项目(看起来你可以和你的一样做,因为它作为项目返回)

function feedLoaded(result) {
  if (!result.error) {
  // Get all items returned.
  var items = result.xmlDocument.getElementsByTagName('item');

  // Loop through our items
  for (var i = 0; i < items.length; i++) {
    var item = items[i];

    // Get the data from the element.  firstChild is the text node containing
    // the title, and nodeValue returns the value of it.
    var title = item.getElementsByTagName('title')[0].firstChild.nodeValue;
    var link = item.getElementsByTagName('link')[0].firstChild.nodeValue;
}

那些应该按原样运作,因为您的Feed也有标题和链接。遵循相同的基本方案来获取其余数据。