Skip to content

Commit e00b7d1

Browse files
authored
Merge pull request #263 from stetime/bugfix
Bugfix: Handle absence of media:content in XML parsing
2 parents 2ea1d8f + 67e4800 commit e00b7d1

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/parser.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Parser {
2626
if (!options.timeout) options.timeout = DEFAULT_TIMEOUT;
2727
this.options = options;
2828
this.xmlParser = new xml2js.Parser(this.options.xml2js);
29+
this.etags = {};
30+
this.lastModified = {};
2931
}
3032

3133
parseString(xml, callback) {
@@ -73,6 +75,12 @@ class Parser {
7375
let get = feedUrl.indexOf('https') === 0 ? https.get : http.get;
7476
let urlParts = url.parse(feedUrl);
7577
let headers = Object.assign({}, DEFAULT_HEADERS, this.options.headers);
78+
if (this.etags[feedUrl]) {
79+
headers['If-None-Match'] = this.etags[feedUrl];
80+
}
81+
if (this.lastModified[feedUrl]) {
82+
headers['If-Modified-Since'] = this.lastModified[feedUrl];
83+
}
7684
let timeout = null;
7785
let prom = new Promise((resolve, reject) => {
7886
const requestOpts = Object.assign({headers}, urlParts, this.options.requestOptions);
@@ -84,9 +92,21 @@ class Parser {
8492
const newLocation = url.resolve(feedUrl, res.headers['location']);
8593
return this.parseURL(newLocation, null, redirectCount + 1).then(resolve, reject);
8694
}
87-
} else if (res.statusCode >= 300) {
88-
return reject(new Error("Status code " + res.statusCode))
95+
} else if (res.statusCode === 304) {
96+
return resolve(null);
97+
}
98+
else if (res.statusCode >= 300) {
99+
return reject(new Error("Status code " + res.statusCode));
100+
}
101+
102+
if (res.headers['etag']) {
103+
this.etags[feedUrl] = res.headers['etag'];
104+
}
105+
106+
if (res.headers['last-modified']) {
107+
this.lastModified[feedUrl] = res.headers['last-modified'];
89108
}
109+
90110
let encoding = utils.getEncodingFromContentType(res.headers['content-type']);
91111
res.setEncoding(encoding);
92112
res.on('data', (chunk) => {
@@ -226,7 +246,7 @@ class Parser {
226246
}
227247
if (xmlItem.category) item.categories = xmlItem.category;
228248

229-
var mediaContent = xmlItem['media:content'][0].$;
249+
var mediaContent = xmlItem['media:content']?.[0]?.$ ?? null;
230250
if(mediaContent) item.mediaContent = mediaContent;
231251

232252
this.setISODate(item);

0 commit comments

Comments
 (0)