<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>IRhetoric - Karsten Januszewski   : Twitter</title><link>http://blogs.msdn.com/karstenj/archive/tags/Twitter/default.aspx</link><description>Tags: Twitter</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Announcing Flotzam: Twitter, Flickr, Facebook and Blog Mash Up</title><link>http://blogs.msdn.com/karstenj/archive/2007/06/21/announcing-flotzam-twitter-flickr-facebook-and-blog-mash-up.aspx</link><pubDate>Fri, 22 Jun 2007 01:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3449349</guid><dc:creator>karstenj</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/karstenj/comments/3449349.aspx</comments><wfw:commentRss>http://blogs.msdn.com/karstenj/commentrss.aspx?PostID=3449349</wfw:commentRss><description>&lt;P&gt;Awhile back, for MIX, &lt;A href="http://systim.spaces.live.com/" mce_href="http://systim.spaces.live.com/"&gt;Tim Aidlin&lt;/A&gt; and I built Flitterbook.&amp;nbsp; We liked it so much that we now have version 2, renamed &lt;A href="http://flotzam.com/" mce_href="http://flotzam.com/"&gt;Flotzam&lt;/A&gt;, ready for your consumption.&amp;nbsp; Lots of improvements, including a new UI, RSS support, Facebook notifications and complete configuration control. Also, check out the &lt;A href="http://flotzam.com/blog/post/flotzam-video.aspx" mce_href="http://flotzam.com/blog/post/flotzam-video.aspx"&gt;Silverlight-created video that is a screen capture of the application in action&lt;/A&gt;.&amp;nbsp; So, we'd love for you to &lt;A href="http://flotzam.com/" mce_href="http://flotzam.com/"&gt;go check it out&lt;/A&gt;, let us know what you think on the &lt;A href="http://flotzam.com/forum/" mce_href="http://flotzam.com/forum/"&gt;Flotzam forum&lt;/A&gt; and watch the &lt;A href="http://flotzam.com/blog" mce_href="http://flotzam.com/blog"&gt;Flotzam blog&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://flotzam.com/" border="0"&gt;&lt;IMG src="http://flotzam.com/images/flotzam.jpg" border=0 mce_src="http://flotzam.com/images/flotzam.jpg"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3449349" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/karstenj/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.msdn.com/karstenj/archive/tags/Facebook/default.aspx">Facebook</category><category domain="http://blogs.msdn.com/karstenj/archive/tags/Twitter/default.aspx">Twitter</category></item><item><title>CREATING A .NET TWITTER API in 4.5 SECONDS</title><link>http://blogs.msdn.com/karstenj/archive/2007/06/08/creating-a-net-twitter-api-in-4-5-seconds.aspx</link><pubDate>Sat, 09 Jun 2007 00:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3168876</guid><dc:creator>karstenj</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/karstenj/comments/3168876.aspx</comments><wfw:commentRss>http://blogs.msdn.com/karstenj/commentrss.aspx?PostID=3168876</wfw:commentRss><description>&lt;P&gt;I've been meaning to document how I wrapped the Twitter API ... in 4.5 seconds.&amp;nbsp;&amp;nbsp; There is a&amp;nbsp;very powerful feature in .NET is a tool in the sdk called &lt;STRONG&gt;xsd.exe&lt;/STRONG&gt;.&amp;nbsp; Darren David actually has &lt;A href="http://blog.lookorfeel.com/index.php/2007/05/02/the-net-xml-schema-definition-tool-or-how-i-learned-to-stop-parsing-and-love-the-dom-part-1/" mce_href="http://blog.lookorfeel.com/index.php/2007/05/02/the-net-xml-schema-definition-tool-or-how-i-learned-to-stop-parsing-and-love-the-dom-part-1/"&gt;a fabulous post&lt;/A&gt; on&amp;nbsp;exactly the power of this tool: never touch a DOM&amp;nbsp;or XPATH again!&amp;nbsp; &amp;nbsp;His post explains what is happening under the hood with xsd.exe quite nicely.&amp;nbsp;I'd definitely recommend reading his post.&amp;nbsp; I figured it might be interesting for folks to see&amp;nbsp;the exact steps I took to wrap the Twitter API:&lt;/P&gt;
&lt;P&gt;1. Grab an instance of the public timeline from Twitter and pass that to xsd.exe to infer a schema as follows: &lt;STRONG&gt;xsd&lt;/STRONG&gt; &lt;A title=http://twitter.com/statuses/public_timeline.xml href="http://twitter.com/statuses/public_timeline.xml" mce_href="http://twitter.com/statuses/public_timeline.xml"&gt;http://twitter.com/statuses/public_timeline.xml&lt;/A&gt;&amp;nbsp;which creates public_timeline.xsd&lt;/P&gt;
&lt;P&gt;3. Use the inferred schema to generate classes: &lt;STRONG&gt;xsd&lt;/STRONG&gt; public_timeline.xsd&lt;/P&gt;
&lt;P&gt;4. Walla!&amp;nbsp; We now have a class to party on called public_timeline.cs&lt;/P&gt;
&lt;P&gt;5. To instantiate the class with live data, it is as simple as the following:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(statuses));&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;statuses s = serializer.Deserialize(new System.Xml.XmlTextReader("&lt;/STRONG&gt;&lt;/FONT&gt;&lt;A href="http://twitter.com/statuses/public_timeline.xml%22))" mce_href='http://twitter.com/statuses/public_timeline.xml"))'&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;http://twitter.com/statuses/public_timeline.xml"))&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt; as statuses;&lt;/STRONG&gt;&lt;/FONT&gt; 
&lt;P&gt;And there you have it.&amp;nbsp; No DOM, no XPATH, no RegEx.&amp;nbsp; Now, if we want to take this wrapper to the next level, there are a few things we can do.&amp;nbsp; First, you will notice that Twitter returns the data as a string.&amp;nbsp; That's kind of a bummer; it would be nice to have it as a strongly typed &lt;STRONG&gt;DateTime&lt;/STRONG&gt;.&amp;nbsp; Here's the line of code to do that: 
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;DateTime date = DateTime.ParseExact(s.created_at, "ddd MMM dd HH:mm:ss zzzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);&lt;/STRONG&gt;&lt;/FONT&gt; 
&lt;P&gt;Another thing that you'll need to do is authenticate when you contact Twitter if you want data other than the public timeline.&amp;nbsp; Here's how to do that: 
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;using (WebClient client = new WebClient())&lt;BR&gt;{&lt;BR&gt;client.Credentials = new NetworkCredential(TWITTER_USERNAME, TWITTER_PASSWORD);&lt;BR&gt;statuses s = serializer.Deserialize(client.OpenRead(TWITTER_URL)) as statuses;&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt; 
&lt;P&gt;Of course, the TWITTER variables need to be set with the username, password and particular URL to the Twitter feed you are looking for.&amp;nbsp; 
&lt;P&gt;Another thing to think about doing is making the calls to Twitter on a different thread.&amp;nbsp; My recommendation for doing that is to implement &lt;A href="http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx" mce_href="http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx"&gt;Dan Crevier's Data-View-View Model&lt;/A&gt;.&amp;nbsp; This frees the UI thread from any calls to Twitter and provides a really nice model for marshalling data between the threads.&amp;nbsp; In fact, I did just this in my &lt;A href="http://winfx.members.winisp.net/blog/flitter.zip" mce_href="http://winfx.members.winisp.net/blog/flitter.zip"&gt;Flitter&lt;/A&gt; and &lt;A href="http://blogs.msdn.com/coding4fun/attachment/2854939.ashx" mce_href="http://blogs.msdn.com/coding4fun/attachment/2854939.ashx"&gt;Flitterbook&lt;/A&gt; implementations, which you can check out in the source code. 
&lt;P&gt;The final thing to think about, which I just recently implemented in some code I was working on, was to make the naming conventions look prettier.&amp;nbsp; Darren David alluded to this in his post but he's been so busy with the totally amazing &lt;A href="http://blog.lookorfeel.com/index.php/2007/06/04/hp-multi-touch-interactive-canvas-launched-at-d5/" mce_href="http://blog.lookorfeel.com/index.php/2007/06/04/hp-multi-touch-interactive-canvas-launched-at-d5/"&gt;HP Multi Touch kiosk he built&lt;/A&gt; (which you have to check out the videos for if you haven't yet) that he hasn't posted his part II.&amp;nbsp; But I imagine he's going to talk about using partial classes so as to make the class look prettier without touching the generated code.&amp;nbsp; This came in real handy with the Twitter API which generated some code with ugly array structures that I wasn't so keen on.&amp;nbsp;I also had a need to use&amp;nbsp;a kind of cool feature of WPF databinding&amp;nbsp;where I&amp;nbsp;wanted to reuse a datatemplate between two&amp;nbsp;different objects&amp;nbsp;(Twitter status and Facebook status) by just having them share names, such as AvatarURL, rather than create two templates that were fundamentally identitcal except for the data object.&amp;nbsp;As such, I created a partial class with a bunch of getters as follows: 
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;public partial class statusesStatus&lt;BR&gt;{&lt;BR&gt;public DateTime Date&lt;BR&gt;{&lt;BR&gt;get&lt;BR&gt;{&lt;BR&gt;return DateTime.ParseExact((string)this.created_at, "ddd MMM dd HH:mm:ss zzzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);&lt;BR&gt;}&lt;BR&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;public string AvatarUrl&lt;BR&gt;{&lt;BR&gt;get&lt;BR&gt;{&lt;BR&gt;return this.user[0].profile_image_url;&lt;BR&gt;}&lt;BR&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;public string Text&lt;BR&gt;{&lt;BR&gt;get&lt;BR&gt;{&lt;BR&gt;return this.text;&lt;BR&gt;}&lt;BR&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;public string UserName&lt;BR&gt;{&lt;BR&gt;get&lt;BR&gt;{&lt;BR&gt;return this.user[0].screen_name;&lt;BR&gt;}&lt;BR&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;public string Location&lt;BR&gt;{&lt;BR&gt;get&lt;BR&gt;{&lt;BR&gt;return this.user[0].location;&lt;BR&gt;}&lt;BR&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;} &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;P&gt;Now I have a much prettier class without touching the generated code from xsd.exe.&amp;nbsp; Sorry for stealing Part II Darren. I couldn't&amp;nbsp;help myself, as I&amp;nbsp;have been&amp;nbsp;meaning to write this post for about 2 months!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3168876" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/karstenj/archive/tags/Windows+Presentation+Foundation+_2800_Avalon_2900_/default.aspx">Windows Presentation Foundation (Avalon)</category><category domain="http://blogs.msdn.com/karstenj/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.msdn.com/karstenj/archive/tags/Twitter/default.aspx">Twitter</category></item></channel></rss>