Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,882
    Thanks
    56
    Thanked 539 Times in 536 Posts
    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>

  • Users who have thanked xelawho for this post:

    MeltingDog (06-04-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,882
    Thanks
    56
    Thanked 539 Times in 536 Posts
    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>");


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •