|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as opmlToJSON from "opml-to-json"; |
| 4 | +import * as rimraf from "rimraf"; |
| 5 | + |
| 6 | +import * as Parser from "rss-parser"; |
| 7 | + |
| 8 | +import { OPML } from "./opml"; |
| 9 | + |
| 10 | +let parser = new Parser({ |
| 11 | + headers: { |
| 12 | + Accept: "application/rss+xml", |
| 13 | + "User-Agent": "goodengineering.dev", |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +async function Process({ source, output }: { source: string; output: string }) { |
| 18 | + const opmlRaw = fs.readFileSync(path.join(__dirname, source)); |
| 19 | + |
| 20 | + const opml: OPML = await new Promise((resolve, reject) => |
| 21 | + opmlToJSON(opmlRaw, (err, json) => { |
| 22 | + if (err) { |
| 23 | + reject(err); |
| 24 | + } |
| 25 | + resolve(json); |
| 26 | + }) |
| 27 | + ); |
| 28 | + |
| 29 | + if (!opml.children) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + rimraf.sync(path.join(__dirname, output)); |
| 34 | + |
| 35 | + await Promise.all( |
| 36 | + opml.children |
| 37 | + .map( |
| 38 | + folder => |
| 39 | + folder.children && |
| 40 | + folder.children.map(async feed => { |
| 41 | + try { |
| 42 | + const url = new URL(feed.htmlurl); |
| 43 | + |
| 44 | + const items = await parser.parseURL(feed.xmlurl); |
| 45 | + |
| 46 | + await fs.mkdirSync(path.join(__dirname, output, url.hostname), { |
| 47 | + recursive: true, |
| 48 | + }); |
| 49 | + |
| 50 | + await fs.writeFileSync( |
| 51 | + path.join(__dirname, output, url.hostname, "feed.json"), |
| 52 | + JSON.stringify({ ...feed, ...items }, null, 2), |
| 53 | + { encoding: "utf8" } |
| 54 | + ); |
| 55 | + return true; |
| 56 | + } catch (err) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + }) |
| 60 | + ) |
| 61 | + .reduce((a, b) => a.concat(b), []) |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export { Process }; |
0 commit comments