korstudio

AS3 Twitter Streaming API Example

Jun 15th, 2011
2,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**** Code fragments from https://github.com/r/twstreamer ****/
  2.  
  3. import flash.net.URLStream;
  4. import mx.utils.Base64Encoder;
  5. import flash.events.ProgressEvent;
  6. import flash.events.IOErrorEvent;
  7. import flash.net.URLRequest;
  8. import flash.net.URLRequestHeader;
  9. import flash.net.URLRequestMethod;
  10.  
  11.  
  12. //Read @AdmOd timeline
  13.  
  14. var host:String = "stream.twitter.com";
  15. var path:String = "/1/statuses/filter.json?follow=9701622";
  16. var username:String = "***"; //Authenticate username+pass here
  17. var pass:String = "***";
  18.  
  19. var request:URLRequest = createStreamRequest(host, path, username, pass);
  20. var stream:URLStream = new URLStream();
  21. stream.addEventListener(IOErrorEvent.IO_ERROR, errorReceived);
  22. stream.addEventListener(ProgressEvent.PROGRESS, dataReceived);
  23. stream.load(request);
  24.  
  25. function createStreamRequest(host:String, path:String, username:String, pass:String):URLRequest {
  26.   var request:URLRequest = new URLRequest("http://" + host + path);
  27.   request.requestHeaders = new Array(new URLRequestHeader("Authorization", "Basic " + b64encode(username + ":" + pass)));
  28.   request.method = URLRequestMethod.POST;
  29.   request.data = 0;
  30.   return request;
  31. }
  32.  
  33. // a simple helper that will base-64 encode a string
  34. function b64encode(s:String):String {
  35.   var encoder:Base64Encoder = new Base64Encoder();
  36.   encoder.encode(s);
  37.   return encoder.toString();
  38. }
  39.  
  40. function encodeStringForTransport(s:String):String {
  41.   return s.split("%").join("%25").split("\\").join("%5c").split("\"").join("%22").split("&").join("%26");
  42. }
  43.  
  44. var amountRead:int = 0;
  45. var isReading:Boolean = false;
  46. var streamBuffer:String = "";
  47. function dataReceived(pe:ProgressEvent):void {
  48.   var toRead:Number = pe.bytesLoaded - amountRead;
  49.   var buffer:String = stream.readUTFBytes(toRead);
  50.   amountRead = pe.bytesLoaded;
  51.  
  52.   // attempt to restart the stream
  53.   var parts:Array;
  54.   if (!isReading) {
  55.     parts = buffer.split(/\n/);
  56.     var firstPart:String = parts[0].replace(/[\s\n]*/, "");
  57.     if (firstPart != ""){
  58.         trace("streamReceived:", firstPart);
  59.         //ExternalInterface.call("streamEvent", encodeStringForTransport(firstPart));
  60.     }
  61.      
  62.     buffer = parts.slice(1).join("\n");
  63.     isReading = true;
  64.   }
  65.  
  66.   // pump the JSON pieces through -- due to actionscript to javascript
  67.   // encoding issues, we have to wrap them funnily
  68.   if ((toRead > 0) && (amountRead > 0)) {
  69.     streamBuffer += buffer;
  70.     parts = streamBuffer.split(/\n/);
  71.     var lastElement:String = parts.pop();
  72.     parts.forEach(function(s:String, i:int, a:Array):void {
  73.         trace("streamBuffer:", s);
  74.       //ExternalInterface.call("streamEvent", encodeStringForTransport(s));
  75.     });
  76.     streamBuffer = lastElement;
  77.   }
  78. }
  79.  
  80. // call out to javascript that there was an error in the stream
  81. function errorReceived(io:IOErrorEvent):void {
  82.   trace("streamError");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment