From ce19c0a62dfd17fc51db1d190c071b5dd4e83771 Mon Sep 17 00:00:00 2001 From: Faisal Umair Date: Tue, 29 May 2018 16:16:10 +0530 Subject: [PATCH] Choose subtitle language --- assets/js/app.js | 64 ++++++++++++++++++++++++++++++++++++++++++------ index.html | 13 ++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 0b0f3dd6..e208dc4d 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -276,6 +276,7 @@ var skipSubtitles = settingsCached.download.skipSubtitles; var lectureindex = -1; var remaining = response.count; coursedata['totallectures'] = 0; + var availableSubs = []; if(response.results[0]._class=="lecture"){ chapterindex++; @@ -298,7 +299,12 @@ var skipSubtitles = settingsCached.download.skipSubtitles; if(v.asset.asset_type!="Video"&&skipAttachments){ remaining--; if(!remaining){ - initDownload($course,coursedata); + if(Object.keys(availableSubs).length){ + askforSubtile(availableSubs,initDownload,$course,coursedata); + }else{ + initDownload($course,coursedata); + } + } return; } @@ -350,7 +356,11 @@ var skipSubtitles = settingsCached.download.skipSubtitles; } coursedata['chapters'][chapterindex]['lectures'][lectureindex] = {src:src,name:lecturename,quality:videoQuality,type:type}; if(!skipSubtitles&&response.asset.captions.length){ - coursedata['chapters'][chapterindex]['lectures'][lectureindex].caption = response.asset.captions[0].url; + coursedata['chapters'][chapterindex]['lectures'][lectureindex].caption = []; + response.asset.captions.forEach(function(caption){ + caption.video_label in availableSubs ? availableSubs[caption.video_label] = availableSubs[caption.video_label]+1 : availableSubs[caption.video_label] = 1; + coursedata['chapters'][chapterindex]['lectures'][lectureindex].caption[caption.video_label] = caption.url; + }); } if(response.supplementary_assets.length&&!skipAttachments){ coursedata['chapters'][chapterindex]['lectures'][lectureindex]['supplementary_assets'] = []; @@ -371,7 +381,12 @@ var skipSubtitles = settingsCached.download.skipSubtitles; remaining--; coursedata['totallectures']+=1; if(!remaining){ - initDownload($course,coursedata); + if(Object.keys(availableSubs).length){ + askforSubtile(availableSubs,initDownload,$course,coursedata); + }else{ + initDownload($course,coursedata); + } + } } } @@ -381,7 +396,11 @@ var skipSubtitles = settingsCached.download.skipSubtitles; remaining--; coursedata['totallectures']+=1; if(!remaining){ - initDownload($course,coursedata); + if(Object.keys(availableSubs).length){ + askforSubtile(availableSubs,initDownload,$course,coursedata); + }else{ + initDownload($course,coursedata); + } } } } @@ -394,13 +413,21 @@ var skipSubtitles = settingsCached.download.skipSubtitles; remaining--; coursedata['totallectures']+=1; if(!remaining){ - initDownload($course,coursedata); + if(Object.keys(availableSubs).length){ + askforSubtile(availableSubs,initDownload,$course,coursedata); + }else{ + initDownload($course,coursedata); + } } lectureindex++; }else{ remaining--; if(!remaining){ - initDownload($course,coursedata); + if(Object.keys(availableSubs).length){ + askforSubtile(availableSubs,initDownload,$course,coursedata); + }else{ + initDownload($course,coursedata); + } } } @@ -416,7 +443,7 @@ var skipSubtitles = settingsCached.download.skipSubtitles; }); -function initDownload($course,coursedata){ +function initDownload($course,coursedata,subtitle=false){ var $clone = $course.clone(); var $downloads = $('.ui.downloads.section .ui.courses.items'); var $courses = $('.ui.courses.section .ui.courses.items'); @@ -689,7 +716,7 @@ function downloadLecture(chapterindex,lectureindex,num_lectures,chapter_name){ var file = fs.createWriteStream(download_directory+'/'+course_name+'/'+chapter_name+'/'+lecture_name).on('finish', function(){ checkAttachment(); }); - var request = https.get(coursedata['chapters'][chapterindex]['lectures'][lectureindex]['caption'], function(response) { + var request = https.get(coursedata['chapters'][chapterindex]['lectures'][lectureindex]['caption'][subtitle] ? coursedata['chapters'][chapterindex]['lectures'][lectureindex]['caption'][subtitle] : coursedata['chapters'][chapterindex]['lectures'][lectureindex]['caption'][Object.keys(coursedata['chapters'][chapterindex]['lectures'][lectureindex]['caption'])[0]], function(response) { response.pipe(file); }); } @@ -1081,4 +1108,25 @@ function loadDefaults(){ if(!settings.get('general')){ loadDefaults(); +} + +function askforSubtile(availableSubs,initDownload,$course,coursedata){ + var $subtitleModal = $('.ui.subtitle.modal'); + var $subtitleDropdown = $subtitleModal.find('.ui.dropdown'); + var subtitleLanguages = []; + for(var key in availableSubs){ + subtitleLanguages.push({ + name: `${key} ${availableSubs[key]} Lectures`, + value: key + }) + } + $subtitleModal.modal({closable: false}).modal('show'); + $subtitleDropdown.dropdown({ + values: subtitleLanguages, + onChange: function(subtitle){ + $subtitleModal.modal('hide'); + $subtitleDropdown.dropdown({values:[]}); + initDownload($course,coursedata,subtitle); + } + }); } \ No newline at end of file diff --git a/index.html b/index.html index 1b664be7..a3d8d4c7 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,19 @@ +