Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jtwt Script Modification

Status
Not open for further replies.

dulla

Technical User
Feb 3, 2003
54
0
0
The following code pulls recent tweets from twitter and feeds them to your website.

I'm trying to figure out a way to change the date of the recent tweet from 1 week (default) to 1 month. Any ideas?



(function($){

$.fn.jtwt = function(options) {

//Declare defaults
var defaults = {
username : 'harbor',
query: '',
count : 4,
image_size: 48,
convert_links: true,
loader_text: 'loading new tweets',
no_result: 'No recent tweets found'

}

//Merge default with options
var options = $.extend(defaults, options);


var parseTwitterDate = function(tdate) {

var system_date = new Date(tdate.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
var user_date = new Date();

// JB EDIT
var my_date = new Date(tdate);
return my_date.format("mmmm d");

var diff = Math.floor((user_date - system_date) / 1000);
if (diff <= 1) {return "just now";}
if (diff < 20) {return diff + " seconds ago";}
if (diff < 40) {return "half a minute ago";}
if (diff < 60) {return "less than a minute ago";}
if (diff <= 90) {return "one minute ago";}
if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
if (diff <= 5400) {return "1 hour ago";}
if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
if (diff <= 129600) {return "1 day ago";}
if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
if (diff <= 777600) {return "1 week ago";}
return "on " + system_date;

}

return this.each(function() {

var o = options;
var obj = $(this);
var q;

/*
// JB EDIT
$(obj).append('<ul class="jtwt"></ul>');

$(".jtwt", obj).append('<li class="jtwt_loader jtwt_tweet" style="display:none;">' + o.loader_text + '</li>');
$(".jtwt_loader", obj).fadeIn('slow');
*/

//Check if there is a search query given, if not fetch user tweets
if(o.query) {

q = encodeURIComponent(o.query);

} else {

q = 'from:' + encodeURIComponent(o.username);

}

//get the tweets from the API
$.getJSON(' + q + '&callback=?', function(data){

var results = data['results'];

if(results.length) {

$(obj).html('');

//Loop through results and append them to the parent
for(var i = 0; i < o.count && i < results.length; i++) {

var item = results;

jtweet = '<article>'; // JB EDIT

/*
// JB EDIT
if (o.image_size) {

today = new Date();

jtweet += '<div class="jtwt_picture">';
jtweet += '<a href=" + item.from_user + '">'
jtweet += '<img width="' + o.image_size +'" height="' + o.image_size + '" src="' + item.profile_image_url + '" />';
jtweet += '</a>';
jtweet += '</div>';

}
*/

var tweettext = item.text;
var tweetdate = parseTwitterDate(item.created_at);

if (o.convert_links) {

tweettext = tweettext.replace(/(http\:\/\/[A-Za-z0-9\/\.\?\=\-]*)/g,'<a href="$1">$1</a>');
tweettext = tweettext.replace(/@([A-Za-z0-9\/_]*)/g,'<a href="tweettext = tweettext.replace(/#([A-Za-z0-9\/\.]*)/g,'<a href="
}

/*
// JB EDIT
jtweet += '<p class="jtwt_tweet_text">' + tweettext + '</p>';
jtweet += '<a href=" + item.from_user + '/statuses/' + item.id_str + '" class="jtwt_date">' + tweetdate + '</a>';
*/
jtweet += '<h4><time>' + tweetdate + '</time></h4>';

jtweet += '<p>' + tweettext + '</p>';

jtweet += '</article>'; // JB EDIT

$(obj).append(jtweet); // JB EDIT

}
// JB EDIT
$('#flutter-sidebar').addClass('scroll-pane');
$('.scroll-pane').jScrollPane();

} /*
// JB EDIT
else {

//If there are not any tweets, display the "no results" container
$(".jtwt_loader", obj).fadeOut('fast', function() {

$(".jtwt", obj).append('<li class="jtwt_noresult jtwt_tweet" style="display:none;">' + o.no_result + '</li>');
$(".jtwt_noresult", obj).fadeIn('slow');

});


}*/

$(".jtwt_loader", obj).fadeOut('fast');

});

});

}

})(jQuery);
 
it's not straight forward to retrieve posts AFTER a specific date. you have to make two calls to do this. the first to get the ID of the first post BEFORE a specific date, and the second to get all IDs higher than a specified ID.

it is, however, easy to get a certain number of tweets (up to 100).

to do this change the following line as shown in red

Code:
$.getJSON('[URL unfurl="true"]http://search.twitter.com/search.json?q='[/URL] + q + '&callback=?[red]&count=100[/red]', function(data){

this assumes you are using the current API (which actually may not be true since it seems like the url is pointing at the deprecated API). For the old API use this

Code:
$.getJSON('[URL unfurl="true"]http://search.twitter.com/search.json?q='[/URL] + q + '&callback=?[red]&rpp=100&page=10[/red]', function(data){

the latter will return roughly 1000 tweets.

note that the old API will stop functioning shortly.

btw I think the username should be prepended with an ampersat.
 
Thank you very much for your reply. I am able to change the number of tweets, but they still disappear after 1 week. So I could potentially have 100 or 1000 tweets but they won't last more than 1 week. I've also tried adding:

You could try adding:
Code:

if (diff >= 2592000) {return "1 month ago";}

Here:
Code:

if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
if (diff <= 777600) {return "1 week ago";}
if (diff >= 2592000) {return "1 month ago";}
return "on " + system_date;

And nothing.
 
there is a maximum number of tweets that the API will server. you may be hitting that value.
otherwise there is nothing in the javascript that could alter what twitter is giving you, date wise. from a visibility perspective perhaps there is some css? (although not evident from the above).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top