1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 19:42:38 +02:00

Make sure you will get the video view/progress on the next request

This commit is contained in:
Daniel 2021-09-14 13:26:13 -03:00
parent b6e9349bdf
commit dd26f24b75

View file

@ -437,21 +437,15 @@ var videoViewAdded = false;
var addViewBeaconTimeout;
function addView(videos_id, currentTime) {
addViewSetCookie(PHPSESSID, videos_id, currentTime, seconds_watching_video);
if (last_videos_id == videos_id && last_currentTime == currentTime) {
return false;
}
if (currentTime > 5 && currentTime % 30 !== 0) { // only update each 30 seconds
if (currentTime > 5 && currentTime % 5 !== 0) { // only update each 30 seconds
return false;
}
if (videoViewAdded && videoViewAdded == videos_id) {
clearTimeout(addViewBeaconTimeout);
addViewBeaconTimeout = setTimeout(function () {
addViewBeacon();
} // update the time watched
, 500);
} else {
if (!videoViewAdded || videoViewAdded !== videos_id) {
videoViewAdded = videos_id;
last_videos_id = videos_id;
last_currentTime = currentTime;
@ -460,38 +454,8 @@ function addView(videos_id, currentTime) {
return true;
}
function addViewBeacon() {
console.log('addViewBeacon');
if (typeof mediaId !== 'undefined' && typeof playerCurrentTime !== 'undefined' && typeof seconds_watching_video !== 'undefined') {
if (seconds_watching_video <= 0) {
console.log('addViewBeacon seconds_watching_video <= 0 ', seconds_watching_video);
return false;
}
var url = webSiteRootURL + 'objects/videoAddViewCount.json.php';
url = addGetParam(url, 'PHPSESSID', PHPSESSID);
url = addGetParam(url, 'id', mediaId);
url = addGetParam(url, 'currentTime', playerCurrentTime);
url = addGetParam(url, 'seconds_watching_video', seconds_watching_video);
console.log('addViewBeacon will be sent', mediaId, playerCurrentTime, seconds_watching_video, beacon);
seconds_watching_video = 0;
var beacon = new Image();
beacon.src = url;
} else {
if (typeof mediaId !== 'undefined') {
console.log('addViewBeacon mediaId is undefined');
}
if (typeof playerCurrentTime !== 'undefined') {
console.log('addViewBeacon playerCurrentTime is undefined');
}
if (typeof seconds_watching_video !== 'undefined') {
console.log('addViewBeacon seconds_watching_video is undefined');
}
}
return '';
}
function _addView(videos_id, currentTime) {
if(typeof PHPSESSID == 'undefined'){
if (typeof PHPSESSID == 'undefined') {
PHPSESSID = '';
}
var url = webSiteRootURL + 'objects/videoAddViewCount.json.php';
@ -511,10 +475,10 @@ function _addView(videos_id, currentTime) {
var _addViewAsyncSent = false;
function _addViewAsync() {
if(_addViewAsyncSent || typeof webSiteRootURL == 'undefined' || typeof player == 'undefined'){
if (_addViewAsyncSent || typeof webSiteRootURL == 'undefined' || typeof player == 'undefined') {
return false;
}
if(typeof PHPSESSID == 'undefined'){
if (typeof PHPSESSID == 'undefined') {
PHPSESSID = '';
}
var url = webSiteRootURL + 'objects/videoAddViewCount.json.php';
@ -531,11 +495,65 @@ function _addViewAsync() {
async: false,
success: function (response) {
console.log('_addViewAsync', response);
setTimeout(function(){_addViewAsyncSent=false;},2000);
setTimeout(function () {
_addViewAsyncSent = false;
}, 2000);
}
});
}
function addViewFromCookie() {
if (typeof webSiteRootURL == 'undefined') {
return false;
}
var addView_PHPSESSID = Cookies.get('addView_PHPSESSID');
var addView_videos_id = Cookies.get('addView_videos_id');
var addView_playerCurrentTime = Cookies.get('addView_playerCurrentTime');
var addView_seconds_watching_video = Cookies.get('addView_seconds_watching_video');
if (!addView_PHPSESSID || addView_PHPSESSID === 'false' ||
!addView_videos_id || addView_videos_id === 'false' ||
!addView_playerCurrentTime || addView_playerCurrentTime === 'false' ||
!addView_seconds_watching_video || addView_seconds_watching_video === 'false') {
return false;
}
addViewSetCookie(false, false, false, false);
var url = webSiteRootURL + 'objects/videoAddViewCount.json.php';
url = addGetParam(url, 'PHPSESSID', addView_PHPSESSID);
$.ajax({
url: url,
method: 'POST',
data: {
'id': addView_videos_id,
'currentTime': addView_playerCurrentTime,
'seconds_watching_video': addView_seconds_watching_video
},
async: false,
success: function (response) {
console.log('addViewFromCookie', response);
}
});
}
function addViewSetCookie(PHPSESSID, videos_id, playerCurrentTime, seconds_watching_video) {
Cookies.set('addView_PHPSESSID', PHPSESSID, {
path: '/',
expires: 1
});
Cookies.set('addView_videos_id', videos_id, {
path: '/',
expires: 1
});
Cookies.set('addView_playerCurrentTime', playerCurrentTime, {
path: '/',
expires: 1
});
Cookies.set('addView_seconds_watching_video', seconds_watching_video, {
path: '/',
expires: 1
});
}
function getPlayerButtonIndex(name) {
var children = player.getChild('controlBar').children();
for (i = 0; i < children.length; i++) {
@ -642,22 +660,22 @@ function playerPlay(currentTime) {
}
}
}).catch(function (error) {
if (player.networkState() === 3 && promisePlaytryNetworkFail<5) {
if (player.networkState() === 3 && promisePlaytryNetworkFail < 5) {
promisePlaytry = 20;
promisePlaytryNetworkFail++;
console.log("playerPlay: Network error detected, trying again", promisePlaytryNetworkFail);
clearTimeout(promisePlaytryNetworkFailTimeout);
promisePlaytryNetworkFailTimeout = setTimeout(function(){
promisePlaytryNetworkFailTimeout = setTimeout(function () {
player.src(player.currentSources());
userIsControling = false;
tryToPlay(currentTime);
},promisePlaytryNetworkFail*1000);
}, promisePlaytryNetworkFail * 1000);
} else {
if (promisePlaytryNetworkFail>=5) {
if (promisePlaytryNetworkFail >= 5) {
userIsControling = true;
console.log("playerPlay: (promisePlaytryNetworkFail) Autoplay was prevented");
player.pause();
}else if (promisePlaytry <= 10) {
} else if (promisePlaytry <= 10) {
console.log("playerPlay: (" + promisePlaytry + ") Autoplay was prevented, trying to mute and play ***");
tryToPlayMuted(currentTime);
} else {
@ -1016,7 +1034,7 @@ function isAutoplayEnabled() {
//console.log("Cookies.get('autoplay')", Cookies.get('autoplay'));
if (typeof forceautoplay !== 'undefined' && forceautoplay) {
return true;
}else if (isWebRTC()) {
} else if (isWebRTC()) {
console.log("isAutoplayEnabled said No because is WebRTC ");
return false;
} else if (isALiveContent()) {
@ -1336,7 +1354,7 @@ function checkDescriptionArea() {
});
}
$(document).ready(function () {
addViewFromCookie();
checkDescriptionArea();
setInterval(function () {// check for the carousel
checkDescriptionArea();
@ -1874,7 +1892,7 @@ function avideoAjax(url, data) {
});
}
function isPlayerUserActive(){
function isPlayerUserActive() {
return $('#mainVideo').hasClass("vjs-user-active");
}