Accessing Google Feed API to retrieve media thumbnails

I am currently utilizing the Google Feed API to extract a thumbnail from an RSS feed ("media:thumbnail")

The media:thumbnail element in the RSS feed is structured as follows:

<media:thumbnail url="http://anyurl.com/thumbnailname.jpg" width="150" height="150"/>

Just to clarify, the thumbnail is not nested within a media:group

This is how the script appears:

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

function initialize() {
  var feed = new google.feeds.Feed("http://website.com/news/feed/");
  feed.setNumEntries(20);               
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      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.title));
        div.appendChild(document.createTextNode(entry.link));
        container.appendChild(div);
      }
    }
  });
}
google.setOnLoadCallback(initialize);

</script>

The TITLE and LINK fields are being fetched correctly. However, information about retrieving media:thumbnail or its URL specifically is missing from the Feed API documentation.

Does anyone have insights on how I can obtain the thumbnail URL using the feed API?

Answer №1

ImplementMIXED_FORMAT and utilize javascript to extract the media:thumbnail links:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

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

function initialize() {
    var feed = new google.feeds.Feed('http://channel9.msdn.com/Feeds/RSS');
    feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
    feed.setNumEntries(25);
    feed.load(function(result) {
    if (!result.error) {
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var mediaEntries = entry.xmlNode.getElementsByTagNameNS('*','thumbnail');
        for (var j = 0; j < mediaEntries.length; j++) {
            var mediaEntry = mediaEntries[j];
            var mediaThumbnailUrl = mediaEntry.attributes.getNamedItem('url').value
            console.log(mediaThumbnailUrl);
        }
      }
    }
  });
}
google.setOnLoadCallback(initialize);

</script> 

Answer №2

Consider changing the feed format to MIXED_FORMAT

feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);

This adjustment should result in an xmlNode being returned within result.feed.entires.


function initialize() {
  var url = "http://www.flickr.com/services/feeds/"
  +"photos_public.gne?tags=nature&format=rss_200";
  var feed = new google.feeds.Feed(url);
  feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
  feed.setNumEntries(5);               
  feed.load(function(result) {
    if (!result.error) {
    var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        // select the `media:thumbnail` element
        var mediaImage = Array.prototype.slice
        .call(entry.xmlNode.children).filter(function(el, i) {
          return el.nodeName === "media:thumbnail" 
        });
        var thumbnail = new Image;
        // set thumbnail `attributes` with `media:element` `attributes`
        Array.prototype.slice
       .call(mediaImage[0].attributes).forEach(function(key, _) {
              thumbnail[key.name.replace(/[url].*/,"src")] = key.value
        });
        var div = document.createElement("div");       
        div.appendChild(document.createTextNode(entry.title + "\n"));
        div.appendChild(document.createTextNode(entry.link + "\n"));
        div.appendChild(thumbnail);
        container.appendChild(div);                
      }
    }
  });
}
google.setOnLoadCallback(initialize);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("feeds", "1");
</script>
<div id="feed"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determining the local coordinate system of an object in THREE.js

I'm a beginner in Three.js and could use some guidance... I've been moving and rotating an object randomly multiple times. Eventually, I need to determine the orientation of the local coordinate system of that object for physics calculations... ...

Delay reading body until npm request completes

My current challenge involves using npm request and cheerio to extract webpages and analyze their HTML structure. Everything works smoothly in scenarios where the HTML is available upon request. However, I am facing a problem when a website initially displ ...

Looking for advice on using the ternary operator in your code? Let us

