Twitter API example

Using someone else's Twitter API proxy to get raw data about some tweets and display some specific pieces of data.

by cambraca

HTML

<ul style="display:none;">loading...</ul>
<pre id="details">loading...</pre>
<hr />
<pre id="rawData">loading...</pre>

JavaScript

var
  searchTerm = 'hello',
  //searchTerm = 'freekashmir',
  numOfTweets = 10,
  $ul = $('ul'),
  $details = $('#details'),
  $rawData = $('#rawData'),
  details = '',
  list = '',
  // Someone else's Twitter API proxy 😉
  url = 'https://api.twitter.com/1.1/search/tweets.json?q=' + searchTerm + '&count=' + numOfTweets;

function jsonPretty(object) {
  return JSON.stringify(object, null, 2);
}

function attributeString(object, attributeName) {
  return attributeName + ': ' + jsonPretty(object[attributeName]) + '\n';
}

$.get(url, function(data) {
  $rawData.text(jsonPretty(data));

  //$.each(data.statuses, function () {
  //  list += '<li>'+ this.text +'</li>';
  //});
  //$ul.html(list);

  $.each(data.statuses, function() {
    details +=
      attributeString(this, 'id') +
      attributeString(this, 'id_str') +
      attributeString(this, 'text') +
      attributeString(this, 'created_at') +
      attributeString(this, 'retweeted') +
      attributeString(this, 'retweet_count') +
      //attributeString(this, 'retweeted_status') +
      '\n';
  });
  $details.text(details);
});