Twitter, Twitter, Twitter
by Martin on 22/03/2009Update!! Yahoo did the same dick move of cockblocking Flash. :/ To remedy this the URLRequest on line 80 had to be changed. If your project used to work but doesn’t anymore; Try this. If this is your first time here; Ignore this message.
—
Ad infinitum, ad nauseum.
So, I saw that I’m still getting some traffic to this post where I suggested a solution to the Twitter crossdomain.xml problem.
The Yahoo pipes solution still works fine, but my code is a godawful mess and I decided I’d have to fix it up a bit. The methodology is essentially the same. We’ll be using this yahoo pipe, but it’s neatly blackboxed so that you just need to know your Twitter ID to use this class.
The last iteration of this class returned the tweets as an Array of Strings. This time I decided to return them as Objects containing the different versions of the tweet :
- raw: The raw tweet. No filtering for URLs, no trimming the username.
- chopped: As above, but “yourusername: ” is removed.
- linked: Same as raw, but with all URLs tagged.
- choppedAndLinked: … you get it.
I’m only partly convinced that this is a wise way to go about it, but I’m thinking of expanding this out to include a “Tweet” class, and if I go that way it’ll be fortuitous to have been treating the information as objects already.
Note that in order to get this to work you’ll need both the TwitterPipe class and the URLValidator class I’ve discussed here earlier. I’ve gathered both in this handy zip for you along with a short example on usage. If you’re the kind that wants to read some code before committing to clicking on a link and opening a document, well here you go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package com.ctrloptcmd.twitter { /** * TwitterPipe.as * * @langversion: ActionScript 3.0 * @playerversion: Flash 10.0 * * @author: Martin Jacobsen * @since: 22-03-2009 * * @description : Cleaned up version of the previous "TweetPipeLoad" * class which was written by a younger, more foolish and not quite as * pretty version of myself. I'm not saying this class is by any means * a stroke of genius, but by jove it certainly is an improvement on * the dreadful mess of the old one. * * This new and improved version has far less unused "mass imports", less * incomprehensible variables and actually uses E4X for what it's worth * rather than clumsily climbing through XMLNode hierarchies AS2 style. * * Also; This class utilizes the URLValidator class for parsing through * tweets for URLs rather than relying on string-parsing mumbo jumbo and * misunderstood implementation of black magic regular expressions to try * and figure out whether there are any URLs present. Just trust me on this; * It's better. * * Note that this version returns a Vector with objects rather than just strings. * I found that returning objects and accessing the different versions of the * string representations seemed somehow easier to use. It also makes it easy to * later expand upon with a "Tweet" class if that should prove interesting at * any time. * * The class uses Vectors because it seems to be all the rage these * days among the optimization literati. If you don't like Flash Player * 10, feel free to swap them for Arrays, as I don't think there's anything * else in here that necessitates FP10. * */ import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLVariables; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import com.ctrloptcmd.string.URLValidator; public class TwitterPipe extends EventDispatcher { private var request : URLRequest; private var variables : URLVariables; private var loader : URLLoader; private var txml : XML; private var urlvalidator : URLValidator; private var ignoreReplies : Boolean; private var userID : String; public var tweets : Vector.<object>; public static const FAILWHALE : String = "failwhale"; public static const FREEBIRD : String = "freebird"; public function TwitterPipe (_userID : String, _ignoreReplies : Boolean = true) { ignoreReplies = _ignoreReplies; userID = _userID; variables = new URLVariables(); variables._render = "rss"; variables.twitter = userID; request = new URLRequest("http://pipes.yahooapis.com/pipes/pipe.run?_id=4b85031723ba7785e277add01b0169c5&_render=rss&twitter="+userID); request.data = variables; loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadIOError); loader.load(request); } private function onLoadComplete(e:Event) : void { if (loader.data) { tweets = new Vector.</object><object>(); urlvalidator = new URLValidator(); txml = new XML(loader.data); for each (var xTweet:XML in txml..item..description.text()) { var sTweet : String = xTweet.toString(); var tweet : Object = {}; tweet.raw = sTweet; tweet.chopped = sTweet.substr(sTweet.indexOf(":") + 2); tweet.linked = urlvalidator.tag(sTweet); tweet.choppedAndLinked = urlvalidator.tag(sTweet.substr(sTweet.indexOf(":") + 2)); if(sTweet.indexOf("@") > -1) tweet.friendly = true; else tweet.friendly = false; if(!ignoreReplies) tweets.push(tweet); else if (ignoreReplies && !tweet.friendly) tweets.push(tweet); } this.dispatchEvent(new Event(FREEBIRD)); } } public function getTweets(): Vector.</object><object> { return tweets; } private function onLoadIOError(ioe : IOErrorEvent) : void { this.dispatchEvent(new Event(FAILWHALE)); } } } |
So there you go. Minor functionality improvements but infinitely more readable and well-structured.
There are 28 comments in this article: