#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    24
    Rep Power
    0

    Jtwt script modification


    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 say 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('http://search.twitter.com/search.json?q=' + 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[i];

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

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

    today = new Date();

    jtweet += '<div class="jtwt_picture">';
    jtweet += '<a href="http://twitter.com/' + 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="http://twitter.com/$1">@$1</a>');
    tweettext = tweettext.replace(/#([A-Za-z0-9\/\.]*)/g,'<a href="http://twitter.com/search?q=$1">#$1</a>');

    }

    /*
    // JB EDIT
    jtweet += '<p class="jtwt_tweet_text">' + tweettext + '</p>';
    jtweet += '<a href="http://twitter.com/' + 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);
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    You could try adding:
    Code:
    if (diff >= 2592000) {return "1 month ago";}
    Here:
    Code:
    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";}
    if (diff >= 2592000) {return "1 month ago";}
    return "on " + system_date;
    Last edited by Edge360; May 10th, 2013 at 06:32 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    24
    Rep Power
    0
    Thanks for your reply. I tried messing with that too, but had no luck. This is the test page the feed (left) gets displayed on. When a tweet becomes one week old, it disappears. I've popped that new line as well, but it didn't pull older tweets. Maybe I have to wait and see if they stay or not?

    http://mothlab.com/wp/flutter/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0
    The problem lies in the fact, that the script uses Twitter's search API, which returns tweets only 6-9 days old.

    I tried adding " + 'until' " or 'since' to the call, but the limitation is the search API it seems

IMN logo majestic logo threadwatch logo seochat tools logo