Created
November 21, 2017 19:48
-
-
Save Riveascore/998036f0efe4dd64a66b1cd2d8ae9afa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get lecture plan for Udemy | |
var finalLecturePlan; | |
finalLecturePlan = getLecturePlan(2); | |
function getLecturePlan(hoursPerDay) { | |
var times, totalTime; | |
times = $('.lecture__item__link__time').map(function (idx, obj) { | |
var $obj, timeStr, regex, execed, minutes, minuteSeconds, seconds, totalSeconds, $parent, lectureNumber; | |
$obj = $(obj); | |
timeStr = $obj.html(); | |
regex = /(\d+):(\d+)/; | |
execed = regex.exec(timeStr); | |
minutes = parseInt(execed[1]); | |
minuteSeconds = minutes * 60; | |
seconds = parseInt(execed[2]); | |
totalSeconds = minuteSeconds + seconds; | |
$parent = $obj.closest('.lecture__item__link'); | |
lectureNumberHTML = $parent.find('.mr5').html().replace(/\./g, ""); | |
lectureNumber = $.trim(lectureNumberHTML); | |
return { | |
totalSeconds: totalSeconds, | |
lectureNumber: lectureNumber | |
}; | |
}); | |
var totalSeconds = 0; | |
for (var i = 0; i < times.length; i++) { | |
totalSeconds += times[i]['totalSeconds'] << 0; | |
} | |
totalTime = totalSeconds.toString().toHHMMSS(); | |
console.log('Total Time:'); | |
console.log(totalTime); | |
var startingIndex, perDay, perDayMax, secondsTotal, startingLecture, endIndex, nextAddition, newTotal, endLecture, newLecturePlan, lecturePlans; | |
lecturePlans = []; | |
startingIndex = 0; | |
perDay = hoursPerDay * 60 * 60; | |
perDayMax = perDay + 15 * 60; | |
while(startingIndex < times.length - 1) { | |
secondsTotal = 0; | |
newTotal = 0; | |
startingLecture = times[startingIndex]['lectureNumber']; | |
endIndex = startingIndex; | |
while(newTotal < perDay) { | |
nextAddition = times[endIndex]['totalSeconds']; | |
newTotal = secondsTotal + nextAddition; | |
if (newTotal > perDayMax) { | |
break; | |
} | |
endIndex++; | |
secondsTotal = newTotal; | |
} | |
endLecture = times[endIndex]['lectureNumber']; | |
newLecturePlan = { | |
startingLecture: startingLecture, | |
endLecture: endLecture | |
} | |
startingIndex = endIndex + 1; | |
lecturePlans.push(newLecturePlan); | |
} | |
return lecturePlans; | |
} | |
String.prototype.toHHMMSS = function () { | |
var sec_num = parseInt(this, 10); // don't forget the second param | |
var hours = Math.floor(sec_num / 3600); | |
var minutes = Math.floor((sec_num - (hours * 3600)) / 60); | |
var seconds = sec_num - (hours * 3600) - (minutes * 60); | |
if (hours < 10) {hours = "0"+hours;} | |
if (minutes < 10) {minutes = "0"+minutes;} | |
if (seconds < 10) {seconds = "0"+seconds;} | |
return hours+':'+minutes+':'+seconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment