Created
October 21, 2015 09:44
-
-
Save CB9TOIIIA/a45187efc119f13d328d to your computer and use it in GitHub Desktop.
Получить лайки из соц. сетей и общее кол-во по url
This file contains hidden or 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
Всего: <span class="totalsumms"></span> | |
<br /> | |
<br />VK: <span class="vk"></span> | |
<br />OK: <span class="ok"></span> | |
<br />FB: <span class="fb"></span> | |
<br />TW: <span class="tw"></span> | |
<br />GP: <span class="gp"></span> | |
<br />MR: <span class="mr"></span> | |
<script> | |
$(document).ready(function () { | |
// Переменные | |
var url = 'http://htmlacademy.ru'; | |
// Создаём массив | |
var totalCount = new Array(); | |
// Функция округления цифр | |
var roundCount = function (number) { | |
if (number > 999 && number <= 999999) var result = number / 1000 + 'k'; | |
else if (number > 999999) var result = '>1M'; | |
else var result = number; | |
return result; | |
}; | |
summa = function(m) { | |
for(var s = 0, k = m.length; k; s += m[--k]); | |
return s; | |
}; | |
// Функция проверки существования элемента | |
var existCount = function(element) { | |
return ($(element).length > 0); | |
}; | |
// Основная функция вывода счётчиков | |
$.fn.getCount = function() { | |
// VK | |
function getVK() { | |
if (existCount('span.vk')) { | |
$.getJSON('https://vk.com/share.php?act=count&index=1&url=' + encodeURIComponent(url) + '&callback=?', function (response) {}); | |
VK = {}; | |
VK.Share = {}; | |
VK.Share.count = function (index, count) { | |
$('span.vk').text(roundCount(count)); | |
totalCount.push(count); | |
}; | |
} | |
else return; | |
}; | |
// OK | |
function getOK() { | |
if (existCount('span.ok')) { | |
$.getJSON('https://connect.ok.ru/dk?st.cmd=extLike&uid=1&ref=' + encodeURIComponent(url) + '&callback=?', function (response) {}); | |
ODKL = {}; | |
ODKL.updateCount = function (index, count) { | |
$('span.ok').text(roundCount(count)).replace('"', ''); | |
totalCount.push(count); | |
}; | |
}; | |
}; | |
// FB | |
function getFB() { | |
if (existCount('span.fb')) { | |
$.getJSON('http://graph.facebook.com/?id=' + encodeURIComponent(url) + '&callback=?', function (response) { | |
if (response.shares === undefined) { | |
$('span.fb').text('0'); | |
totalCount.push(0); | |
} else { | |
$('span.fb').text(roundCount(response.shares)); | |
totalCount.push(response.shares); | |
}; | |
}); | |
}; | |
}; | |
// MR | |
function getMR() { | |
if (existCount('span.mr')) { | |
$.getJSON('http://connect.mail.ru/share_count?url_list=' + encodeURIComponent(url) + '&callback=1&func=?', function(response) { | |
var url = encodeURIComponent(location.href); | |
for (var url in response) { | |
if (response.hasOwnProperty(url)) { | |
var count = response[url].shares; | |
break; | |
} | |
} | |
if ($.isEmptyObject(response)) { | |
$('span.mr').text('0'); | |
totalCount.push(0); | |
} | |
else { | |
$('span.mr').text(roundCount(count)); | |
totalCount.push(count); | |
}; | |
}); | |
}; | |
}; | |
// Получаем значения всех счётчиков | |
$.when( | |
getVK(), | |
getOK(), | |
getFB(), | |
$.getJSON('http://cdn.api.twitter.com/1/urls/count.json?url=' + encodeURIComponent(url) + '&callback=?', function (response) { | |
$('span.tw').text(roundCount(response.count)); | |
totalCount.push(response.count); | |
}), | |
$.ajax({ | |
type: 'POST', | |
url: 'https://clients6.google.com/rpc', | |
processData: true, | |
contentType: 'application/json', | |
data: JSON.stringify({ | |
'method': 'pos.plusones.get', | |
'id': url, | |
'params': { | |
'nolog': true, | |
'id': url, | |
'source': 'widget', | |
'userId': '@viewer', | |
'groupId': '@self' | |
}, | |
'jsonrpc': '2.0', | |
'key': 'p', | |
'apiVersion': 'v1' | |
}), | |
success: function (response) { | |
$('span.gp').text(roundCount(response.result.metadata.globalCounts.count)); | |
totalCount.push(response.result.metadata.globalCounts.count); | |
} | |
}), | |
getMR() | |
).done(function() { | |
// Выводим в консоль получившийся массив | |
console.log(totalCount); | |
var totalsum = (summa(totalCount)); | |
// console.log(totalsum); | |
$(".totalsumms").html(totalsum); | |
}); | |
}; | |
// Выводим всё | |
$(document).getCount(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment