Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Ajax call to get FaceBook and Twitter User Data - POC
var tfriendsCount = null;
var tfollowersCount = null;
var tfavouritesCount = null;
var tstatusesCount = null;
var tlistedCount = null;
google.load("visualization", "1", {packages:["corechart"]});
$("#getTweets").bind("click", function() {
var twitterUsername = $("#twitterUsername").val();
var recordRequest = 1;
$.ajax({
url: "https://api.twitter.com/1/statuses/user_timeline/" + twitterUsername + ".json?count=" + recordRequest + "&callback=?",
type: "get",
dataType: "json",
cache: false,
//data: {param1: 'value1'},
success: function(data) {
$("#twitterOutput").empty("<ul />");
$.each(data, function(index, item) {
$("#twitterOutput").append('<li><strong>Profile Image: </strong><img src=' + item.user.profile_image_url_https + '></li>').append('<li><strong>Name: </strong>' + item.user.name + '</li>').append('<li><strong>User Name: </strong>' + item.user.screen_name + '</li>').append('<li><strong>ID: </strong>' + item.user.id + '</li>').append('<li><strong>User Description: </strong>' + item.user.description + '</li>').append('<li><strong>User URL: </strong>' + item.user.url + '</li>').append('<li><strong>Followers Count: </strong>' + item.user.followers_count + '</li>').append('<li><strong>Friends Count: </strong>' + item.user.friends_count + '</li>').append('<li><strong>List(s) Count: </strong>' + item.user.listed_count + '</li>').append('<li><strong>Time Created: </strong>' + item.user.created_at + '</li>').append('<li><strong>Favourites Count: </strong>' + item.user.favourites_count + '</li>').append('<li><strong>Time Zone: </strong>' + item.user.time_zone + '</li>').append('<li><strong>Statuses Count: </strong>' + item.user.statuses_count + '</li>').wrapInner("<ul />");
tfriendsCount = item.user.friends_count;
tfollowersCount = item.user.followers_count;
tfavouritesCount = item.user.favourites_count;
tstatusesCount = item.user.statuses_count;
tlistedCount = item.user.listed_count;
});
console.log(tfriendsCount);
console.log(tfollowersCount);
console.log(tfavouritesCount);
console.log(tstatusesCount);
console.log(tlistedCount);
$("#twitterOutput").append('<li><strong>Recent Tweet(s): </strong>');
$.each(data, function(index, item) {
$("#twitterOutput").append('<li>' + item.text + '</li>');
});
var chart = new google.visualization.PieChart(document.getElementById('twitter-charts'));
chart.draw(google.visualization.arrayToDataTable([
['Friends Count', tfriendsCount],
['Followers Count', tfollowersCount],
['Favourites Count', tfavouritesCount],
['List Count', tlistedCount],
['Statuses Count', tstatusesCount]
]), {title: 'Twitter User Analytics'});
},
error: function() {
//called when there is an error
alert('Something is wrong man! Hate it :@');
}
});
});
$("#getFeeds").bind("click", function () {
var faceBookUsername = $("#faceBookUsername").val();
var fburl = "https://graph.facebook.com/" + faceBookUsername;
$.getJSON(fburl, function (data) {
$("#fbOutput").empty("<ul />");
$("#fbOutput")
.append('<li><strong>ID: </strong>' + data.id + '</li>')
.append('<li><strong>Name: </strong>' + data.name + '</li>')
.append('<li><strong>User Name: </strong>' + data.username + '</li>')
.append('<li><strong>Profile Link: </strong>' + data.link + '</li>')
.append('<li><strong>Gender: </strong>' + data.gender + '</li>')
.append('<li><strong>Locale: </strong>' + data.locale + '</li>')
//Redefinig fburl variable to get FB User photo from different FB API URL
var fburl = "https://graph.facebook.com/"+ faceBookUsername+ "/picture";
$("#fbOutput")
.append('<li><strong>Profile Picture: </strong><img src="https://graph.facebook.com/'+faceBookUsername+'/picture?type=large"/></li>')
.wrapInner("<ul />");
console.log(fburl)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.