From 48436e50b3be179562b9a5ce54006d5152c2df88 Mon Sep 17 00:00:00 2001 From: kskarthik Date: Tue, 26 Mar 2019 18:29:04 +0000 Subject: [PATCH] Add support to export as Newpipe & OPML formats (#243) * Add support to export as Newpipe & OPML formats * minor code cleanup in export subscriptions --- src/js/settings.js | 131 ++++++++++++++++++++++++++++++++---- src/templates/settings.html | 12 ++++ 2 files changed, 130 insertions(+), 13 deletions(-) diff --git a/src/js/settings.js b/src/js/settings.js index 8536fc7b6..b91394442 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -591,12 +591,56 @@ function exportSubscriptions() { const dateYear = date.getFullYear(); const dateString = 'freetube-subscriptions-' + dateYear + '-' + dateMonth + '-' + dateDay; - // Open user file browser. User gives location of file to be created. + switch(document.querySelector('#exportSelect').value){ + + case "NewPipe": + exportNewpipeSubscriptions(dateYear, dateMonth, dateDay); + break; + case "OPML": + exportOpmlSubscriptions(dateYear, dateMonth, dateDay); + break; + default: + // Open user file browser. User gives location of file to be created. + dialog.showSaveDialog({ + defaultPath: dateString, + filters: [{ + name: 'Database File', + extensions: ['db'] + }, ] + }, function(fileLocation) { + console.log(fileLocation); + if (typeof(fileLocation) === 'undefined') { + console.log('Export Aborted'); + return; + } + fs.readFile(appDatabaseFile, function(readErr, data) { + if (readErr) { + throw readErr; + } + fs.writeFile(fileLocation, data, function(writeErr) { + if (writeErr) { + throw writeErr; + } + showToast('Susbcriptions have been successfully exported'); + }); + }) + }); + } +} +/** + * Export the subscriptions database compatable with NewPipe. + * + * @return {Void} + */ +function exportNewpipeSubscriptions(dateYear, dateMonth, dateDay){ + + const dateString = 'newpipe-subscriptions-' + dateYear + '-' + dateMonth + '-' + dateDay; + dialog.showSaveDialog({ defaultPath: dateString, filters: [{ - name: 'Database File', - extensions: ['db'] + name: 'JSON', + extensions: ['json'] }, ] }, function(fileLocation) { console.log(fileLocation); @@ -604,20 +648,81 @@ function exportSubscriptions() { console.log('Export Aborted'); return; } - fs.readFile(appDatabaseFile, function(readErr, data) { - if (readErr) { - throw readErr; - } - fs.writeFile(fileLocation, data, function(writeErr) { - if (writeErr) { - throw writeErr; + returnSubscriptions().then((result)=>{ + let newpipe = { + app_version: "0.16.1", + app_version_int: 730, + subscriptions: [] } - showToast('Susbcriptions have been successfully exported'); - }); - }) + for (let i=0; i < result.length; i++) { + + let subs = { + service_id: 0, + url: `https://youtube.com/channel/${result[i].channelId}`, + name: result[i].channelName + } + + newpipe.subscriptions.push(subs); + fs.writeFile(fileLocation, JSON.stringify(newpipe), function(writeErr) { + if (writeErr) { + throw writeErr; + } + if (i === result.length-1) { + showToast('Susbcriptions have been successfully exported'); + return; + } + }); + } + }); }); } +/** + * Export subscriptions database as OPML. + * + * @return {Void} + */ +function exportOpmlSubscriptions(dateYear, dateMonth, dateDay){ + const dateString = 'freetube-subscriptions-' + dateYear + '-' + dateMonth + '-' + dateDay; + + dialog.showSaveDialog({ + defaultPath: dateString, + filters: [{ + name: 'OPML', + extensions: ['opml'] + }, ] + }, function(fileLocation) { + console.log(fileLocation); + if (typeof(fileLocation) === 'undefined') { + console.log('Export Aborted'); + return; + } + returnSubscriptions().then((result)=>{ + + let opml = ``; + + for (let i=0; i < result.length; i++) { + + let subs = ``; + + if (i === result.length-1) { + + subs += ``; + } + opml += subs; + fs.writeFile(fileLocation, opml, function(writeErr) { + if (writeErr) { + throw writeErr; + } + if (i === result.length-1) { + showToast('Susbcriptions have been successfully exported'); + return; + } + }); + } + }); + }); +} /** * Clear out the data in a file. * diff --git a/src/templates/settings.html b/src/templates/settings.html index 522d320d7..1086ca0c2 100644 --- a/src/templates/settings.html +++ b/src/templates/settings.html @@ -317,6 +317,18 @@

+
+
+
+ + + + +
IMPORT SUBSCRIPTIONS