Last active
March 20, 2020 05:13
-
-
Save thinkAmi/5996382 to your computer and use it in GitHub Desktop.
GoogleAppsScriptでRSS2.0フィードを出力する例 (使う時は、.jsを.gsにする)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet() { | |
var rss = makeRss(); | |
rss.setTitle('RSS 2.0 test'); | |
rss.setLink('http://example.com'); | |
rss.setDescription('RSS 2.0のテスト'); | |
rss.setLanguage('ja'); | |
rss.setAtomlink('http://example.com/rss'); | |
for (var i = 1; i < 3; i++){ | |
rss.addItem({title: 'ページ:' + i, | |
link: 'http://example.com/' + i, | |
description: i + 'つ目のページ', | |
pubDate: new Date() | |
}) | |
} | |
return ContentService.createTextOutput(rss.toString()) | |
.setMimeType(ContentService.MimeType.RSS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeRss = function(){ | |
var channel = XmlService.createElement('channel'); | |
var root = XmlService.createElement('rss') | |
.setAttribute('version', '2.0') | |
.setAttribute('xmlnsatom', "http://www.w3.org/2005/Atom") | |
.addContent(channel); | |
var title = ''; | |
var link = ''; | |
var description = ''; | |
var language = ''; | |
var atomlink = ''; | |
var items = {}; | |
var createElement = function(element, text){ | |
return XmlService.createElement(element).setText(text); | |
}; | |
return { | |
setTitle: function(value){ title = value; }, | |
setLink: function(value){ link = value; }, | |
setDescription: function(value){ description = value; }, | |
setLanguage: function(value){ language = value; }, | |
setAtomlink: function(value){ atomlink = value; }, | |
addItem: function(args){ | |
if (typeof args.title === 'undefined') { args.title = ''; } | |
if (typeof args.link === 'undefined') { args.link = ''; } | |
if (typeof args.description === 'undefined') { args.description = ''; } | |
if (!(args.pubDate instanceof Date)) { throw 'pubDateは日付型'; } | |
if (typeof args.timezone === 'undefined') { args.timezone = "JST"; } | |
if (typeof args.guid === 'undefined' && typeof args.link === 'undefined') { throw 'GUIDが設定できません'; } | |
var item = { | |
title: args.title, | |
link: args.link, | |
description: args.description, | |
pubDate: Utilities.formatDate(args.pubDate, args.timezone, "EEE, dd MMM yyyy HH:mm:ss Z"), | |
guid: args.guid === 'undefined' ? args.link : args.link | |
} | |
items[item.guid] = item; | |
}, | |
toString: function(){ | |
// createElementではコロンの指定ができないため、あとでreplaceする。 | |
// 誤って他のデータをreplaceしないよう、先頭に変換対象のatomlinkを持ってきておく | |
channel.addContent(XmlService.createElement("atomlink") | |
.setAttribute('href', atomlink) | |
.setAttribute('rel', 'self') | |
.setAttribute('type', 'application/rss+xml') | |
); | |
channel.addContent(createElement('title', title)); | |
channel.addContent(createElement('link', link)); | |
channel.addContent(createElement('description', description)); | |
channel.addContent(createElement('language', language)); | |
for (var i in items) | |
{ | |
channel.addContent( | |
XmlService | |
.createElement('item') | |
.addContent(createElement('title', items[i].title)) | |
.addContent(createElement('link', items[i].link)) | |
.addContent(createElement('description', items[i].description)) | |
.addContent(createElement('pubDate', items[i].pubDate)) | |
.addContent(createElement('guid', items[i].guid)) | |
); | |
} | |
var document = XmlService.createDocument(root); | |
var xml = XmlService.getPrettyFormat().format(document) | |
// コロンが必要なタグの変換作業は、xml化できたところで行う | |
var result = xml.replace('xmlnsatom', 'xmlns:atom') | |
.replace('<atomlink href=','<atom:link href='); | |
return result; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment