Added CSV support for subscribtion converter

This commit is contained in:
Shiba 2021-12-11 21:38:47 +00:00
parent 6cc149a5c6
commit dfdfe6778a
2 changed files with 19 additions and 4 deletions

View file

@ -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.
*

View file

@ -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;