Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-03-2012, 09:56 AM   PM User | #1
MeltingDog
New to the CF scene

 
Join Date: May 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
MeltingDog is an unknown quantity at this point
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!
MeltingDog is offline   Reply With Quote
Old 06-03-2012, 04:44 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is online now   Reply With Quote
Users who have thanked xelawho for this post:
MeltingDog (06-04-2012)
Old 06-04-2012, 02:21 AM   PM User | #3
MeltingDog
New to the CF scene

 
Join Date: May 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
MeltingDog is an unknown quantity at this point
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?
MeltingDog is offline   Reply With Quote
Old 06-04-2012, 03:44 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>");
xelawho is online now   Reply With Quote
Reply

Bookmarks

Tags
api, date created, javascript, twitter

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 AM.


Advertisement
Log in to turn off these ads.