Created
November 30, 2024 12:41
-
-
Save sota1235/5603e0c61fdaf7c752d5644669265f37 to your computer and use it in GitHub Desktop.
parse opml script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generated by ChatGPT | |
import * as fs from 'fs'; | |
import { parseStringPromise } from 'xml2js'; | |
// OPMLファイルをパースして出力する関数 | |
async function parseOpml(filePath: string): Promise<void> { | |
try { | |
// OPMLファイルを読み込む | |
const opmlData = fs.readFileSync(filePath, 'utf-8'); | |
// XMLをパース | |
const result = await parseStringPromise(opmlData); | |
// OPMLのアウトラインを取得 | |
const outlines = result.opml.body[0].outline; | |
// フィードのURLを収集 | |
const feedConfigs = outlines | |
.flatMap((outline: any) => getXmlUrls(outline)) | |
.map(([url, title]: [string, string]) => ({ url, title })); | |
// 結果をフォーマットして出力 | |
console.log('export const feedConfigs: FeedConfig[] = ['); | |
feedConfigs.forEach((feed: any) => { | |
console.log(` { | |
title: '${feed.title}', | |
url: '${feed.url}', | |
},`); | |
}); | |
console.log('];'); | |
} catch (error) { | |
console.error('Error parsing OPML:', error); | |
} | |
} | |
// 再帰的にxmlUrlを取得 | |
function getXmlUrls(outline: any): [string, string][] { | |
const result: [string, string][] = []; | |
if (outline.$?.xmlUrl) { | |
result.push([outline.$.xmlUrl, outline.$.title]); | |
} | |
if (outline.outline) { | |
outline.outline.forEach((child: any) => { | |
result.push(...getXmlUrls(child)); | |
}); | |
} | |
return result; | |
} | |
// 実行 | |
const opmlFilePath = 'feedly.opml'; // ここにOPMLファイルのパスを指定 | |
parseOpml(opmlFilePath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment