-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject.js
74 lines (67 loc) · 1.68 KB
/
subject.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
const { get } = require('axios');
const { writeFile } = require('fs');
const { js2xml } = require('xml-js');
async function fetchSubjectsParents() {
try {
const response = await get('http://api.atf.morsalat.ir/v1/day/25/month/11/type/1');
return response.data.data;
} catch (error) {
console.error(error);
}
}
async function fetchPodcastsByParentId(parentId) {
try {
const response = await get(`http://api.atf.morsalat.ir/v1/podcasts?parent=${parentId}`);
return response.data.data;
} catch (error) {
console.error(error);
}
}
async function getPodcasts() {
const allpodcasts = [];
const parents = await fetchPodcastsParents();
for (const parent of parents) {
const podcasts = await fetchPodcastsByParentId(parent.id);
podcasts.forEach(podcast => {
allpodcasts.push(podcast);
});
}
const xmlObj = {
_declaration: {
_attributes: {
version: '1.0',
encoding: 'UTF-8'
}
},
urlset: {
_attributes: {
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
},
url: allpodcasts.map(podcast => {
const lastmod = new Date(podcast.timestamp*1000).toISOString();
return {
loc: {
_text: `http://ruzbarg.morsalat.ir/podcast/${podcast.id}`
},
lastmod: {
_text: lastmod
},
priority: {
_text: '0.7'
}
};
})
}
};
const xml = js2xml(xmlObj, { compact: true, spaces: 2 });
writeFile('podcasts.xml', xml, err => {
if (err) {
console.error(err);
return;
}
console.log('Podcasts generated successfully!');
});
}
module.exports = {
getPodcasts
};