Skip to content

Instantly share code, notes, and snippets.

@sota1235
Created November 30, 2024 12:41
Show Gist options
  • Save sota1235/5603e0c61fdaf7c752d5644669265f37 to your computer and use it in GitHub Desktop.
Save sota1235/5603e0c61fdaf7c752d5644669265f37 to your computer and use it in GitHub Desktop.
parse opml script
// 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