This repository was archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSS.gs
94 lines (77 loc) · 2.67 KB
/
RSS.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Code here is not meant to be modified. It wraps the actual logic
//Code that is run before first call
function start() {
//initialize is a USER-DEFINED argument-less function, which may be a stub
initialize ();
}
//this function is the entry point when the script URL is pinged
//the parameter of "e" is the JSON object generated from "?key1=value1&key2=..."
function doGet(e) {
var parameters = e.parameter;
/* So as not to hit web resources too often, content is stored in cache
for specific parameter-determined feed. Content is only refreshed when cache expires */
//note that too many params will cause this to fail
var id = Utilities.base64Encode(Utilities.jsonStringify(parameters));
var cache = CacheService.getPublicCache();
var rss = cache.get(id);
//if item not in cache...
if ( ! rss ) {
//get manually. processParameters is USER-DEFINED
rss = createRSSFeed(processParameters(parameters))
//then store in cache for USER-DEFINED amount of time (in seconds)
cache.put(id, rss, UpdateInterval);
}
//return the RSS feed to client, whether from cache or freshly generated
return ContentService.createTextOutput(rss)
.setMimeType(ContentService.MimeType.RSS);
}
//Last step. Wraps a JSON object generated by processParameters into an actual feed.
/*processParameters should have the following format:
{
title: ...
link: ...
date: ...
posts:
[ {title: ..., author: ..., id: ..., link: ..., content: ..., }, ... ]
}
*/
function createRSSFeed(object) {
//utility method. Wraps key/value pair to HTML node
function wrapInTag(tag, item){
if (item === "" || tag === "")
{
return ""
}
else
{
return "<" + tag + ">" + item + "</" + tag + ">"
}
}
rss = '<?xml version="1.0"?><rss version="2.0">';
rss += ' <channel>';
rss += wrapInTag("title", object.title);
rss += wrapInTag("link", object.link);
rss += wrapInTag("date", object.date);
for (var i = 0; i < object.posts.length; i++) {
post = object.posts[i];
rss += "<item>";
rss += wrapInTag("title", post.title);
rss += wrapInTag ("author", post.author);
rss += wrapInTag("pubDate", post.date);
rss += wrapInTag("guid", post.id);
rss += wrapInTag ("link", post.link);
rss += wrapInTag("description", "<![CDATA[<img src= " + post.content + ">]]>");
rss += "</item>";
}
rss += "</channel></rss>";
return rss;
}
//Utility function for debugging. Allows one to force wipe cache before running
function debugWrap(e, doClearCache){
if (doClearCache){
var id = Utilities.base64Encode(Utilities.jsonStringify(e.parameter));
var cache = CacheService.getPublicCache();
cache.remove(id);
}
doGet(e)
}