In my JS file, the code $scope.button = id ? "Edit" : "Add"; is functioning correctly. I am trying to implement it in the View like this: <button name="saveBtn" class="btn btn-primary" tabindex="10">{{person.id ? 'Edit' : 'Add&ap ...

Removing an HTML element entirely

Although I am utilizing .remove() method and it is effectively removing the desired element, upon inspecting the page source by right-clicking in a browser window, I can still see those removed elements. It seems like they are not being permanently delet ...

typescript what type of functionality do dynamic class methods provide

I'm currently working on building a class that creates dynamic methods during the constructor stage. While everything is functioning properly, I've encountered an issue with VS Code's auto suggestion not recognizing these dynamic methods. Ho ...

Reactjs and Isotope offer the ability to expand and collapse on click. In this unique feature, only one item can be expanded at

Currently, I am working on refining an isotope application where each item expands upon clicking it and collapses when another item is clicked. However, the issue I am facing is that multiple cells can be opened simultaneously. I am looking for the most ef ...

Custom HTML binding in expanding rows of Angular 2 DataTables

I am currently working on implementing a data table feature that allows for an extended child row to be displayed when clicking the + icon. This row will show additional data along with some buttons that are bound via AJAX before transitioning to Angular 2 ...

How can I convert the left links in my navigation bar to a CSS drop-down menu to make it more responsive on small screens?

Here is the structure of my HTML (at the top): <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></s ...

Ways to patiently wait in a for loop until the ajax call is completed

I am a beginner in web development and currently working on a small website project that involves using ajax to display new comments. Below is the function I have created: function show_comments() { $('div#P_all_posts>div').each(function () { ...

What is the most effective method for coding an input tag with specific restricted characters?

Introduction I have a unique idea for creating an input field of fixed length where users can fill in the gaps without modifying certain pre-filled characters. For example, I want to display "__llo w_rld!" and let users complete the missing characters. In ...

Issues with AngularJS compatibility on Internet Explorer 8

Recently, I've been developing a new Angular app and I'm trying to ensure that it's compatible with IE8. It seems like the app is loading in the routing information and the template partially, but I keep encountering an error in the console ...

Mastering Puppeteer: Tips for Successfully Submitting Forms

Can you use puppeteer to programmatically submit a form without a submit input? I have been successful with forms that include a submit input by using page.click('.input[type="submit"]'), but when the form does not have a submit input, focusing o ...

Reactstrap: Is it necessary to enclose adjacent JSX elements within a wrapping tag?

While working on my React course project, I encountered an issue with my faux shopping website. The error message that keeps popping up is: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...& ...

The custom tab component in React is currently not accepting the "disabledTabs" prop

I have designed a tab component as shown below: tab/index.jsx import React from 'react'; import TabHeader from './header'; import TabBody from './body'; import TabHeaderList from './header/list'; import TabBodyList ...

Exploring AngularJS's capabilities with asynchronous tasks

I am in the process of developing a simple app using AngularJS. One of the key functionalities I am working on is removing items from a list. To achieve this, I have created the following code snippet: $scope.removeItem = function(item) { var toRemove = ...

Struggling to organize and paginate numbers in angularjs and facing filtering and sorting issues

I'm running into some issues with getting the paging feature to work properly while applying filters. Currently, when the filters are active, the paging numbers do not display correctly and only filter the first page of results. What I'm aiming ...

Learn how to insert JavaScript code into the head of an iframe using jQuery

My goal is to inject javascript code into the head of an iframe using jquery with the code provided below. var snippets_js='<?php echo $snippets_javascript;?>'; var scriptjs = document.createElement("script"); scriptjs.type = "text/j ...

Implementing JavaScript if statements that evaluate to true without cycling through all my if statements

Hey everyone, I've encountered an issue with my code. When testing each part individually, everything works fine. However, when all parts are combined and the first IF statement is reached, the form gets submitted without validating the others. Can an ...

Retrieving the value of a checkbox in a React custom checkbox component

I am facing an issue with my dynamic checkbox functionality. I need to update the state based on the selected options only, but my attempt to filter the state on change is not working as expected. Can someone help me identify what went wrong? const check ...

I'm in the process of constructing a create-next-app and I need to retrieve data from a web API. However, I'm unsure of the best place to securely store the API key

I am working on building a create-next-app that will retrieve data from the News Catcher API and display it within my application. I have obtained an API key to access the News Catcher API. However, I am unsure of where to securely store the API key and h ...