Offline watch
This commit is contained in:
Theenoro 2017-08-23 00:20:53 +02:00
parent 44042f5d5e
commit acab4b1908
5 changed files with 142 additions and 55 deletions

View File

@ -1,68 +1,93 @@
$(function(){ $(function() {
var download_progress = $('#download-progress'); var download_progress = $('#download-progress');
var out = $('#output'); var out = $('#output');
const ipcRenderer = require('electron').ipcRenderer; const ipcRenderer = require('electron').ipcRenderer;
const request = require('request'); const request = require('request');
const {dialog} = require('electron').remote; const {
dialog
} = require('electron').remote;
var cache = {}; var cache = {};
var path = ""; var path = "";
ipcRenderer.on('download-progress', function(event, arg) { ipcRenderer.on('download-progress', function(event, arg) {
console.log(arg); // prints "pong" console.log(arg); // prints "pong"
$('#download-progress-'+arg.id).css("width",arg.percent); $('#download-progress-' + arg.id).css("width", arg.percent);
$('#download-progress-'+arg.id).html(arg.percent); $('#download-progress-' + arg.id).html(arg.percent);
}); });
ipcRenderer.on('process-fin', function(event, arg) { ipcRenderer.on('process-fin', function(event, arg) {
console.log(arg); // prints "pong" console.log(arg); // prints "pong"
$('#download-progress-'+arg.id).css("width",arg.percent); $('#download-progress-' + arg.id).css("width", arg.percent);
$('#download-progress-'+arg.id).html("CONVERTED"); $('#download-progress-' + arg.id).html("CONVERTED");
}); });
ipcRenderer.on('file', function(event, arg) { ipcRenderer.on('file', function(event, arg) {
console.log(arg); // prints "pong" console.log(arg); // prints "pong"
$('#body-'+arg.id).append('<audio src="file://'+path+'/'+cache[arg.id]+'.mp3" controls="true"/>'); $('#body-' + arg.id).append('<audio src="file://' + path + '/' + cache[arg.id] + '.mp3" controls="true"/>');
}); });
document.getElementById('party').addEventListener('click', _ => { document.getElementById('party').addEventListener('click', _ => {
path = dialog.showOpenDialog({ path = dialog.showOpenDialog({
properties: ['openDirectory'] properties: ['openDirectory']
})[0]; })[0];
console.log(path) console.log(path)
ipcRenderer.send('setPath', {path:path}); ipcRenderer.send('setPath', {
path: path
});
}) })
$('body').delegate('.format','click',function(e){ $('body').delegate('.format', 'click', function(e) {
ipcRenderer.send('set-format', {format:e.currentTarget.dataset.format}); ipcRenderer.send('set-format', {
format: e.currentTarget.dataset.format
});
console.log(e.currentTarget.dataset.format); console.log(e.currentTarget.dataset.format);
}) })
$('#start-download').click(function(){ $('#start-download').click(function() {
var video_id = $('#url').val().split('v=')[1]; var url = $('#url').val();
var ampersandPosition = video_id.indexOf('&');
if(ampersandPosition != -1) { if (url.match('crunchyroll')) {
video_id = video_id.substring(0, ampersandPosition); var video_id = Math.floor(Date.now() / 1000);
} $('<div class="media" ><div class="media-left"><a href="#"><img id="img-' + video_id + '" width="128" class="media-object" src="' + '" alt="..."></a></div><div class="media-body" id="body-' + video_id + '"><h4 class="media-heading">' + url + '</h4> Author: ' + '<div class="progress"><div class="progress-bar" id="download-progress-' + video_id + '" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
var playlist_id = $('#url').val().split('list=')[1]; ipcRenderer.send('start-download', {
if(typeof playlist_id !== 'undefined'){ url: $('#url').val(),
var playlist_idPOS = playlist_id.indexOf('&'); id: video_id
if(playlist_idPOS != -1) { });
playlist_id = playlist_id.substring(0, playlist_idPOS); } else {
var video_id = $('#url').val().split('v=')[1];
var ampersandPosition = video_id.indexOf('&');
if (ampersandPosition != -1) {
video_id = video_id.substring(0, ampersandPosition);
} }
var playlist_id = $('#url').val().split('list=')[1];
if (typeof playlist_id !== 'undefined') {
var playlist_idPOS = playlist_id.indexOf('&');
if (playlist_idPOS != -1) {
playlist_id = playlist_id.substring(0, playlist_idPOS);
}
request
.get('https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list=' + playlist_id + '&format=json', function(err, httpResponse, body) {
var YT = JSON.parse(body);
$('<div class="media" ><div class="media-left"><a href="#"><img id="img-' + video_id + '" width="128" class="media-object" src="' + YT.thumbnail_url + '" alt="..."></a></div><div class="media-body" id="body-' + video_id + '"><h4 class="media-heading">' + YT.title + '</h4> Playlistauthor: ' + YT.author_name + '<div class="progress"><div class="progress-bar" id="download-progress-' + video_id + '" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download-pl', {
url: $('#url').val(),
id: video_id,
folder: YT.author_name + '-' + YT.title
});
})
} else {
request
.get('https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=' + video_id + '&format=json', function(err, httpResponse, body) {
var YT = JSON.parse(body);
$('<div class="media" ><div class="media-left"><a href="#"><img id="img-' + video_id + '" width="128" class="media-object" src="' + YT.thumbnail_url + '" alt="..."></a></div><div class="media-body" id="body-' + video_id + '"><h4 class="media-heading">' + YT.title + '</h4> Author: ' + YT.author_name + '<div class="progress"><div class="progress-bar" id="download-progress-' + video_id + '" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download', {
url: $('#url').val(),
id: video_id
});
})
}
console.log(playlist_id + ' id:' + video_id)
request
.get('https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list='+ playlist_id+'&format=json', function(err,httpResponse,body){
var YT = JSON.parse(body);
$( '<div class="media" ><div class="media-left"><a href="#"><img id="img-'+video_id+'" width="128" class="media-object" src="'+YT.thumbnail_url +'" alt="..."></a></div><div class="media-body" id="body-'+video_id+'"><h4 class="media-heading">'+YT.title+'</h4> Playlistauthor: '+YT.author_name+'<div class="progress"><div class="progress-bar" id="download-progress-'+video_id+'" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download-pl', {url:$('#url').val(),id:video_id,folder:YT.author_name+'-'+YT.title});
})
}else{
request
.get('https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='+ video_id+'&format=json', function(err,httpResponse,body){
var YT = JSON.parse(body);
$( '<div class="media" ><div class="media-left"><a href="#"><img id="img-'+video_id+'" width="128" class="media-object" src="'+YT.thumbnail_url +'" alt="..."></a></div><div class="media-body" id="body-'+video_id+'"><h4 class="media-heading">'+YT.title+'</h4> Author: '+YT.author_name+'<div class="progress"><div class="progress-bar" id="download-progress-'+video_id+'" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download', {url:$('#url').val(),id:video_id});
})
} }
console.log(playlist_id+' id:'+video_id)
}) })
}) })

View File

@ -0,0 +1,34 @@
/*
JUST TO WATCH OFFLINE
*/
const fs = require('fs');
var crunch = {};
crunch.session_save = (session)=>{
var net = `# Netscape HTTP Cookie File\n
# http://curl.haxx.se/rfc/cookie_spec.html\n
# This is a generated file! Do not edit.\n\n`;
var old_exp = 0;
for(var i = 0;i<session.length;i++){
if(typeof session[i].expirationDate == 'undefined'){
session[i].expirationDate = old_exp;
}
if(session[i].expirationDate>old_exp){
old_exp = session[i].expirationDate;
}
net += session[i].domain+' '+session[i].httpOnly.toString().toUpperCase()+' '+session[i].path+' '+session[i].secure.toString().toUpperCase()+' '+Math.ceil(session[i].expirationDate)+' '+session[i].name+' '+session[i].value+'\n';
}
console.log(net);
fs.writeFile(global.dir+"/tmp/c", net, function(err) {
if(err) {
return console.log(err);
}
console.log("T!");
});
}
module.exports = crunch;

View File

@ -5,6 +5,8 @@ const {
session session
} = require('electron'); } = require('electron');
const crunch = require('./../crunchyroll')
class browser { class browser {
constructor(url){ constructor(url){
console.log("TEST") console.log("TEST")
@ -17,7 +19,8 @@ class browser {
}) })
setInterval(function(){ setInterval(function(){
session.defaultSession.cookies.get({url:'http://crunchyroll.com'}, (error, cookies) => { session.defaultSession.cookies.get({url:'http://crunchyroll.com'}, (error, cookies) => {
console.log(error, cookies) //console.log(error, cookies)
crunch.session_save(cookies);
}) })
},3000) },3000)
var window = this.window var window = this.window

View File

@ -1,5 +1,6 @@
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const ipcMain = require('electron').ipcMain; const ipcMain = require('electron').ipcMain;
const dl_routine = require('./youtube-dl.routine')
console.log(global.dir); console.log(global.dir);
var orig_path = ""; var orig_path = "";
@ -54,16 +55,9 @@ var yt_dl = class{
var log = []; var log = [];
var me = this; var me = this;
var ls; var ls;
switch (format) {
case "mp3":
ls = spawn(global.dir+'/lib/youtube-dl', ['-x','--audio-format','mp3','-i',this.url,'-o',path+'/%(title)s.%(ext)s']);
break;
case "mp4":
ls = spawn(global.dir+'/lib/youtube-dl', ['-f','bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',this.url,'-o',path+'/%(title)s.%(ext)s']);
break;
default:
} var m = dl_routine(path,this.url,format);
ls = spawn(m[0],m[1]);
if(path == ''){ if(path == ''){
//ls = spawn(global.dir+'/lib/youtube-dl', ['-x','--audio-format','mp3','-i',this.url]); //ls = spawn(global.dir+'/lib/youtube-dl', ['-x','--audio-format','mp3','-i',this.url]);
}else{ }else{

View File

@ -0,0 +1,31 @@
module.exports = (path,url,format)=>{
var command = global.dir+'/lib/youtube-dl';
var param = [];
console.log(url);
if(url.match('youtube')){
console.log('youtube')
switch (format) {
case "mp3":
param = ['-x','--audio-format','mp3','-i',url,'-o',path+'/%(title)s.%(ext)s'];
break;
case "mp4":
param = ['-f','bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',url,'-o',path+'/%(title)s.%(ext)s'];
break;
default:
}
}
else if(url.match('crunchyroll')){
console.log('crunchyroll')
param = ['--cookie', global.dir+'/tmp/c', '--all-subs', '--sub-format', 'srt', '--write-sub', '--embed-subs',url,'-o',path+'/%(title)s.%(ext)s'];
}
return [command,param];
}