CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Advice with Twitter API getting date created (http://www.codingforums.com/showthread.php?t=263424)

MeltingDog 06-03-2012 09:56 AM

Advice with Twitter API getting date created
 
Hi all

I have built this Twitter plugin by going through some tutorials. It works great but I would also like to display the date and time the tweet was created as well.

I was hoping someone might be able to help me modify this code:

Any help would be greatly appreciated

Code:

(function($){
//Creating Function called Twitterize
    $.fn.twitterize = function(username,options){
//check to see if the username has been set.
        if (username){
           
            //SET DEFAULT AMOUNT OF TWEETS TO GET
            var defaultSettings = {
                count:'20'
            }                       
          // Finds which default settings have been overwritten by the options object
            // and creates a new object with all the new and untouched settings.
            var settings = $.extend(defaultSettings, options);       
// The URL for the Twitter JSON API.
            var url = "http://twitter.com/status/user_timeline/"+username+".json?count="+settings.count+"&callback=?";         
            //Variable to get around scope problem in callback function.
            var holder = this;
            //Contact Twitter
            $.getJSON(url,           
                              //This function is called when twitter responds with the appropriate information.
                              function(data){               
                                //Step through each tweet.
                                $.each(data, function(i, item) {
                                //place the tweet within a paragraph tag within the main div.
                                holder.append("<p>"+item.text.makeLinks()+"</p>");                   
                });
            });       
        }
else{
            //IF Username paramater has not been set.
            console.debug("jquery plugin twitterize requires a username! Check your parameters");
        }
        //MAKE LINKS
        String.prototype.makeLinks = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(str) {
                    return str.link(str);
                    });
        };

 $(document).ready(function() {
                              //Calling Twitterize - Assigning to Element and User
                              $('#tweets').twitterize('username');
        });
<div id="tweets"></div>

Thanks heaps!

xelawho 06-03-2012 04:44 PM

I don't much see the need for the plugin here when you can just hook straight into the twitter API. I don't much see the need for jQuery, either, but I guess that's a different story...

Code:

<body>

<script type="text/javascript">
$(document).ready(function(){

var username="KimKardashian";
var tweetcount=10;

 $.ajax({        url : "http://twitter.com/statuses/user_timeline/"+username+".json?count="+tweetcount+"&callback=?",
                        dataType : "json",
                        timeout:15000,
                        success : function(data){
          for (i=0; i<data.length; i++)        {
  $("#tweets").append("<p>" + data[i].text.makeLinks() +" ("+data[i].created_at +")</p>");
                                  }
                        },
                        error : function() {
                                alert("Failure!");
                        },

                });
                String.prototype.makeLinks = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(str) {
                    return str.link(str);
                    });
                        }
});                       
</script>
<div id="tweets"></div>
</body>


MeltingDog 06-04-2012 02:21 AM

Thanks,

I managed it by including this line:
holder.append("<p>"+item.created_at+"</p>");

But I think your way is more economical.

By the way, I dont suppose you know a way of displaying Twitter images as well?

xelawho 06-04-2012 03:44 AM

you can see an example of the object returned here

so if you're looking for the user profile pic I'm guessing that would be something like
Code:

$("#tweets").append("<p>" + item.text.makeLinks() +" ("+item.created_at +") <img src="+item.user.profile_image_url+"></img></p>");


All times are GMT +1. The time now is 08:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.