如何使用Google App Script从Youtube搜索中制作RSS提要? [英] How can I make an RSS feed from Youtube search using Google App Script?

查看:273
本文介绍了如何使用Google App Script从Youtube搜索中制作RSS提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Youtube使用其最新版本的API关闭了它的RSS提要,因此我一直试图使用Google App Script重新创建它们。这是我目前为止的内容(根据本教程进行转换一个Twitter小部件到RSS):

pre $ function getSearches(a){
try {
var rss,标题,链接;

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

var d = ScriptApp.getService()。getUrl()+?+ a;
rss ='<?xml version =1.0?>< rss version =2.0xmlns:atom =http://www.w3.org/2005/Atom>';
rss + ='< channel>< title>'+ title +'< / title>';
rss + ='< link>'+ link +'< / link>';
rss + ='< atom:link href ='+ d +' =selftype =application / rss + xml/>';
rss + ='< description> Youtube RSS feed已更新于'+ 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出错了,几分钟后请重试
}
}

函数doGet(e){
// var a = e.queryString();
var a = getSearches(search term);

返回ContentService.createTextOutput(a).setMimeType(ContentService.MimeType.RSS);
}

当我将它作为一个web应用程序发布并测试它时,好。我可以点击链接,然后他们带我看到正确的视频。但是,当我尝试订阅订阅源(在我的情况下使用Inoreader)时,它表示没有发现订阅源。如果我直接在我的阅读器(再次,Inoreader)中订阅Web应用程序的URL,它似乎工作;但所有条目链接到网络应用程序,而不是YouTube,并单击时从Google应用程序脚本返回错误。



理想情况下,我希望网络应用程序能够搜索任意搜索字词并返回订阅源,以通过 https://脚本进行订阅。 google.com/macros/s/LONG_KEY/exec?SEARCH_TERM 类似于twitter RSS链接的上述功能。有没有人有过这样的事情,或可以给我指针吗?

解决方案

因此,看来我的问题是版本已发布的网络应用程序。我并不知道不增加版本会缓存应用程序,因为它是在该修订版首次发布时缓存的。我注意到,当我测试应用程序时,代码更改显示出来;但是当查看已发布的url输出时,它们不存在。



文件>管理版本允许我增加版本号,然后更改开始显示,并且我可以订阅订阅源。


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?

解决方案

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.

这篇关于如何使用Google App Script从Youtube搜索中制作RSS提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