
{"id":1196,"date":"2009-12-30T09:00:16","date_gmt":"2009-12-30T08:00:16","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=1196"},"modified":"2010-01-09T19:52:31","modified_gmt":"2010-01-09T18:52:31","slug":"twitter-rss-reader-in-flash-as3","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2009\/12\/30\/twitter-rss-reader-in-flash-as3\/","title":{"rendered":"Twitter rss reader in Flash as3"},"content":{"rendered":"<div class=\"update\"><strong>Update #1:<\/strong> <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2010\/01\/06\/twitter-rss-reader-part-2\/\">must read this post<\/a>, otherwise this code will fail when you place it on your server!<\/div>\n<p>I recently started my own company (<a href=\"http:\/\/www.eMCeeKay.nl\">eMCeeKay.nl<\/a>) and I need to make a website for it&#8230;. Not really a problem besides that I don&#8217;t have any time to create a design.<\/p>\n<p>So I was thinking about a easy way to update this website without spending extra time on that.<\/p>\n<p>Because the main focus of the company is papertoys (urban papertoys), I need design or papertoy related info in my my new website ().<\/p>\n<p>And I came with two ways to update it without extra effort: <a href=\"http:\/\/www.twitter.com\/matthijskamstra\">twitter<\/a> (I tweet about a lot of stuff but primarily about papertoys) and this the content on this blog (category: <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/category\/design\/urban-papercraft\/\">urban-papercraft<\/a>).<\/p>\n<p>I started with the Twitter part.<br \/>\nThere are two Twitter AS3 libraries that &#8220;speak&#8221; to the Twitter API: <a href=\"http:\/\/code.google.com\/p\/twitterscript\/\">twitterscript\/<\/a> and <a href=\"http:\/\/code.google.com\/p\/tweetr\/\">tweetr\/<\/a>.<br \/>\nBut after reading the source I concluded that it was a little bit to much: I don&#8217;t want to tweet from emceekay.nl or do searches, so that was not the way to go.<\/p>\n<p>A little google search gave me the solution: <a href=\"http:\/\/www.theflashlogs.org\/flash\/displaying-twitter-feeds-in-flash-with-rss\/\">http:\/\/www.theflashlogs.org\/flash\/displaying-twitter-feeds-in-flash-with-rss\/<\/a>, simple using the standard rss from Twitter (in my case: <a href=\"http:\/\/twitter.com\/statuses\/user_timeline\/27657030.rss\" title=\"Matthijs Kamstra - Twitter - rss\" rel=\"alternate\" type=\"application\/rss+xml\">Matthijs Kamstra &#8211; Twitter &#8211; rss<\/a>).<\/p>\n<p>The code on theflashlogs.org is pritty simple, and very easy to use.<br \/>\nSo my code is a little addition to that, I need some stuff done to the rss feed:<\/p>\n<ul>\n<li>remove &#8220;MatthijsKamstra:&#8221; form the title<\/li>\n<li>convert <strong>@paperkraft<\/strong> to a link<\/li>\n<li>convert <strong>#Mecha6<\/strong> to a link<\/li>\n<li>convert <strong>http:\/\/<\/strong> to a link<\/li>\n<\/ul>\n<p>Here is my code:<br \/>\n[as light=&#8221;false&#8221; wraplines=&#8221;false&#8221;]<br \/>\npackage nl.emceekay.twttr<br \/>\n{<br \/>\n\timport flash.display.MovieClip;<br \/>\n\timport flash.events.Event;<br \/>\n\timport flash.net.URLLoader;<br \/>\n\timport flash.net.URLRequest;<br \/>\n\timport flash.text.TextField;<br \/>\n\t\/**<br \/>\n\t * \/\/ nl.emceekay.twttr.TwttrExample<br \/>\n\t * &#8230;<br \/>\n\t * @author Matthijs Kamstra aka [mck]<br \/>\n\t *\/<br \/>\n\tpublic class TwttrExample extends MovieClip<br \/>\n\t{<br \/>\n\t\t\/\/default: http:\/\/twitter.com\/matthijskamstra<br \/>\n\t\tprivate var url:String = &quot;http:\/\/twitter.com\/statuses\/user_timeline\/27657030.rss&quot;; <\/p>\n<p>\t\tprivate var _txt:TextField;<\/p>\n<p>\t\tpublic function TwttrExample()<br \/>\n\t\t{<br \/>\n\t\t\tstage.scaleMode = &quot;noScale&quot;;<br \/>\n\t\t\tstage.align = &quot;TL&quot;;<\/p>\n<p>\t\t\t\/\/ generate textfield<br \/>\n\t\t\t_txt = new TextField()<br \/>\n\t\t\t_txt.x = 10;<br \/>\n\t\t\t_txt.y = 10;<br \/>\n           \t\t _txt.width = stage.stageWidth &#8211; 20;<br \/>\n           \t\t_txt.height =  stage.stageHeight &#8211; 20;<br \/>\n\t\t\t_txt.wordWrap = true;<br \/>\n\t\t\t_txt.multiline = true;<br \/>\n\t\t\t_txt.autoSize = &quot;left&quot;;<br \/>\n\t\t\taddChild(_txt);<\/p>\n<p>\t\t\t\/\/ start<br \/>\n\t\t\tgetFeed(url);<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ loading rss \/ show rss \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p>\t\tprivate function getFeed (inURL:String) : void<br \/>\n\t\t{<br \/>\n\t\t\t_txt.htmlText = &quot;getting tweets&quot;;<\/p>\n<p>\t\t\tvar loader:URLLoader = new URLLoader();<br \/>\n\t\t\tloader.addEventListener(Event.COMPLETE, onFeedHandler);<br \/>\n\t\t\tloader.load(new URLRequest(inURL));<br \/>\n\t\t}<\/p>\n<p>\t\tprivate function onFeedHandler (e:Event):void<br \/>\n\t\t{<br \/>\n\t\t\t_txt.htmlText = &quot;&quot;;<br \/>\n\t\t\tvar _feed:XML = new XML(e.target.data);<br \/>\n\t\t\tvar _item:XMLList = _feed.channel.item;<br \/>\n\t\t\tfor each (var feedItem:XML in _item){<br \/>\n\t\t\t\tvar _title\t\t:String = feedItem.title;<br \/>\n\t\t\t\tvar _pubDate\t:String = feedItem.pubDate;<br \/>\n\t\t\t\tvar _link\t\t:String = feedItem.link;<\/p>\n<p>\t\t\t\t_title = convertTweet(_title);<\/p>\n<p>\t\t\t\t_txt.htmlText += _title + &quot;&lt;br&gt;&lt;i&gt;&quot; + _pubDate + &quot;&lt;\/i&gt;&lt;br&gt;&lt;br&gt;&quot;;<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ twitter specific \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p>\t\t\/\/ one place to convert the tweet<br \/>\n\t\tprivate function convertTweet (inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\t_str = twttrStripName(_str);<br \/>\n\t\t\t_str = twttrConvertHTTP(_str);<br \/>\n\t\t\t_str = twttrConvertMention(_str);<br \/>\n\t\t\t_str = twttrConvertHashtag(_str);<br \/>\n\t\t\t_str = twttrConvertSmileys(_str);<br \/>\n\t\t\treturn _str;<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ remove the writers name from the tweet<br \/>\n\t\tprivate function twttrStripName (inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\tvar _charNumber:Number = _str.indexOf(&quot;:&quot;);<br \/>\n\t\t\treturn _str.substr(_charNumber + 2);<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ convert http-strings to links<br \/>\n\t\tprivate function twttrConvertHTTP(inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\tvar _array:Array = _str.split(&quot; &quot;);<br \/>\n\t\t\tfor (var i:int = 0; i &lt; _array.length; i++)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tvar _str2:String = twttrStripChar(_array[i]);<br \/>\n\t\t\t\tif (_array[i].indexOf(&quot;http&quot;) != -1)<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\t_array[i] = &quot;&lt;u&gt;&lt;a href=&#8217;&quot; + _str2 + &quot;&#8217; target=&#8217;_blank&#8217;&gt;&quot; +_array[i] + &quot;&lt;\/a&gt;&lt;\/u&gt;&quot;;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\treturn _array.join(&quot; &quot;);<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ convert mentions (@) to links<br \/>\n\t\tprivate function twttrConvertMention(inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\tvar _array:Array = _str.split(&quot; &quot;);<br \/>\n\t\t\tfor (var i:int = 0; i &lt; _array.length; i++)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tvar _str2:String = twttrStripChar(_array[i]);<br \/>\n\t\t\t\tif (_array[i].substr(0, 1) == &quot;@&quot;)<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\t_array[i] = &quot;&lt;u&gt;&lt;a href=&#8217;http:\/\/www.twitter.com\/&quot; + _str2.split(&quot;@&quot;)[1] + &quot;&#8217; target=&#8217;_blank&#8217;&gt;&quot; +_array[i] + &quot;&lt;\/a&gt;&lt;\/u&gt;&quot;;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\treturn _array.join(&quot; &quot;);<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ convert hashtags (#) to links<br \/>\n\t\tprivate function twttrConvertHashtag(inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\tvar _array:Array = _str.split(&quot; &quot;);<br \/>\n\t\t\tfor (var i:int = 0; i &lt; _array.length; i++)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tvar _str2:String = twttrStripChar(_array[i]);<br \/>\n\t\t\t\tif (_array[i].substr(0, 1) == &quot;#&quot;)<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\t_array[i] = &quot;&lt;u&gt;&lt;a href=&#8217;http:\/\/twitter.com\/search?q=%23&quot; + _str2.split(&quot;#&quot;)[1] + &quot;&#8217; target=&#8217;_blank&#8217;&gt;&quot; +_array[i] + &quot;&lt;\/a&gt;&lt;\/u&gt;&quot;;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\treturn _array.join(&quot; &quot;);<br \/>\n\t\t}\t\t<\/p>\n<p>\t\t\/\/ TODO: [mck] convert \ud83d\ude42 to a smiley image<br \/>\n\t\tprivate function twttrConvertSmileys(inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\treturn _str;<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ remove &quot;strange&quot; characters from the end of the string<br \/>\n\t\tprivate function twttrStripChar(inString:String):String<br \/>\n\t\t{<br \/>\n\t\t\tvar _str:String = inString;<br \/>\n\t\t\tvar _charArray:Array = [&#8216;,&#8217;, &#8216;;&#8217;, &#8216;:&#8217;, &#8216; &#8216;, &#8216;-&#8216;, &#8216;_&#8217;];<br \/>\n\t\t\tfor (var i:int = 0; i &lt; _charArray.length; i++)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tif (_str.charAt(_str.length &#8211; 1) == _charArray[i]) {<br \/>\n\t\t\t\t\t_str = _str.substr(0, _str.length &#8211; 1);<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\treturn _str;<br \/>\n\t\t}<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<\/p>\n<p>Currently I have only this proof of concept, but soon I will post the example on <a href=\"http:\/\/www.emceekay.nl\">emceekay.nl<\/a>&#8230;<\/p>\n<p>Because this is part one of what I want to do, and don&#8217;t want to search for every link again, I&#8217;m posting also the WordPress link here (it&#8217;s just because I&#8217;m lazy).<\/p>\n<p>So here the feeds I will be using:<\/p>\n<ul>\n<li><a href=\"http:\/\/twitter.com\/statuses\/user_timeline\/27657030.rss\" title=\"Matthijs Kamstra - Twitter - rss\" rel=\"alternate\" type=\"application\/rss+xml\">Matthijs Kamstra &#8211; Twitter &#8211; rss<\/a><\/li>\n<li><a href=\"http:\/\/www.matthijskamstra.nl\/blog\/wp-rss2.php?cat=11\" title=\"[mck] Urban papercraft RSS feed\" rel=\"alternate\" type=\"application\/rss+xml\">All Urban papercraft posts<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Update #1: must read this post, otherwise this code will fail when you place it on your server! I recently started my own company (eMCeeKay.nl) and I need to make a website for it&#8230;. Not really a problem besides that I don&#8217;t have any time to create a design. So I was thinking about a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,10,3,11],"tags":[408,398,23,119,123,24,402,186,36],"class_list":["post-1196","post","type-post","status-publish","format-standard","hentry","category-as3","category-design","category-flash","category-urban-papercraft","tag-as3","tag-flash","tag-papercraft","tag-papertoy","tag-papertoys","tag-urban","tag-urban-papercraft","tag-wordpress","tag-xml"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/1196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/comments?post=1196"}],"version-history":[{"count":11,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/1196\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}