Allow importing / exporting of new subscriptions file from yt

This commit is contained in:
Preston 2020-11-01 16:23:06 -05:00
parent af7fbe29db
commit cd5023ae72
1 changed files with 192 additions and 1 deletions

View File

@ -30,6 +30,7 @@ export default Vue.extend({
subscriptionsPromptValues: [ subscriptionsPromptValues: [
'freetube', 'freetube',
'youtube', 'youtube',
'youtubeold',
'newpipe' 'newpipe'
] ]
} }
@ -59,6 +60,7 @@ export default Vue.extend({
const importNewPipe = this.$t('Settings.Data Settings.Import NewPipe') const importNewPipe = this.$t('Settings.Data Settings.Import NewPipe')
return [ return [
`${importFreeTube} (.db)`, `${importFreeTube} (.db)`,
`${importYouTube} (.json)`,
`${importYouTube} (.opml)`, `${importYouTube} (.opml)`,
`${importNewPipe} (.json)` `${importNewPipe} (.json)`
] ]
@ -69,6 +71,7 @@ export default Vue.extend({
const exportNewPipe = this.$t('Settings.Data Settings.Export NewPipe') const exportNewPipe = this.$t('Settings.Data Settings.Export NewPipe')
return [ return [
`${exportFreeTube} (.db)`, `${exportFreeTube} (.db)`,
`${exportYouTube} (.json)`,
`${exportYouTube} (.opml)`, `${exportYouTube} (.opml)`,
`${exportNewPipe} (.json)` `${exportNewPipe} (.json)`
] ]
@ -89,6 +92,9 @@ export default Vue.extend({
case 'youtube': case 'youtube':
this.importYouTubeSubscriptions() this.importYouTubeSubscriptions()
break break
case 'youtubeold':
this.importOpmlYouTubeSubscriptions()
break
case 'newpipe': case 'newpipe':
this.importNewPipeSubscriptions() this.importNewPipeSubscriptions()
break break
@ -219,7 +225,102 @@ export default Vue.extend({
}) })
}, },
handleYoutubeImportFile: function (filePath) {
fs.readFile(filePath, async (err, data) => {
if (err) {
const message = this.$t('Settings.Data Settings.Unable to read file')
this.showToast({
message: `${message}: ${err}`
})
return
}
let textDecode = new TextDecoder('utf-8').decode(data)
textDecode = JSON.parse(textDecode)
console.log(textDecode)
const primaryProfile = JSON.parse(JSON.stringify(this.profileList[0]))
const subscriptions = []
this.updateShowProgressBar(true)
this.setProgressBarPercentage(0)
let count = 0
textDecode.forEach((channel) => {
const snippet = channel.snippet
if (typeof snippet === 'undefined') {
throw new Error('Unable to find channel data')
}
const subscription = {
id: snippet.resourceId.channelId,
name: snippet.title,
thumbnail: snippet.thumbnails.default.url
}
const subExists = primaryProfile.subscriptions.findIndex((sub) => {
return sub.id === subscription.id || sub.name === subscription.name
})
if (subExists === -1) {
subscriptions.push(subscription)
}
count++
const progressPercentage = (count / textDecode.length) * 100
this.setProgressBarPercentage(progressPercentage)
if (count === textDecode.length) {
primaryProfile.subscriptions = primaryProfile.subscriptions.concat(subscriptions)
this.updateProfile(primaryProfile)
if (subscriptions.length < count) {
this.showToast({
message: this.$t('Settings.Data Settings.One or more subscriptions were unable to be imported')
})
} else {
this.showToast({
message: this.$t('Settings.Data Settings.All subscriptions have been successfully imported')
})
}
this.updateShowProgressBar(false)
}
})
})
},
importYouTubeSubscriptions: function () { importYouTubeSubscriptions: function () {
const options = {
properties: ['openFile'],
filters: [
{
name: 'Database File',
extensions: ['json']
}
]
}
dialog.showOpenDialog(options).then(async (response) => {
if (response.canceled || response.filePaths.length === 0) {
return
}
const filePath = response.filePaths[0]
try {
this.handleYoutubeImportFile(filePath)
} catch (err) {
console.log(err)
}
})
},
importOpmlYouTubeSubscriptions: function () {
const options = { const options = {
properties: ['openFile'], properties: ['openFile'],
filters: [ filters: [
@ -452,6 +553,9 @@ export default Vue.extend({
case 'youtube': case 'youtube':
this.exportYouTubeSubscriptions() this.exportYouTubeSubscriptions()
break break
case 'youtubeold':
this.exportOpmlYouTubeSubscriptions()
break
case 'newpipe': case 'newpipe':
this.exportNewPipeSubscriptions() this.exportNewPipeSubscriptions()
break break
@ -521,7 +625,94 @@ export default Vue.extend({
}) })
}, },
exportYouTubeSubscriptions: async function () { exportYouTubeSubscriptions: function () {
const date = new Date()
let dateMonth = date.getMonth() + 1
if (dateMonth < 10) {
dateMonth = '0' + dateMonth
}
let dateDay = date.getDate()
if (dateDay < 10) {
dateDay = '0' + dateDay
}
const dateYear = date.getFullYear()
const exportFileName = 'youtube-subscriptions-' + dateYear + '-' + dateMonth + '-' + dateDay + '.json'
const options = {
defaultPath: exportFileName,
filters: [
{
name: 'Database File',
extensions: ['json']
}
]
}
const subscriptionsObject = this.profileList[0].subscriptions.map((channel) => {
const object = {
contentDetails: {
activityType: 'all',
newItemCount: 0,
totalItemCount: 0
},
etag: '',
id: '',
kind: 'youtube#subscription',
snippet: {
channelId: channel.id,
description: '',
publishedAt: new Date(),
resourceId: {
channelId: channel.id,
kind: 'youtube#channel'
},
thumbnails: {
default: {
url: channel.thumbnail
},
high: {
url: channel.thumbnail
},
medium: {
url: channel.thumbnail
}
},
title: channel.name
}
}
return object
})
dialog.showSaveDialog(options).then((response) => {
if (response.canceled || response.filePath === '') {
// User canceled the save dialog
return
}
const filePath = response.filePath
fs.writeFile(filePath, JSON.stringify(subscriptionsObject), (writeErr) => {
if (writeErr) {
const message = this.$t('Settings.Data Settings.Unable to write file')
this.showToast({
message: `${message}: ${writeErr}`
})
return
}
this.showToast({
message: this.$t('Settings.Data Settings.Subscriptions have been successfully exported')
})
})
})
},
exportOpmlYouTubeSubscriptions: async function () {
const date = new Date() const date = new Date()
let dateMonth = date.getMonth() + 1 let dateMonth = date.getMonth() + 1