How can I make an RSS feed from Youtube search using Google App Script?

落花浮王杯 提交于 2019-12-22 10:36:38

问题


Since Youtube shut down its RSS feeds for searches with it's newest version of the API, I've been trying to recreate them using Google App Script. Here's what I have so far (based off of this tutorial for converting a twitter widget to RSS):

function getSearches(a){
  try{
    var rss,title,link;

    title="Youtube RSS Feed";
    link="http://www.youtube.com";

    var d=ScriptApp.getService().getUrl()+"?"+a;
    rss='<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
    rss+='<channel><title>'+title+'</title>';
    rss+='<link>'+link+'</link>';
    rss+='<atom:link href="'+d+'" rel="self" type="application/rss+xml" />';
    rss+='<description>Youtube RSS feed updated on '+new Date()+'.</description>';

    var results = YouTube.Search.list('id, snippet', {
      q: a,
      maxResults: 50,
      order: 'date'
    });

    for(var i = 0; i < results.items.length; i++){
      var item = results.items[i];
      rss += "<item>";
      rss += "<title>" + item.snippet.title + "</title>";
      rss += "<link>http://www.youtube.com/watch?v=" + item.id.videoId + "</link>";
      rss += "<description>" + item.snippet.description + "</description>";
      rss += "<pubDate>" + Utilities.formatDate(new Date(item.snippet.publishedAt), "EDT", "EEE, dd MMM yyyy HH:mm:ss Z") + "</pubDate>";
      rss += "<guid>http://www.youtube.com/watch?v=" + item.id.videoId + "</guid>";
      rss += "</item>";
    }
    rss+="</channel></rss>";
    Logger.log(rss)
    return rss
  }
  catch(e){
    return"Something went wrong. Please retry after few minutes"
  }
}

function doGet(e){
  //var a = e.queryString();
  var a = getSearches("search term");

  return ContentService.createTextOutput(a).setMimeType(ContentService.MimeType.RSS);
}

When I publish this as a web app and test it, the resulting page looks good. I can click the links and they take me to the correct videos. However when I try to subscribe to the feed (using Inoreader in my case), it says that there is no feed found. If I subscribe to the web app url directly in my reader (again, Inoreader), it appears to work; but all of the entries link to the web app, not youtube, and return an error from Google App Script when clicked.

Ideally I want the web app to be able to take an any search term and return the feeds to subscribe to via https://script.google.com/macros/s/LONG_KEY/exec?SEARCH_TERM similar to how the twitter RSS linked above functions. Has anyone had any success with something like this or can give me pointers?


回答1:


So it would appear that my problem was the version of the published web app. I was not aware that not incrementing the version would cache the app as it was when it was first published under that revision. I noticed that code changes were showing up when I was testing the app; but when looking at the published url output, they were not present.

File > Manage Versions allowed me to increment the version number and then the changes started showing up and I was able to subscribe to the feed.



来源:https://stackoverflow.com/questions/30486682/how-can-i-make-an-rss-feed-from-youtube-search-using-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!