Skip to content

Instantly share code, notes, and snippets.

@charliecm
Last active February 16, 2017 10:20
Show Gist options
  • Save charliecm/5593889 to your computer and use it in GitHub Desktop.
Save charliecm/5593889 to your computer and use it in GitHub Desktop.
Gets user timeline feed from Twitter using API 1.1 and OAuth. Based on codes from http://www.webdevdoor.com/javascript-ajax/custom-twitter-feed-integration-jquery/
/**
* Get Twitter Feed
* Based on codes from http://www.webdevdoor.com/javascript-ajax/custom-twitter-feed-integration-jquery/
* Author: Charlie Chao
*/
$(function() {
var twitterFeed = (function() {
var options = {
user: 'twitter', // Twitter screen name
count: 2, // tweets to display
container: '.twitter-feeds' // DOM container to display tweets
};
var populateFeed = function() {
$.getJSON('twitter/' + options.user + '-tweets.txt?' + Math.random(), getFeedData);
},
getFeedData = function(feeds) {
var i,
feedCount = (feeds.length <= options.count) ? feeds.length : options.count,
feedHTML = '';
for (i = 0; i < feedCount; i++) {
// TODO: organize feed DOM
feedHTML += '<div class="twitter-box">'
+ '<span class="twitter-time">' + relativeTime(feeds[i].created_at).toUpperCase() + '</span><br>'
+ '<p>' + addLinks(feeds[i].text) + '</p>'
+ '</div>';
}
$(options.container).html(feedHTML);
},
addLinks = function(data) {
// add link to all http:// links within tweets
data = data.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
return '<a href="' + url + '" >' + url + '</a>';
});
// add link to @usernames used within tweets
data = data.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
return '<a href="http://twitter.com/' + reply.substring(1) + '" style="font-weight:lighter;" >' + reply.charAt(0) + reply.substring(1) + '</a>';
});
return data;
},
relativeTime = function(time) {
var values = time.split(" ");
time = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
var parsed_date = Date.parse(time);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000, 10);
var shortdate = time.substr(4, 2) + " " + time.substr(0, 3);
delta = delta + (relative_to.getTimezoneOffset() * 60);
if ( delta < 60 ) {
return 'a minute ago';
} else if( delta < 120 ) {
return 'a minute ago';
} else if( delta < (60 * 60) ) {
return (parseInt(delta / 60), 10).toString() + ' minutes ago';
} else if( delta < (120 * 60) ) {
return 'an hour ago';
} else if( delta < (24 * 60 * 60) ) {
return (parseInt(delta / 3600), 10).toString() + ' hours ago';
} else if( delta < (48 * 60 * 60) ) {
//return '1 day';
return shortdate;
} else {
return shortdate;
}
}
return {
populateFeed: populateFeed
};
})();
twitterFeed.populateFeed();
})
<?php
/**
* Get Twitter Feed
* Authenticates and returns the latest tweets from the user timeline.
* Schedule this task to refresh cache.
*
* Based on codes from http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/
* Author: Charlie Chao
*/
session_start();
require_once('twitteroauth.php');
// API keys
$consumerKey = "1234";
$consumerSecret = "1234";
$accessToken = "1234";
$accessTokenSecret = "1234";
// https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
$user = "twitter";
$count = 10;
$excludeReplies = true;
$includeRetweets = true;
$contributorDetails = false;
$trimUser = false;
// get tweets
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$tweets = $connection->get( "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $user . "&count=" . $count . "&excludeReplies=" . $excludeReplies . "&include_rts=" . $includeRetweets . "&contributor_details=" . $contributorDetails . "&trim_user=" . $trimUser);
// echo json_encode($tweets);
// write tweets to cache
$file = $user . "-tweets.txt";
$fh = fopen($file, 'w') or die("Cannot open file.");
fwrite($fh, json_encode($tweets));
fclose($fh);
if (file_exists($file)) {
echo $file . " successfully written (" . round(filesize($file) / 1024) . "KB)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment