From dfdfe6778a22b4642a6cd9d84c8895adbf12d42f Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Sat, 11 Dec 2021 21:38:47 +0000 Subject: [PATCH] Added CSV support for subscribtion converter --- src/common/yt.ts | 15 ++++++++++++++- src/tools/YTtoLBRY.tsx | 8 +++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/common/yt.ts b/src/common/yt.ts index c72565b..1e2f08d 100644 --- a/src/common/yt.ts +++ b/src/common/yt.ts @@ -57,7 +57,7 @@ export const ytService = { */ readOpml(opmlContents: string): string[] { const opml = new DOMParser().parseFromString(opmlContents, 'application/xml'); - + opmlContents = '' return Array.from(opml.querySelectorAll('outline > outline')) .map(outline => outline.getAttribute('xmlUrl')) .filter((url): url is string => !!url) @@ -73,9 +73,22 @@ export const ytService = { */ readJson(jsonContents: string): string[] { const subscriptions: YtSubscription[] = JSON.parse(jsonContents); + jsonContents = '' return subscriptions.map(sub => sub.snippet.resourceId.channelId); }, + /** + * Reads an array of YT channel IDs from the YT subscriptions CSV file + * + * @param csvContent a CSV file as a string + * @returns the channel IDs + */ + readCsv(csvContent: string): string[] { + const rows = csvContent.split('\n') + csvContent = '' + return rows.map((row) => row.substr(0, row.indexOf(','))) + }, + /** * Extracts the channelID from a YT URL. * diff --git a/src/tools/YTtoLBRY.tsx b/src/tools/YTtoLBRY.tsx index 87cf16f..107c30d 100644 --- a/src/tools/YTtoLBRY.tsx +++ b/src/tools/YTtoLBRY.tsx @@ -14,9 +14,11 @@ import readme from './README.md' */ async function lbryChannelsFromFile(file: File) { const ext = file.name.split('.').pop()?.toLowerCase(); - const content = await getFileContent(file); - - const ids = new Set((ext === 'xml' || ext == 'opml' ? ytService.readOpml(content) : ytService.readJson(content))) + + const ids = new Set(( + ext === 'xml' || ext == 'opml' ? ytService.readOpml : + ext === 'csv' ? ytService.readCsv : + ytService.readJson)(await getFileContent(file))) const lbryUrls = await ytService.resolveById(...Array.from(ids).map(id => ({ id, type: 'channel' } as const))); const { platform } = await getSettingsAsync('platform'); const urlPrefix = platformSettings[platform].domainPrefix;