Funky Twitter Thoughts Widgets - Using Multiple API Calls

Description

This is a pure javascript widget. This widget displays a keyword map of the various kewwords in a twitter user's statuses. The keywords are highlighted by their frequency. The widget uses IFRAME based multi-threading to keep the reponse times down as it makes over 100 Yahoo API calls to the yahoo context API for keyword extraction.

View the Widget



APIs Used
Yahoo Context API
Twitter user timeline API

How It Works
First, the code makes a call to the twitter api for a particular user and gets the first 100 statuses by that user. Next, it makes a call to the yahu context api for each of those statuses (100 calls). After the keywords are extracted it keeps a count of how many times a word is mentioned. The more it's mentioned the bolder the text.

This example uses a form of faux multi-threading using iframes. This is to reduce the amount of time taken to make the 100 api calls. For a tutorial on this multi-threading methodology please see Real Javascript multithreading using iframes

You can download the zipped example here.

This example is completely self contained and should run from your desktop without any issues. In IE you will get a "warning", and you will have to allow blocked content. This is only because you are running it off the desktop.

For completeness, the code is given below.
 1. Yahu_appid="sQV.btjV34GS4QX9sJM5h45PV_suKMOJPEcn3pYRbdgHG840E3wMIrhqbDRrsg";
 2. 
 3. var twittercurrentkeyval;
 4. var twittercurrentalloc;
 5. var twitterthreadlimit;
 6. var Twt_twitterthreadsrunning=0;
 7. 
 8. function Twt_twitterresultclass(){}
 9. var Twt_twitterresults = new Array();
10. var Twt_twitterkeywords = new Array();
11. 
12. var Twt_twitterresultslength=0;
13. var Twt_twitterresultspage=0;
14. var Twt_twitterresultscountonpage=0;
15. var Twt_twittercurrentcount=0;
16. 
17. var twitterreulstset;
18. 
19. var twitterstarttime;
20. 
21. function Twitter_CallUserSearch(user, context, noofthreads){
22.     if(context==null && user==null){
23.         return;
24.     }
25.     else if(user!=null){
26.         twitterstarttime = new Date().getTime();
27.         twitterthreadlimit=(noofthreads==null?10:noofthreads);
28.         for(i=0;i<twitterthreadlimit;i++)
29.             document.write("<iframe id=\"twitterload"+i+"\" style=\"width:0px;height:0px;border:none\"></iframe>");
30. 
31.         document.write("<script type=\"text/javascript\" src=\"http://twitter.com/statuses/user_timeline.json?user_id="+user+"&"+
32.                     "callback=Twitter_ShowResults&count=50\"/>");
33.     }
34.     else{
35.         document.write("<script type=\"text/javascript\" src=\"http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction?"+
36.             "appid="+Yahu_appid+
37.             "&context="+context+
38.             "&output=json"+
39.             "&query="+"celebrity"+
40.             "&callback=parent.Yahu_ContextCallback\"/>");
41.     }
42. 
43. }
44. 
45. function Twitter_RemoveBadChars(s){
46.     s = s.replace(/[^a-zA-Z0-9]/g, " "); 
47.     s = s.replace(/ /g,"+");
48.     return s;    
49. }
50. 
51. function Twitter_ShowResults(rsp){
52.     twitterreulstset=rsp;
53.     Twt_twitterresultslength=rsp.length;
54.     for (i=0;i<rsp.length;i++){
55.         var rsi=rsp[i];
56.         tmpcls = new Twt_twitterresultclass();
57.         tmpcls.text = rsi.text;
58.         tmpcls.date = rsi.created_at;
59.         Twt_twitterresults.push(tmpcls);
60.     }
61.     Twt_CallThreads();
62.                 
63. }
64. var twitterpercentcomplete="0";
65. function Twt_CallThreads(){
66.     var i;
67.     var x = Twt_twitterresultspage*twitterthreadlimit;
68.     twitterpercentcomplete="<h2>"+Math.round(x*100/Twt_twitterresultslength) +" %  please wait</h2>";
69.     if((Twt_twitterresultslength-x)<=0){
70.         Twt_Finished();
71.         return;
72.     }
73.     for(i=0 ; i<twitterthreadlimit && i < (Twt_twitterresultslength - x) ; i++){
74.         tmps = MyGetThisPage()+"?context="+escape(Twitter_RemoveBadChars(Twt_twitterresults[x+i].text));
75.         document.getElementById("twitterload"+i).src=tmps;
76.     }
77.     Twt_twitterresultscountonpage=i;
78. }
79. 
80. function Twt_ThreadCallback(){}
81. 
82. var twitterinterimresult;
83. function Yahu_ContextCallback(rsp){
84.     twitterinterimresult="";
85.     var wordcount;
86.     for(i=0;i<rsp.ResultSet.Result.length;i++){
87.         keyword = rsp.ResultSet.Result[i]
88.         twitterinterimresult+=keyword+" ";
89.         if(Twt_twitterkeywords[keyword]==null){
90.             wordcount=1;
91.             for(oldkeyword in Twt_twitterkeywords){
92.                 if( oldkeyword.indexOf(keyword)>=0 )
93.                     wordcount++;
94.                 else if (keyword.indexOf(oldkeyword)>=0)
95.                     Twt_twitterkeywords[oldkeyword]++;
96.                 else{}    
97.             }
98.             Twt_twitterkeywords[keyword]=wordcount;
99.         }
100.         else
101.             Twt_twitterkeywords[keyword]++;           
102.     }
103.     document.getElementById("twitterresult").innerHTML=twitterpercentcomplete+"  "+twitterinterimresult;
104.     
105.     if(--Twt_twitterresultscountonpage == 0){
106.       Twt_twitterresultspage++;
107.       Twt_CallThreads();
108.     }              
109. }
110. 
111. function Twt_Finished(){
112.     var size;
113.     var text="";
114.     var basesize=11;
115.     for(keyword in Twt_twitterkeywords){
116.         size = Twt_twitterkeywords[keyword]>7?7:Twt_twitterkeywords[keyword];
117.         if(Twt_twitterkeywords[keyword]>1)
118.             text+="<span style=\"font-size:"+(basesize+size*3)+"px\"><b>"+keyword+"</b></span>";
119.         else
120.             text+="<span style=\"font-size:"+basesize+"\">"+keyword+"</span>";
121.         text+="  ";    
122.     }
123.     text+="<br/><br/><span style=\"font-size:"+basesize+"\">("+twitterthreadlimit+" threads, "+(new Date().getTime()-twitterstarttime)+" ms)</span>";
124.     document.getElementById("twitterresult").innerHTML=text;
125. }
126. 
Hide line numbers