SmexyyWeby

Scripting for Fun and Passion

Fetch Tweets Using jQuery & JSON: TweeteReads


Update: The App right now works best in Mozilla Firefox 7.0+ also sometimes you will get no tweets when using Chrome or other browser. This is not an error but a restriction by twitter on the no of API calls a particular application can make and hence restricted by twitter.

Twitter is growing popular day by day and has already occupied a place in users browsers & smartphones as Apps. The power of the social media apps and other web applications these days is that they allow users to use the app without even the need for visiting the webpage of that app.

I recently had the opportunity to solve the following problem:

Write an HTML page to fetch the top 10 tweets of a user whose Screen Name is entered by the user.

Looks trivial, considering the RSS feed of a twitter user are easily available. You can check the RSS Feed of my tweets at https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=theshubhanshu&count=2

However when writing an HTML page this method of retrieving tweets via RSS won’t work because of the security issues of HTML. The reason is your HTML page will try to do a Cross Site Request [XSS Request] which is not allowed by browsers.

To solve this problem, the web architect community came up with a new format of information exchange between 2 websites called JSON [JavaScript Object Notation] which allows websites to develop APIs to allow other web applications to use their data.

Twitter Supports JSON as one of the formats for data exchange in its API. https://dev.twitter.com/docs/api

To solve the problem stated above. I used Twitter’s JSON API to fetch the latest tweets of any user whose screen_name is given. I used jQuery to publish the tweets on the web page.

function getTweets(){
$("#tweets").html("</pre>
<h1>Loading ...</h1>
<pre>
");
var screen_name = document.getElementsByName("screen_name")[0].value;
$.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?screen_name="+screen_name+"&count=10&callback=?",
 function(data) {
 $("#tweets").html("");
 $.each(data, function(i,item){
 $("

").html(item.text).appendTo("#tweets");
 if ( i == 10 ) return false;
 });
 });
}

The code above uses the jQuery getJSON function to fetch and parse the tweets in JSON format and display them on the webpage.

I used the GET statuses/friends_timeline function of the twitter api.

I added a little bit of styling to the page which rendered the page like this:

TweeteReads

TweeteReads

The full code of the page is

<!DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <style>
  *{
	margin: 0px;
  }
  body{
	font: 20px cursive;
	margin: 0px;
  }
  #header{
  margin: 0px;
	background-color: #9AE4E8;
	border-bottom: solid 2px black;
  }
  p{
	background-color: #9AE4E8;
	padding: 2px;
	margin: 10px auto;
	border: solid 1px black;
	width: 50%;
  }
  #tweets{
	text-align: center;
  }
  input{
	
    border: 5px solid #9AE4E8;
    border-radius: 5px 5px 5px 5px;
    font: 20px cursive;
	margin: auto;
  }
  input[type="text"]{
	text-align: right;
	width: 50%; 
	
  }
  input[type="submit"]{
	background-color: #4DB8FF;
  }
  #inputPanel{
	margin: 50px auto;
	text-align: center;
  }
  </style>
</head>
<body>
<div id="header">
<h1 align="center">TweeteReads</h1>
<h4 align="center">Designed & Coded by <a href="http://twitter.com/theshubhanshu">@TheShubhanshu</a></h4>
</div>
<div id="inputPanel">
<input type="text" name="screen_name" />
<input type="submit" value="Retrieve Tweets" onclick="javascript: getTweets();" />
</div>

<div id="tweets">

</div>

<script>
function getTweets(){
$("#tweets").html("<h1>Loading ...</h1>");
var screen_name = document.getElementsByName("screen_name")[0].value;
$.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?screen_name="+screen_name+"&count=10&callback=?",
  function(data) {
	$("#tweets").html("");
    $.each(data, function(i,item){
      $("<p />").html(item.text).appendTo("#tweets");
      if ( i == 10 ) return false;
    });
  });
}
</script>

</body>
</html>

The full source code can be downloaded from : https://sites.google.com/site/shubhanshumishra/jsonTest.htm
Please feel free to post a comment if you find any difficulty in using the app.
Feel free to share the information and help others.

Reject ALL Requests with 1 Click


Hi everyone, thanks for visiting my last article on Confirm ALL Friend Requests with 1 Click I received a lot of feedback regarding that on twitter. A lot of them asking to also give a code for REJECTing all the requests on Facebook with 1 click.

Thanks Saptak for the idea. Here you are =)

So when you get a lot of friend requests and find it boring to click Not Now on each one of them. Then just create this bookmarklet and use it to REJECT all friend requests with just 1 click. Simple!

Important point: this bookmarklet will reject all the requests on the page including event invites, apps, etc.

I have already given a tutorial on how to make a bookmarklet in your browser. So if you are a newbie to makeing a bookmarklet read How to Invite all Friends Facebook Event

In the above Bookmarklet just change the URL to the following code to get your bookmarklet for confirming all friend requests.

javascript: var field = document.getElementsByName("actions[hide]");for (i = 0; i &lt; field.length; i++)field[i].click() ;

Reject Requests but do confirm the one's you do want to add

Reject Requests but do confirm the one's you do want to add

Now once created. Go to the following page to get all your Friend Requests: http://www.facebook.com/reqs.php and use the bookmarklet to reject all your requests which you do not want. [Don't forget to accept the ones you do want manually before clicking the bookmarklet].

PS: Its unsafe to accept unknown friend requests on facebook as a lot of your private information which you wish only your close friends should know becomes accessible to all your facebook friends. This includes messages, contacts, personal pictures etc.

Please feel free to post a comment if you find any difficulty in using the bookmarklet.
Also Read about how to make the bookmarklet which Confirm ALL Friend Requests with 1 Click
Feel free to share the information and help others.

Confirm ALL Friend Requests with 1 Click


Facebook is growing and so is the about of connections we are making. It happens a lot of time that when people join facebook that they get a lot of friend requests. Its highly suggested that you make friends with only the people you know.

So when you get a lot of friend requests and find it boring to click confirm on each one of them. Then just create this bookmarklet and use it to confirm all friend requests with just 1 click. Simple!

I have already given a tutorial on how to make a bookmarklet in your browser. So if you are a newbie to makeing a bookmarklet read How to Invite all Friends Facebook Event

 

In the above Bookmarklet just change the URL to the following code to get your bookmarklet for confirming all friend requests.

javascript: var field = document.getElementsByName("actions[accept]");for (i = 0; i < field.length; i++)field[i].click() ;

Confirm Requests

Confirm Requests but do Reject the one's you don't want to add

Now once created. Go to the following page to get all your Friend Requests: http://www.facebook.com/reqs.php and use the bookmarklet to confirm all your new friend requests. [Don't forget to reject the ones you don't want manually before clicking the bookmarklet].

PS: Its unsafe to accept unknown friend requests on facebook as a lot of your private information which you wish only your close friends should know becomes accessible to all your facebook friends. This includes messages, contacts, personal pictures etc.

Please feel free to post a comment if you find any difficulty in using the bookmarklet.
Feel free to share the information and help others.

Code Solves EVERYTHING


This blogpost is about the new header image of my blog. I was bored of the old header image of my blog, which didn’t reflect me or my blog by any means. I had set up this blog is a hurry during my exams [I am doing this editing also during my exams, no wonder this is the most productive period for me =P].

Old Blog Header Image

Old Blog Header Image

So today I was just sitting and solving a programming problem after a long time and after being satisfied with a successful execution of the same, this thought came to my mind — “Code solves EVERYTHING”. It tries to depict the general view of any science believing person. Code need not necessarily mean programs but code is a way of conveying small bits of hacks in day to day life which can help us solve a lot of problems.

Anyways more on this topic in days to come. I will present to you the new series of header images.

PS: The colors don’t represent what you are thinking ;-)

Header Image Blue

Header Image Blue

Header Image Red

Header Image Red

Header Image Yellow

Header Image Yellow

Header Image Green

Header Image Green

Header Image Orange

Header Image Orange

Header Image Purple

Header Image Purple

Header Image Turquoise

Header Image Turquoise

So these are the 7 colors which I used for my headers, Blue, Red, Yellow, Green, Orange, Purple &  Turquoise . These colors denote an old school style of thinking and a vintage style.

Feel free to share your opinions & feedback about the new style and also to share this post.

See you all soon.

Invite all Facebook Friends for Event Bookmarklet


Facebook Invite all Friends to events Bookmarklet

Facebook Invite all Friends to events Bookmarklet

Hello Folks,

A lot of time you end up in this situation where you have created a new Facebook event and would want to invite all your friends to attend that event. If you have a lot of friends then its a real trouble clicking on select for all the friends and then sending the invites.

To solve this problem I have created a small bookmarklet which automates the process of selecting all your friends for inviting to an event just with the click of the mouse button.

First of all Create a Bookmarklet in your Browser. I am stating the steps for Chrome Browser.

  1. Right Click on the Bookmarks Bar and Click on Add New Page… [Click Ctrl+Shift+B to open Bookmarks bar if its not there] 

    Create New Bookmarklet

    Create New Bookmarklet

  2. Give a Name to the Bookmarklet. 

    Edit Bookmarklet

    Edit Bookmarklet

  3. In the URL Section enter the following code. 
    javascript: var field = document.getElementsByName("checkableitems[]");for (i = 0; i < field.length; i++)field[i].checked = true ;
  4. Hit OK and your Bookmarklet will show in your Browser Bar.

Following are the steps to invite all your friends to an event. Lets consider the event Youth 2 Business – IIT Kharagpur 

  • Go to the Event Page https://www.facebook.com/event.php?eid=231553940227603
  • Click on +Select Guests to Invite
  • An Invite Friends box will open. Drag the scroll bar to the end of that box. [It will happen slowly as facebook loads all your friends as you scroll to the bottom of the box. Make sure you end only when the box is at the very end of the scroll space as shown in the picture.]

    Instructions for Invite Friends Box

    Instructions for Invite Friends Box

  • Click on the Bookmarklet from your browser bookmark Bar.
  • All your friends will be selected. Now click on Submit and your invites will be sent. 
Please feel free to post a comment if you find any difficulty in using the bookmarklet.
Also if you want you can try using the bookmarklet at our event Youth 2 Business – IIT Kharagpur . Visit the link to try it out. https://www.facebook.com/event.php?eid=231553940227603
Feel free to share the information and help others.