Feature: Import OPML files

This commit is contained in:
PrestonN 2018-03-03 23:24:31 -05:00
parent 589429bdc0
commit 515f7dc842
5 changed files with 7727 additions and 1265 deletions

8918
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -64,6 +64,7 @@
"jquery": "^3.3.1",
"mustache": "^2.3.0",
"nedb": "^1.8.0",
"node-async-loop": "^1.2.2"
"node-async-loop": "^1.2.2",
"opml-to-json": "0.0.3"
}
}

View File

@ -38,6 +38,7 @@ const asyncLoop = require('node-async-loop');
const shell = electron.shell; // Used to open external links into the user's native browser.
const localDataStorage = electron.remote.app.getPath('userData'); // Grabs the userdata directory based on the user's OS
const clipboard = electron.clipboard;
const getOpml = require('opml-to-json'); // Gets the file type for imported files.
const fs = require('fs'); // Used to read files. Specifically in the settings page.
let currentTheme = '';
let apiKey;

View File

@ -209,6 +209,29 @@ function setTheme(option){
document.getElementsByTagName("head").item(0).replaceChild(newTheme, currentTheme);
}
/**
* Import Subscriptions from an OPML file.
*
* @param {string} subFile - The file location of the OPML file.
*
* @return {Void}
*/
function importOpmlSubs(json){
if(json[0]['folder'] !== 'YouTube Subscriptions'){
showToast('Invalid OPML File. Import is unsuccessful.');
return;
}
json.forEach((channel) => {
let channelId = channel['xmlurl'].replace('https://www.youtube.com/feeds/videos.xml?channel_id=', '');
addSubscription(channelId, false);
});
window.setTimeout(displaySubs, 1000);
showToast('Subscriptions have been imported!');
return;
}
/**
* Import a subscriptions file that the user provides.
*
@ -221,22 +244,45 @@ function importSubscriptions(){
dialog.showOpenDialog({
properties: ['openFile'],
filters: [
{name: 'Database File', extensions: ['db']},
{name: 'Database File', extensions: ['*']},
]
}, function(fileLocation){
if(typeof(fileLocation) === 'undefined'){
console.log('Import Aborted');
return;
}
if(typeof(fileLocation) !== 'object'){
showToast('Incorrect filetype. Import Aborted.');
console.log(fileLocation);
let i = fileLocation[0].lastIndexOf('.');
let fileType = (i < 0) ? '' : fileLocation[0].substr(i);
console.log(fileType);
/*if (fileType !== '.db'){
showToast('Incorrect filetype. Import was unsuccessful.');
return;
}
}*/
fs.readFile(fileLocation[0], function(readErr, data){
if(readErr){
showToast('Unable to read file. File may be corrupt or have invalid permissions.');
throw readErr;
}
if (data.includes("<opml")){
getOpml(data, function (error, json){
if (!error){
clearFile('subscriptions', false);
importOpmlSubs(json['children'][0]['children']);
}
});
return;
}
else if (fileType !== '.db'){
showToast('Incorrect file type. Import unsuccessful.');
return;
}
clearFile('subscriptions', false);
fs.writeFile(appDatabaseFile, data, function(writeErr){
if(writeErr){
showToast('Unable to create file. Please check your permissions and try again.');
@ -286,7 +332,7 @@ function exportSubscriptions(){
*
* @param {string} type - The type of file to be cleared.
*/
function clearFile(type){
function clearFile(type, showMessage = true){
console.log(type);
let dataBaseFile;
@ -310,6 +356,9 @@ function clearFile(type){
if(err){
throw err;
}
showToast('File has been cleared. Restart FreeTube to see the changes');
if (showMessage){
showToast('File has been cleared. Restart FreeTube to see the changes');
}
})
}

View File

@ -28,7 +28,7 @@ along with FreeTube. If not, see <http://www.gnu.org/licenses/>.
*
* @return {Void}
*/
function addSubscription(channelId) {
function addSubscription(channelId, useToast = true) {
console.log(channelId);
// Request YouTube API
let request = gapi.client.youtube.channels.list({
@ -50,10 +50,11 @@ function addSubscription(channelId) {
// Refresh the list of subscriptions on the side navigation bar.
subDb.insert(data, (err, newDoc) => {
displaySubs();
if (useToast){
showToast('Added ' + channelName + ' to subscriptions.');
displaySubs();
}
});
showToast('Added ' + channelName + ' to subscriptions.');
});
}