mirror of https://github.com/FreeTubeApp/FreeTube
Feature: Import OPML files
This commit is contained in:
parent
589429bdc0
commit
515f7dc842
File diff suppressed because it is too large
Load Diff
|
@ -64,6 +64,7 @@
|
||||||
"jquery": "^3.3.1",
|
"jquery": "^3.3.1",
|
||||||
"mustache": "^2.3.0",
|
"mustache": "^2.3.0",
|
||||||
"nedb": "^1.8.0",
|
"nedb": "^1.8.0",
|
||||||
"node-async-loop": "^1.2.2"
|
"node-async-loop": "^1.2.2",
|
||||||
|
"opml-to-json": "0.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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 localDataStorage = electron.remote.app.getPath('userData'); // Grabs the userdata directory based on the user's OS
|
||||||
const clipboard = electron.clipboard;
|
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.
|
const fs = require('fs'); // Used to read files. Specifically in the settings page.
|
||||||
let currentTheme = '';
|
let currentTheme = '';
|
||||||
let apiKey;
|
let apiKey;
|
||||||
|
|
|
@ -209,6 +209,29 @@ function setTheme(option){
|
||||||
document.getElementsByTagName("head").item(0).replaceChild(newTheme, currentTheme);
|
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.
|
* Import a subscriptions file that the user provides.
|
||||||
*
|
*
|
||||||
|
@ -221,22 +244,45 @@ function importSubscriptions(){
|
||||||
dialog.showOpenDialog({
|
dialog.showOpenDialog({
|
||||||
properties: ['openFile'],
|
properties: ['openFile'],
|
||||||
filters: [
|
filters: [
|
||||||
{name: 'Database File', extensions: ['db']},
|
{name: 'Database File', extensions: ['*']},
|
||||||
]
|
]
|
||||||
}, function(fileLocation){
|
}, function(fileLocation){
|
||||||
if(typeof(fileLocation) === 'undefined'){
|
if(typeof(fileLocation) === 'undefined'){
|
||||||
console.log('Import Aborted');
|
console.log('Import Aborted');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(typeof(fileLocation) !== 'object'){
|
console.log(fileLocation);
|
||||||
showToast('Incorrect filetype. Import Aborted.');
|
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;
|
return;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
fs.readFile(fileLocation[0], function(readErr, data){
|
fs.readFile(fileLocation[0], function(readErr, data){
|
||||||
if(readErr){
|
if(readErr){
|
||||||
showToast('Unable to read file. File may be corrupt or have invalid permissions.');
|
showToast('Unable to read file. File may be corrupt or have invalid permissions.');
|
||||||
throw readErr;
|
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){
|
fs.writeFile(appDatabaseFile, data, function(writeErr){
|
||||||
if(writeErr){
|
if(writeErr){
|
||||||
showToast('Unable to create file. Please check your permissions and try again.');
|
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.
|
* @param {string} type - The type of file to be cleared.
|
||||||
*/
|
*/
|
||||||
function clearFile(type){
|
function clearFile(type, showMessage = true){
|
||||||
console.log(type);
|
console.log(type);
|
||||||
let dataBaseFile;
|
let dataBaseFile;
|
||||||
|
|
||||||
|
@ -310,6 +356,9 @@ function clearFile(type){
|
||||||
if(err){
|
if(err){
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (showMessage){
|
||||||
showToast('File has been cleared. Restart FreeTube to see the changes');
|
showToast('File has been cleared. Restart FreeTube to see the changes');
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ along with FreeTube. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* @return {Void}
|
* @return {Void}
|
||||||
*/
|
*/
|
||||||
function addSubscription(channelId) {
|
function addSubscription(channelId, useToast = true) {
|
||||||
console.log(channelId);
|
console.log(channelId);
|
||||||
// Request YouTube API
|
// Request YouTube API
|
||||||
let request = gapi.client.youtube.channels.list({
|
let request = gapi.client.youtube.channels.list({
|
||||||
|
@ -50,10 +50,11 @@ function addSubscription(channelId) {
|
||||||
|
|
||||||
// Refresh the list of subscriptions on the side navigation bar.
|
// Refresh the list of subscriptions on the side navigation bar.
|
||||||
subDb.insert(data, (err, newDoc) => {
|
subDb.insert(data, (err, newDoc) => {
|
||||||
displaySubs();
|
if (useToast){
|
||||||
});
|
|
||||||
|
|
||||||
showToast('Added ' + channelName + ' to subscriptions.');
|
showToast('Added ' + channelName + ' to subscriptions.');
|
||||||
|
displaySubs();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue