views:

27

answers:

1

Hello. I have this Twitter widget that works really well (and is easily customizable), but I have one issue with it. I only want to post tweets less than two days old, but I can't figure out how to do it. So in the HTML, I have the following:

<ul id="twitter_update_list"></ul>

<script src="/assets/script_twitterwidget.js" type="text/javascript"></script>
<script src="http://twitter.com/statuses/user_timeline/YourTwitterProfile.json?callback=twitterCallback2&amp;amp;count=1" type="text/javascript"></script>

Then the js has:

function relative_time(time_value) {
 var values = time_value.split(" ");
 time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
 var parsed_date = Date.parse(time_value);
 var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
 var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
 delta = delta + (relative_to.getTimezoneOffset() * 60);
 return delta;
}

function absolute_time(created_at) { 
 var date = (created_at).substring(0,11);
 var year = (created_at).substring(26);
 var time = (created_at).substring(10,26);
 var full = date + year + time;
 var full = new Date(Date.parse(full)).toLocaleDateString();
 return full;
}

if (delta < (48*60*60)) {

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'"&gt;'+reply.substring(1)+'&lt;/a&gt;';
    });
    statusHTML.push('<li><a target="_blank" style="font-size:100%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'"&gt;'+absolute_time(twitters[i].created_at)+':'+'&lt;/a&gt;'+' '+'<span>'+status+'</span></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
  }

}

I added the relative time function and the if statement to try to post only recent tweets (it works great without that code but shows whatever the last tweet is, no matter how old it is). However, it doesn't do the trick. Thanks for the help.

A: 

The above was actually pretty close, even though it took me all day to figure it out. I just had to reword the if statement and move it to the right spot. So the first two functions are fine, but the last should be:

function twitterCallback2(twitters) {
    var statusHTML = [];
    for (var i=0; i<twitters.length; i++) {
        if ((relative_time(twitters[i].created_at)) < (3*24*60*60)) {
            var username = twitters[i].user.screen_name;
            var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
                return '<a href="'+url+'">'+url+'</a>';
                }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
                return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'"&gt;'+reply.substring(1)+'&lt;/a&gt;';
                });
                statusHTML.push('<li><a target="_blank" style="font-size:100%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'"&gt;'+absolute_time(twitters[i].created_at)+':'+'&lt;/a&gt;'+' '+'<span>'+status+'</span></li>');
        }
    }
    document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

As you can see, if the tweet time is less than three days ago, it will post to the widget on my site.

Beau