Parsing Tweets with the TwitterString Class

While building a little Twitter aggregator for an upcoming conference, I found myself in need of a set of methods to create hyperlinks from three distinct elements that can be included within a tweet; links, usernames, and hashtags.

I was able to find regular expressions to do all the heavy lifting from various sources on the Web and have created a class which pretty much does all the processing with one method call. Here’s an example of the thing working, followed by example code, and the class itself.

Example SWF

Flash Player 10.2 or greater is required!


Example MXML

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
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   creationComplete="init()" width="300" height="100" 
			   backgroundColor="#1E1E1E" preloaderBaseColor="#989898">
 
	<fx:Script>
		<![CDATA[
			import flashx.textLayout.elements.Configuration;
			import flashx.textLayout.elements.TextFlow;
			import flashx.textLayout.conversion.TextConverter;
			import flashx.textLayout.formats.TextLayoutFormat;
			import com.fracturedvisionmedia.utils.TwitterString;
 
			private var myTxt:String = "Everyone should follow @josephlabrecque (http://bit.ly/7NkqrB) - really cool stuff and super-informative insights! #Awesome #Super #LOL";
 
			private function init():void {
				// Configure styling the TextFlow links to match richTxt
				var cfg:Configuration = TextFlow.defaultConfiguration;
				var normalTLF:TextLayoutFormat = new TextLayoutFormat(cfg.defaultLinkNormalFormat);
				normalTLF.color = 0xDCD9D9;
				cfg.defaultLinkNormalFormat = normalTLF;
				TextFlow.defaultConfiguration = cfg;
 
				// Import tweet as HTML
				richTxt.textFlow = TextConverter.importToFlow(TwitterString.instance.parseTweet(myTxt), TextConverter.TEXT_FIELD_HTML_FORMAT);
			}
		]]>
	</fx:Script>
 
	<s:RichEditableText id="richTxt" selectable="false" editable="false" right="10" top="10" bottom="10" color="#DCD9D9" left="10"/>
 
</s:Application>

TwitterString Class

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
package com.fracturedvisionmedia.utils {
 
	/**
	 * The TwitterString class assists with the parsing of a tweet to add hyperlinks 
	 * around Links, HashTags, and UserNames in a tweet.
	 * @author Joseph Labrecque
	 * v. 0.1.2
	 */ 
 
	public final class TwitterString {
		private static var _instance:TwitterString = new TwitterString();
 
		public function TwitterString(){
			if (_instance != null){
				throw new Error("TwitterString can only be accessed through TwitterString.instance");
			}
		}
 
		public static function get instance():TwitterString {
			return _instance;
		}
 
		public function parseTweet(t:String):String {
			var step1:String = parseHyperlinks(t);
			var step2:String = parseUsernames(step1)
			var step3:String = parseHashtags(step2)
			return step3;
		}
 
		private function parseUsernames(t:String):String {
			var result:String = t.replace(/(^|\s)#(\w+)/g, "$1#<a href='http://search.twitter.com/search?q=$2' target='_blank'>$2</a>");
			return result;
		}
 
		private function parseHashtags(t:String):String {
			var result:String = t.replace(/(^|\s)@(\w+)/g, "$1@<a href='http://www.twitter.com/$2' target='_blank'>$2</a>");
			return result;
		}
 
		private function parseHyperlinks(t:String):String {
			var urlPattern:RegExp = new RegExp("(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)", "g")
			var result:String = t.replace(urlPattern, "<a href='$1' target='_blank'>$1</a>");
			return result;
		}
 
	}
}

Download TwitterString

Experimenting with Twitter, Again

The last time I messed around with Twitter was mid-2007. I basically used it to post little status updates a-la Facebook and experimented with the (then) newly released ActionScript 3 API. About a month into it, I got tired, deleted my account, and haven’t touched it since.

Now, all of a sudden, the US media is talking about Twitter as if it was some grand, new technology. They are, in general, freaking out about it. This gave me the idea last night to begin subverting the service in some experimental way. A person is supposed to use it to provide updates and communications to their “followers”. I’m gonna use it to generate small bits of stream-of-consciousness poetry which will most likely have no direct communicable affect.

There’s a new page on this site called “Twitter Poetry” where you can access this stuff directly, if you so wish.

On a related note, I’ve got about 30 minutes of material recorded for a new An early morning letter, displaced release. I don’t write nearly as much poetry as I used to which is pretty bad since that is actually the origin of the project. I’m hoping this Twitter experiment will give a little more energy to that area.

Thoughts on Twitter…

Twitter is a really strange service. When I first caught wind of this, my immediate reaction was; do we really need to be pouring more ephemeral nonsense into an internet already smothering with useless information?

I have to say that after using Twitter myself for the past few weeks, and playing around with the ActionScript 3 API they provide, that this service does have a certain temporal beauty to it. This is a somewhat controlled experiment which does not allow for much abuse as something like a MySpace:

  1. A user is restricted to the number of characters that can be used- effectively cutting off anyone from saying too much…
  2. The native interface, while somewhat customizable, is not prone to so much abuse by heady 12 yr. olds.
  3. The data is, by nature, temporal- so, not much needs to be said. Right to the point- like small bursts of thought.

I cannot say how long I will be using the service, but for now it is an interesting and unique method of communication which does [in my mind] lend itself to a certain natural attraction.

Messing Around with Twitter

I’ve taken some time today to experiment with Twitter using the ActionScript 3.0 API that is available. It’s a very simple class library and, as such, pretty easy to build a widget around. One thing that has bothered me a bit is the seemingly lengthy amount of time the services take to update the API feed once a new status event has been submitted. Maybe things are just slow on their end being a Saturday- but having any sort amount of delay in the feed broadcast would effectively cripple a service which is supposed to be immediate and timely.

I’m unsure how useful the Twitter service will be… right now it’s more of a novelty and an excuse to have some fun with AS3. Although, there are some projects coming up which will involve the integration of social network components- so I’ve begun to research various services. I have to say, Twitter is the only one that seems remotely useful so far!