Repository navigation
Expand file tree
/
Copy pathtest-rss.js
More file actions
91 lines (83 loc) · 2.71 KB
/
Copy pathtest-rss.js
File metadata and controls
91 lines (83 loc) · 2.71 KB
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
// Simple test script for RSS feed parsing
import Parser from 'rss-parser';
import axios from 'axios';
// Create a more forgiving parser
const parser = new Parser({
customFields: {
item: [
['media:content', 'mediaContent', { keepArray: true }],
['content:encoded', 'contentEncoded'],
['media:thumbnail', 'thumbnail'],
],
},
defaultRSS: 2.0,
xml2js: {
normalize: true,
normalizeTags: true,
explicitArray: false,
trim: true,
strict: false
}
});
// RSS Feeds to test
const feeds = [
'https://bikerumor.com/feed/',
'https://electrek.co/feed/',
'https://www.rvbusiness.com/feed/'
];
async function testFeed(url) {
console.log(`\nTesting feed: ${url}`);
try {
// First try the direct approach
try {
console.log('Attempting direct parsing...');
const feed = await parser.parseURL(url);
console.log(`✓ Direct parsing succeeded`);
console.log(`Feed title: ${feed.title}`);
console.log(`Items found: ${feed.items ? feed.items.length : 0}`);
if (feed.items && feed.items.length > 0) {
console.log(`First item title: ${feed.items[0].title}`);
}
return;
} catch (directError) {
console.log(`✗ Direct parsing failed: ${directError.message}`);
// Try with axios as fallback
console.log('Attempting axios fallback...');
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; OutdoorTechPulse/1.0)',
'Accept': 'application/rss+xml, application/xml, text/xml, */*'
},
responseType: 'text',
timeout: 5000
});
if (response.data && typeof response.data === 'string') {
console.log(`Got data (${response.data.length} bytes)`);
try {
const feed = await parser.parseString(response.data);
console.log(`✓ Axios fallback parsing succeeded`);
console.log(`Feed title: ${feed.title}`);
console.log(`Items found: ${feed.items ? feed.items.length : 0}`);
if (feed.items && feed.items.length > 0) {
console.log(`First item title: ${feed.items[0].title}`);
}
} catch (parseError) {
console.log(`✗ Axios fallback parsing failed: ${parseError.message}`);
console.log('Data sample:', response.data.substring(0, 100) + '...');
}
} else {
console.log('✗ No valid data received from axios');
}
}
} catch (error) {
console.error(`Error testing feed ${url}:`, error.message);
}
}
async function main() {
console.log('RSS Feed Test Script');
console.log('===================');
for (const feed of feeds) {
await testFeed(feed);
}
}
main().catch(console.error);