-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcast.js
229 lines (203 loc) · 6.69 KB
/
podcast.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
if (js.podcast_load_headers == undefined)
js.global.load("podcast_routines.js");
var opts = load({}, "modopts.js", "Podcast");
var base;
var i;
var out;
var hdrs;
var img_title;
var img_link;
var item;
var m;
var item_info;
function encode_xml(str)
{
if (str==undefined)
return '';
str = str.toString();
str=str.replace(/&/g, '&');
str=str.replace(/</g, '<');
str=str.replace(/>/g, '>');
str=str.replace(/"/g, '"');
str=str.replace(/'/g, ''');
return str;
}
function add_channel_opt_attribute_rename(name, newName) {
if (opts[name] !== undefined)
out.write('\t\t<'+newName+'>'+encode_xml(opts[name])+'</'+newName+'>\n');
}
function add_channel_opt_attribute(name) {
var xml_name = name.substr(0,1).toLowerCase()+name.substr(1);
add_channel_opt_attribute_rename(name, xml_name);
}
function add_channel_itunes_attribute(name) {
add_channel_opt_attribute_rename('IT'+name, 'itunes:'+name.substr(0,1).toLowerCase()+name.substr(1));
}
function add_lost_episodes(out) {
var f = new File(system.data_dir + "podcast_lost_episodes.xml");
if(!f.open("r"))
return;
var xml = f.readAll();
f.close();
out.writeAll(xml);
}
function create_item_xml(base, hdr) {
var info;
var item;
info=podcast_get_info(base, hdr);
if (info == undefined)
return;
if (!podcast_get_enclosure_info(info.enclosure))
return;
item = '\t\t<item>\n';
item += '\t\t\t<title>'+encode_xml(info.title)+'</title>\n';
item += '\t\t\t<link>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</link>\n';
item += '\t\t\t<description>'+encode_xml(info.description)+'</description>\n';
// TODO: author?
// TODO: category?
item += '\t\t\t<comments>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</comments>\n';
item += '\t\t\t<enclosure url="'+encode_xml(info.enclosure.url)+'" length="'+encode_xml(info.enclosure.length)+'" type="'+encode_xml(info.enclosure.type)+'" />\n';
item += '\t\t\t<guid isPermaLink="false">'+encode_xml(info.guid)+'</guid>\n';
item += '\t\t\t<pubDate>'+encode_xml(info.pubDate)+'</pubDate>\n';
// TODO: source?
item += '\t\t</item>\n';
return item;
}
if (opts == undefined) {
log("Unable to load Podcast modopts");
exit(1);
}
if (opts.Sub === undefined) {
log("Sub undefined in modopts.ini");
exit(1);
}
if (opts.Filename == undefined) {
log("Filename undefined in modopts.ini.");
exit(1);
}
if (opts.Title == undefined) {
log("Title undefined in modopts.ini.");
exit(1);
}
if (opts.Description == undefined) {
log("Description undefined in modopts.ini.");
exit(1);
}
if (opts.FeedURI == undefined) {
log("FeedURI undefined in modopts.ini.");
exit(1);
}
if (opts.Link == undefined) {
log("Link undefined in modopts.ini.");
exit(1);
}
if (opts.From == undefined) {
log("From undefined in modopts.ini.");
exit(1);
}
if (opts.To == undefined) {
log("To undefined in modopts.ini.");
exit(1);
}
if (opts.EpisodeLink == undefined) {
log("EpisodeLink undefined in modopts.ini.");
exit(1);
}
base = new MsgBase(opts.Sub);
if (base == null) {
log('Unable to create "'+opts.Sub+'" MsgBase object.');
exit(1);
}
if (!base.open()) {
log('Unable to open sub '+opts.Sub+'.');
exit(1);
}
out = new File(opts.Filename+'.new');
if (!out.open("wxb")) {
log("Unable to open temorary file "+out.name+".");
exit(1);
}
hdrs = podcast_load_headers(base, opts.From, opts.To);
// TODO: iTunes tags?
out.write('<?xml version="1.0"?>\n');
out.write('<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">\n');
out.write('\t<channel>\n');
out.write('\t\t<title>'+encode_xml(opts.Title)+'</title>\n');
out.write('\t\t<description>'+encode_xml(opts.Description)+'</description>\n');
out.write('\t\t<link>'+encode_xml(opts.Link)+'</link>\n');
add_channel_opt_attribute('Language');
add_channel_opt_attribute('Copyright');
add_channel_opt_attribute('ManagingEditor');
add_channel_opt_attribute('WebMaster');
// TODO: pubDate
out.write('\t\t<lastBuildDate>' + encode_xml((new Date()).toUTCString()) + '</lastBuildDate>\n');
add_channel_opt_attribute('Category');
out.write('\t\t<generator>Synchronet Podcast Script '+("$Revision: 1.16 $".split(' ')[1])+'</generator>\n');
add_channel_opt_attribute('Docs');
// TODO: cloud (fancy!)
add_channel_opt_attribute_rename('TTL', 'ttl');
if (opts.ImageURL != undefined) {
img_title = opts.ImageTitle == undefined ? opts.Title : opts.ImageTitle;
img_link = opts.ImageLink == undefined ? opts.Link : opts.ImageLink;
out.write('\t\t<image>\n');
out.write('\t\t\t<url>'+encode_xml(opts.ImageURL)+'</url>\n');
out.write('\t\t\t<title>'+encode_xml(img_title)+'</title>\n');
out.write('\t\t\t<link>'+encode_xml(img_link)+'</link>\n');
if (opts.ImageHeight != undefined)
out.write('\t\t\t<height>'+encode_xml(opts.ImageHeight)+'</height>\n');
if (opts.ImageWidth != undefined)
out.write('\t\t\t<width>'+encode_xml(opts.ImageWidth)+'</width>\n');
out.write('\t\t</image>\n');
}
add_channel_opt_attribute('Rating');
// TODO: textInput?
add_channel_opt_attribute('SkipHours');
add_channel_opt_attribute('SkipDays');
add_channel_itunes_attribute('Author');
add_channel_itunes_attribute('Block');
if (opts.ITImageURL != undefined) {
out.write('\t\t<itunes:image href="'+encode_xml(opts.ITImageURL)+'" />\n');
}
else if(opts.ImageURL != undefined) {
out.write('\t\t<itunes:image href="'+encode_xml(opts.ImageURL)+'" />\n');
}
if (opts.ITCategory != undefined) {
if ((m = opts.ITCategory.match(/^\s*([^\/]+)\/(.+?)\s*$/)) != null) {
out.write('\t\t<itunes:category text="'+encode_xml(m[1])+'">\n');
out.write('\t\t\t<itunes:category text="'+encode_xml(m[2])+'" />\n');
out.write('\t\t</itunes:category>\n');
}
else {
out.write('\t\t<itunes:category text="'+encode_xml(opts.ITCategory)+'" />\n');
}
}
add_channel_itunes_attribute('Explicit');
add_channel_itunes_attribute('Complete');
add_channel_opt_attribute_rename('ITNewFeedURL', 'itunes:new-feed-url');
if (opts.ITOwnerEmail != undefined) {
out.write('\t\t<itunes:owner>\n');
out.write('\t');
add_channel_opt_attribute_rename('ITOwnerEmail', 'itunes:email');
if (opts.ITOwnerName != undefined) {
out.write('\t');
add_channel_opt_attribute_rename('ITOwnerName', 'itunes:name');
}
out.write('\t\t</itunes:owner>\n');
}
add_channel_itunes_attribute('Subtitle');
add_channel_itunes_attribute('Summary');
out.write('\t\t<atom:link href="'+opts.FeedURI+'" rel="self" type="application/rss+xml" />\n');
for (i=hdrs.length - 1; i >= 0; i--) {
item = create_item_xml(base, hdrs[i]);
if (item != undefined)
out.write(item);
}
add_lost_episodes(out); // These messages were purged from dove-net/entertainment (oops)
out.write('\t</channel>\n');
out.write('</rss>\n');
out.close();
if (!file_rename(out.name, opts.Filename)) {
log("Unable to rename "+out.name+" to "+opts.Filename+".");
file_remove(out.name);
exit(1);
}