//var postID = 1745365715708413;
//var tokenString = '?access_token=339414929580032|8QqXpvkTdTXiMtWgG2ukqBnOsmY';
var tokenString = '?access_token=EAADZBU5UCnEsBAPjpAjZBdA2UTMU33EKI0vIWNq9FZBmCjLFifhhtQV9RVI4UsMEjtIEtkptF2ZCQpO7yBVezXjs6h4SfeaGQuIv2qnjZAR4kBjdd7kUbkAZB6zIU7Urtwb5ZBLf6ttKE6UnJ0p0V80MwLqZBqfP8JzJxwE9VohBZAyR5ToZARG4XzhSzco36UgGAZD';
//var tokenString = "null";
var lastTime = (new Date).getTime();
//production
var refreshInterval = 15000;
//var refreshInterval = 5000;
var preloadPostIDFound = false;
var inError = false;
var postID;
var pageID = null;
var pageToken = null;
var pageName = "Page";
var selectedPost = null;
var dataInterval = null;
var numLikes;
var numLoves;
var numWow;
var numHaHa;
var numAngry;
var numSad;
var numThankful;
var totalCommentCount = 0;
var postArray = new Array();
var cleanPostArray = new Array();
var currentPostName = "";
function buildPageNavOLD()
{
var theHTML = "";//'Select the Facebook Page ';
theHTML += '
Select the Facebook Page
";
$("#menuNav").html(theHTML);
$(".todoitem").click(function(e) {
console.log( $(this).attr('data-label'));
$(this).addClass('btn-success');
buildSelectedPageMessage();
$("#menuNav").html(buildSelectedPageMessage($(this).attr('data-label')));
});
$("#theTable").html("");
$("#postNav").html("");
resetPage();
}
function reloadpagePosts()
{
postID = null;
selectedPost = null;
//buildSelectedPageMessage
buildBreadcrumb("posts");
setPage(pageID);
}
function buildSelectedPageMessage(thePage)
{
pageName = thePage;
buildBreadcrumb("posts");
$("#postTitle").text(pageName);
//$("#pageBreadcrumb").text(thePage);
//console.log("set the page message "+thePage);
//console.log("set the page pageid ");
//console.log(pageID);
var theMessage = "";
//theMessage = 'Page: '+thePage+"
";
//theMessage += " ";
//theMessage += 'Post: None selected
';
//theMessage += " ";
theMessage += ' Waiting for a live video post to begin on the '+pageName+' Facebook page...
';
theMessage += 'Current Posts
';
return theMessage;
}
// function setSelectedPage(pageTitle)
// {
// var theHTML = 'DelmarvaLife ';
// theHTML += 'WBOC ';
// theHTML += 'Instant Articles Test ';
//
// $("#menuNav").html(theHTML);
//
// }
function buildPostNav()
{
var theHTML = "";
var dataArray = getPostArray();
//console.log(dataArray);
$.each(dataArray, function(index, value) {
var theTitle = "";
if (value.from.id == thePageID) {
if(value.message)
{
theTitle = value.message.substring(0, 80);
}
else
{
theTitle = value.name.substring(0, 80);
}
//theDropDown = theDropDown + '' + theTitle + ' ';
theHTML =+ 'theTitle ';
}
});
}
function setPage(id, token)
{
//var att = $(this).attr("data-id");
//console.log("setPageObject");
//console.log(id);
//console.log(token);
//displayMessage("Select a post to view comments");
clearCommentCount();
// fetch posts
pageID = id;
pageToken = token;
//tokenString = '?access_token=' + token;
console.log("set pageid " + id);
setCookie("pageid", id);
getPostListData();
}
function setPostLink(id)
{
//console.log("setPost "+id);
//theID = $("#postSelector").val();
setPostID(id);
setCookie("postid", id);
console.log("set post id "+id);
loadComments(id);
$("#menuNav").html("");
$("#postNav").html("");
}
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function initPage() {
var pageCookie = getCookie("pageid");
buildBreadcrumb("home");
setInterval(returnCurrentTime, 1000);
//pageID = 319034668130985;
//instant articles
//pageID = 1721581924753459;
//loadComments();
//displayMessage("Select a page then a post to view comments...");
//displayErrorMessage("init");
buildPageNav();
//handles the post selector
$("#postSelector").on('change', function(eve) {
console.log($("#postSelector").val());
var theIndex = eve.target.selectedIndex;
var theLabel = eve.currentTarget.children[theIndex].innerHTML;
var newLabel = theLabel.substring(0, 40);
$("#selectedTitle").html(newLabel);
//var theID = $("#pageSelector").val();
//set the id forthe post
theID = $("#postSelector").val();
setPostID(theID);
console.log("set post id "+theID);
loadComments(theID);
});
$("#pageThing").on('change', function(eve) {
displayMessage("Select a post to view comments");
clearCommentCount();
// fetch posts
pageID = $("#pageThing").val();
console.log("set pageid " + pageID);
setCookie("pageid", pageID);
getPostListData();
});
setReloadInterval();
getPostListData();
preloadPageFromQueryString();
}
function preloadPageFromQueryString()
{
var preloadpageid = getParameterByName("preloadpage");
if(preloadpageid != null)
{
setPage(preloadpageid);
$("#menuNav").html(buildSelectedPageMessage("DelmarvaLife"));
}
}
function resetPage()
{
cleanPostArray = new Array();
selectedPost = null;
preloadPostIDFound = false;
inError = false;
postID = null;
pageID = null;
dataInterval = null;
totalCommentCount = 0;
postArray = new Array();
clearReactions();
clearCommentCount();
$("#postTitle").html("");
buildBreadcrumb("home");
}
function setPostID(id)
{
//console.log("setPostID " + id);
postID = id;
var postName = getPostName(id);
currentPostName = postName;
postTitle = postName.substring(0, 80)+"...";
//$("#postTitle").text( postName.substring(0, 120)+"...");
//$("#postTitle").text(pageName);
buildBreadcrumb("selectedpost");
//$("#postBreadcrumb").text( postName.substring(0, 80)+"...");
}
function getPostID()
{
//console.log("getPostID " + postID);
return postID;
}
function doTest() {
var theID = 319034668130985;
var tokenString = '?access_token=339414929580032|8QqXpvkTdTXiMtWgG2ukqBnOsmY';
var fieldString = "";
FB.api(
'/' + theID + '/feed' + '?access_token=' + token,
'GET', {
order: 'reverse_chronological',
limit: 20
},
function(response) {
}
);
//dataInterval = setInterval(loadComments, refreshInterval);
}
function getPostReactions(postID) {
//var theID = 319034668130985;
numLikes = 0;
numLoves = 0;
numWow = 0;
numHaHa = 0;
numAngry = 0;
numSad = 0;
numThankful = 0;
try {
showActivity();
FB.api(
'/' + postID + '/reactions' + tokenString,
'GET', {},
function(response) {
hideActivity();
$.each(response.data, function(index, value) {
switch (value.type) {
case "LIKE":
numLikes++;
break;
case "LOVE":
numLoves++;
break;
case "THANKFUL":
numThankful++;
break;
case "HAHA":
numHaHa++;
break;
case "SAD":
numSad++;
break;
case "WOW":
numWow++;
break;
case "ANGRY":
numAngry++;
break;
} //console.log(value.type);
//theDropDown = theDropDown + '' + value.message.substring(0, 70) + ' ';
// var selectedID = "'" + value.id + "'";
//theDropDown = theDropDown + '' + value.message.substring(0, 700) + ' ';
});
//console.log("got reactions");
$('#numLikes').text(numLikes);
setOpacity('numLikes', numLikes);
$('#numLoves').text(numLoves);
setOpacity('numLoves', numLoves);
$('#numHaHa').text(numHaHa);
setOpacity('numHaHa', numHaHa);
$('#numWow').text(numWow);
setOpacity('numWow', numWow);
$('#numAngry').text(numAngry);
setOpacity('numAngry', numAngry);
$('#numSad').text(numSad);
setOpacity('numSad', numSad);
}
);
} catch (e) {
displayErrorMessage("Error loading content...");
} finally {
}
}
function clearReactions() {
$('#numLikes').text("0");
setOpacity('numLikes', 0);
$('#numLoves').text("0");
setOpacity('numLoves', 0);
$('#numHaHa').text("0");
setOpacity('numHaHa', 0);
$('#numWow').text("0");
setOpacity('numWow', 0);
$('#numAngry').text("0");
setOpacity('numAngry', 0);
$('#numSad').text("0");
setOpacity('numSad', 0);
}
function setOpacity(theID, itemCount) {
var theCount = Number(itemCount);
//console.log("setOpacity: " + theCount);
if (theCount > 0) {
//numLikesIcon
$("#" + theID + "Icon").fadeTo("slow", 0.9);
$("#" + theID).fadeTo("slow", 0.9);
//$("#" + theID).css('background-color', 'red');
//$("#" + theID).css("cssText", "background-color: gray !red;");
} else {
$("#" + theID + "Icon").fadeTo("slow", 0.2);
$("#" + theID).fadeTo("slow", 0.2);
//$("#" + theID).css("filter", "grayscale(100%);");
//$("#" + theID).css("-webkit-filter", "grayscale(100%);");
//filter: grayscale(100%);
//$("#" + theID).css('background-color', 'gray');
//$("#" + theID).css("cssText", "background-color: gray !important;");
//css('background-color', 'red')
//background-color: gray;
}
}
function preloadVideoPost(post)
{
if(preloadPostIDFound == false)
{
//console.log(post);
if(isRecentLivePost(post.created_time))
{
//flag that we have the most recent video stream post
preloadPostIDFound = true;
playConnected();
//set the id
setPostID(post.id);
//force a load of the comments so we dont have to wait for the timer
loadComments(post.id);
//clear the nav & menu list
$("#postNav").html("");
$("#menuNav").html("");
}
}
}
function setPostArray(data)
{
postArray = data;
//console.log(postArray);
}
function getPostArray()
{
return postArray;
}
function getStringValue(object, property)
{
var cleanString = null;
if(object.hasOwnProperty(property)){
cleanString = object[property];
//console.log(object[property]);
}
return cleanString;
}
function buildPostItem(postItem)
{
var cleanPost = {
title: "-",
name: null,
message: null,
id: null,
admin_creator: null,
from: null,
source: null,
created_time: null,
islive: null
}
//we have a page id, lets do it
if (postItem.from.id == pageID) {
cleanPost.id = getStringValue(postItem, "id");
cleanPost.name = getStringValue(postItem, "name");
cleanPost.message = getStringValue(postItem, "message");
cleanPost.created_time = getStringValue(postItem, "created_time");
cleanPost.title = "";
cleanPost.full_title = "";
if(cleanPost.message)
{
cleanPost.full_title = cleanPost.message;
cleanPost.title = cleanPost.message.substring(0, 100);
}
else
{
if(cleanPost.name)
{
cleanPost.full_title = cleanPost.name;
cleanPost.title = cleanPost.name.substring(0, 100);
}
}
//console.log(cleanPost.title);
var thing = cleanPost.title;
cleanPost.title = thing.replace(/:?[a-z0-9]+;/g, "");
cleanPost.source = getStringValue(postItem, "source");
if(getStringValue(postItem, "source"))
{
if(postItem.source.indexOf("m3u8") > 0)
{
cleanPost.islive = true;
console.log("its live!");
}
}
}
cleanPostArray.push("cleanPostArray", cleanPost);
return cleanPost;
}
function watchForNewLive()
{
var returnVal = "";
//console.log(cleanPostArray);
$.each(cleanPostArray, function(index, value) {
if(value.islive == true )
{
preloadVideoPost(value);
}
});
}
function getPostName(id)
{
var returnVal = "hmm";
//console.log(cleanPostArray);
$.each(cleanPostArray, function(index, value) {
if(value.id == id )
{
returnVal = value.full_title;
}
});
return returnVal;
}
function getPostListData() {
//Delmarvalife
//var thePageID = 319034668130985;
//other page
var pageCookie = getCookie("pageid");
// console.log("pageCookie: " + pageCookie);
var thePageID = pageID; //168146169872122;
console.log(" load posts: " + thePageID);
//wboc
//thePageID = 219252434538;
//219252434538
var fieldFilter = "&fields=admin_creator,id,name,message,from,source,created_time";
console.log(thePageID + "/feed" + tokenString + fieldFilter);
showActivity();
//console.log(pageToken);
FB.api(
'/' + thePageID + '/feed' + tokenString + fieldFilter,
'GET', {
order: 'reverse_chronological',
limit: 20
},
function(response) {
hideActivity();
// console.log("post response");
// console.log(response.data);
var postCounter = 0;
// var theDropDown = 'Select Post ';
var postListNav = '';
//console.log(response.data);
$.each(response.data, function(index, value) {
//suppress posts from users, we only want posts from the main page!!!!!!!
try {
var theTitle = "";
var theIcon = "";
var extraClass="";
var cleanPost = buildPostItem(value);
if(cleanPost.islive == true)
{
theIcon = '
Live ';
//extraClass = " list-group-item-warning";
console.log("live "+theTitle);
preloadVideoPost(cleanPost);
// theDropDown = theDropDown + '
' + theTitle + ' ';
}
else
{
theIcon = '';
// theDropDown = theDropDown + '
' + theTitle + ' ';
}
var theURL = " onClick=setPostLink('"+ cleanPost.id.toString()+"')";
// postListNav = postListNav + '
'+theTitle+' ';
//
Dapibus ac facilisis in
postCounter++;
// var theListIcon = '';//
';
// postListNav = postListNav + '';
//
var theListIcon = '';//
';
postListNav = postListNav + '';
// console.log("updated post list");
} catch (e) {
console.log(e);
//$("#tableData").html(e);
} finally {
}
//theDropDown = theDropDown + '
' + value.message.substring(0, 100) + ' ';
});
// console.log("posts: " + postCounter);
//theDropDown = theDropDown + '';
// postListNav = postListNav + "
";
if(preloadPostIDFound == false)
{
$("#postNav").html(postListNav);
}
}
);
//$("#postNav").html(postListNav);
//console.log("clean array");
//console.log(cleanPostArray);
//setReloadInterval();
//dataInterval = setInterval(loadComments, refreshInterval);
//buildPostNav();
}
function setReloadInterval() {
dataInterval = setInterval(timerFired, refreshInterval);
console.log("set load comment interval: " + refreshInterval);
}
function timerFired() {
//var temp = getPostArray();
//console.log(temp);
console.log("timer fired");
//console.log(" id" +postID);
var postIDCookie = getCookie("postid");
console.log("post cookie: " + postIDCookie);
loadComments(postIDCookie);
showActivity();
}
function forceReloadComments()
{
var postIDCookie = getCookie("postid");
console.log("FORCE RELOAD post cookie: " + postIDCookie);
loadComments(postIDCookie);
showActivity();
}
function showActivity()
{
$("#dataActivity").html('Loading... ');
}
function hideActivity()
{
$("#dataActivity").html('');
}
///{{POST_ID}}/?fields=comments{comments}&access_token={{ACCESS_TOKEN}}
//post-id/comments?fields=id,message... [other fields you want] ...,comments{from,message,id... }
//1745365715708413/comments?fields=id,message,comments{from,message,id}
function clearCommentCount() {
$("#commentCount").text("-");
clearReactions();
}
function loadComments(postID) {
try {
//console.log("passed postid "+postID);
//displayDebugMessage("loading comments");
var tempID = getPostID();
//console.log("id retrieved from var: " + tempID);
// var theID = $("#pageSelector").val();
var theID = tempID;
// setCookie("postid", theID);
//console.log("loadComments fired: " + theID);
//Video1.play();
if (theID != "null") {
//console.log("load comments: " + theID);
//displayErrorMessage("loading comments...");
if (theID) {
$("#blurMaker").focus();
getPostReactions(theID);
showActivity();
FB.api(
'/' + theID + '/comments' + tokenString + '&fields=id,message,comments{message,id,comments,from,created_time},comment_count,from,created_time',
'GET', {
order: 'reverse_chronological',
limit: 20
},
function(response) {
//console.log(response);
// Insert your code here
//makeMediaObject(response);
console.log("update");
hideActivity();
//updateDisplayTime(convertTime(Date()));
try {
if (response.data && response.data.length > 0) {
if (haveNewComments(response)) {
//console.log("new comments " + totalCommentCount);
//console.log("new data");
playAlert();
buildTableData(response);
} else {
//playAlert();
//buildTableData(response);
console.log("no new comments");
updateTableDataTimes(response);
}
// if (lastID != response.data[0].id) {
// console.log("new data");
// playAlert();
// buildTableData(response);
// } else {
// //buildTableData(response);
// updateTableDataTimes(response);
// }
} else {
try {
if (response.error) {
inError = true;
displayErrorMessage("Unable to load comments, will retry.");
} else {
//var theInfo = 'Live Video Post Detected:
';
//var currentInfo = $('#theTable').html();
//$('#theTable').html(theInfo +currentInfo);
noCommentsMessage("No comments yet", ""+currentPostName+" ");
clearCommentCount();
}
} catch (e) {
} finally {
}
//console.log(response.error);
}
} catch (e) {
displayErrorMessage("Error loading reactions...");
//console.log(response);
console.log(e);
} finally {
//console.log("finally");
}
}
);
} else {
console.log("Load comments: no post id set");
//loop through our list of posts and see if we have a live one to preset!!!!
getPostListData();
}
} else {
console.log("do it");
getPostListData();
}
} catch (e) {
displayErrorMessage("Error loading comments...");
} finally {
}
}
var auth_status_change_callback = function(response) {
console.log("auth_status_change_callback: " + response.status);
if (response.status == "connected") {
console.log("pi connected OK");
//setInterval(loadComments, 5000);
//loadComments();
//doTest();
//initPage();
}
}
function dologin() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
} else {
FB.login();
}
});
}
function displayDebugMessage(message) {
var currentTime = new Date();
var currentMS = (currentTime).getTime();
var currentHour = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
// var theContent = '' + currentHour + ":" + currentMinutes + ":" + currentSeconds + " " + message + '
';
var theContent = '' + doTheTime() + " " + message + '
';
//formatTimeDisplay();
$('#debugMessage').html(theContent);
}
function displayErrorMessage(message) {
var originalContent = $('#theTable').html();
var theContent = '' + message + '
' + originalContent;
$('#theTable').html(theContent);
}
function displayMessage(message) {
var theContent = '' + message + '
';
$('#theTable').html(theContent);
}
function noCommentsMessage(message, headertext) {
var theContent = headertext + '' + message + '
';
$('#theTable').html(theContent);
}
function updateDisplayTime(thetime) {
$("#updatedTime").text(thetime);
}
function playAlert() {
var audio = new Audio('ding.mp3');
audio.play();
//Video1.play();
}
function playConnected() {
// var theInfo = 'Live Video Post Detected:
';
// var currentInfo = $('#theTable').html();
// $('#theTable').html(theInfo +currentInfo);
var audio = new Audio('chime-connect.mp3');
audio.play();
}
function updateCommentTime(theComment) {
//build the current updated time
var createdTime = theComment.created_time;
var displayTime = convertTime(createdTime, true);
var thetimeID = "#updatedTimeID" + theComment.id;
$(thetimeID).html(displayTime);
}
function renderReply(theComment) {
//console.log(theComment);
var theCommentHTML = "";
var displayName = theComment.from.name;
var userMessage = theComment.message;
var createdTime = theComment.created_time;
getThumbnailID(theComment.from.id, theComment.id);
var milliseconds = (new Date(createdTime)).getTime();
if (milliseconds > lastTime) {
theCommentHTML = '
' + displayName + '
'; theCommentHTML = theCommentHTML + userMessage; theCommentHTML = theCommentHTML + "" + convertTime(createdTime, true) + ""; theCommentHTML = theCommentHTML + '
' + displayName + '
'; theCommentHTML = theCommentHTML + userMessage; theCommentHTML = theCommentHTML + "" + convertTime(createdTime, true) + ""; try { if (theComment.comments.data) { //console.log(theComment.comments.data); $.each(theComment.comments.data, function(index, value) { //console.log(value); // theCommentHTML = theCommentHTML + renderReply(value); // }); } } catch (e) { } finally { } theCommentHTML = theCommentHTML + '
'; theContent = theContent + convertTime(createdTime); theContent = theContent + '
' theContent = theContent + "" + displayName; theContent = theContent + "
"; theContent = theContent + '' + userMessage + ""; theContent = theContent + "