FlyPaper

the sticky side goes up…

Parsing Hashtags in Flash


   Feb 19

Parsing Hashtags in Flash

Today I helped a friend with some top secret mission to parse his twitter in flash.  So here’s how it works:

First, you’ll need to borrow this package from In Flagrante Delicto  (you won’t need the mxml or flex but his demo is great if you are lookin’ for that kind o’ thing).

Now for my assumptions: 1, you can build an AS 3 Flash doc with a dynamic html text box.  2. You already have a string for your twitter feed XML.

Set yourself up with a as3 flash doc and add a dynamic text field, name it textArea.

You’ll need the following code:

 

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.xml.XMLDocument;
    import TwitterString;

    public class Main extends MovieClip
    {
        private var twitterXML:XML; // This holds the xml data
        private var twitterScriptPath:String;
        private var myTwitterID:String;

        public function Main()
        {
            // Put your Twitter username here:
            myTwitterID = "YER_ID"; 

            // Put the path to your script to get the XML feed here:
            twitterScriptPath = "http://www.YERSITE.com/YerFile";

            loadTwitterXML();
        }

        private function loadTwitterXML():void
        {
            var urlLoader:URLLoader = new URLLoader();
            // When all the junk has been pulled in from the url, we'll fire finishedLoadingXML:
            urlLoader.addEventListener(Event.COMPLETE, finishLoadingXML);

            urlLoader.load(new URLRequest(twitterScriptPath + "?twitterId=" + myTwitterID));
            //You can mess with the security sandbox error (or to test locally) by swapping the next line with the one above
            //urlLoader.load(new URLRequest("http://twitter.com/statuses/user_timeline/YER_USERNAME.xml"));        

        }

        private function finishLoadingXML(e:Event):void
        {
            e.target.removeEventListener(Event.COMPLETE, finishLoadingXML);

            // Populate the xml object with the xml data:

            var XMLTwitterDoc:XMLDocument = new XMLDocument();
            var twitterFeed:String;

            XMLTwitterDoc.parseXML(e.target.data);
            twitterXML = new XML(XMLTwitterDoc); 

            showTwitterStatus();
        }

        private function showTwitterStatus():void
        {
            trace("showTwitterStatus");

            // Prep the text field to hold our latest Twitter update:
            twitter_txt.wordWrap = true;
            twitter_txt.autoSize = TextFieldAutoSize.LEFT;

            // Populate the text field with the first element in the status.text nodes:
            var textString:String = twitterXML.status.text[0].toString();

            trace(TwitterString.instance.parseTweet(textString));
            twitter_txt.htmlText = TwitterString.instance.parseTweet(textString);        

        }
    }
}
    public class Main extends MovieClip

    {

        private var twitterXML:XML; // This holds the xml data
        private var twitterScriptPath:String;
        private 

So that’s about it,  other than it being a good idea to save the data in a flash stored object so you don’t overhit the twitter API. 

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.