Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Getting the current Twitter-buzz with C#

My problem
I want to know what my friends on Twitter are talking about most

image

What’s the plan:

  1. Get the tweets from my friends
  2. Search this tweets for the #hashtags (Regex!!!)
  3. Count the hashtags
  4. Output the hashtags ordered by count

With tools like VS, .net framework and Twitter API this is EASY!!
Wooohooo

1. How to get the tweets

a) Twitter API

Find all info here http://apiwiki.twitter.com/ 
Currently Twitter has 2 APIs, maybe they make things easier and bring these together…
image

b) Tweetsharp

Tweetsharp is an awesome fluent interface for the Twitter API.
It’s toooo easy!!!! Make sure you check it out

http://code.google.com/p/tweetsharp/
image

c) Twitter "low level"

You can consume Twitter stuff via standard HTTP calls and get the response as XML or JSON, see this blog for an example http://psantos-blog.zi-yu.com/?p=197
That’s tooo hard :-)

 

I picked Tweetsharp, because there are so many samples available, and it is fluent!  I love Intellisense!

  1. Download tweetsharp http://code.google.com/p/tweetsharp/
  2. Unzip to your References Solution folder
  3. Add reference to your project
  4. Done

How easy is that!

 

Note
Make sure to use a Console project, we want to focus on functionality not on UI
image

 

The code to get the list of tweets looks like this

           var twitter = FluentTwitter.CreateRequest().AuthenticateAs(MyUser, MyPass) 
           .Statuses().OnFriendsTimeline().Take(1000).AsJson();

           // Get response from Twitter
           var response = twitter.Request();

           // Convert response to data classes
           var tweets = response.AsStatuses();

 

Next step

Iterate through tweets and search for the hashtags
We use a Regex for that

private static Regex regexHashTags = new Regex(@"#\w*", RegexOptions.IgnoreCase);

No more code, because that’s up to you how to do that

 

Put this hashtags into dictionary

Dictionary<string, int> 

(string is the hashtag, int is the count)
Easy, so no code here

 

Sort the Dictionary with LINQ

var sortedDict = (from entry in hashtagCountDict orderby entry.Value descending select entry);

 

The final output is easy again

Iterate through the sorted dictionary and throw it to the Console

image
Figure: Output of current Twitter buzz. Sharepoint is big currently (13/05/2009)
because of the TechEd announcements of Sharepoint 2010

 

Advanced steps

  1. Get the tweet updates every couple of seconds
  2. Use a   while (abortKeyNotPressed) {   }  with a Thread.Sleep to get the buzz every 1 minute
  3. Use Console KeyAvailable to abort application
  4. Use the space key to get the current state of the iteration
  5. Serialize current dictionary to file, and load that at startup
    Continue from last time

 

Thanks Robert Mühsig for VERY useful input http://code-inside.de/blog/2009/04/20/howto-twittern-mit-c/

LINQ: Remember to use the let keyword

 

Sample code from http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx

How to filter for names that are 4 or 5 and startwith or endwith vowel?
My description is more difficult to read than the actual LINQ query.

Check it out!
namelist = List<string> { …with some names… };

var names = (from p in nameList 
                let vowels = new List<string> { "A", "E", "I", "O", "U" } 
                let startsWithVowel = vowels.Any(v => p.ToUpper().StartsWith(v)) 
                let endsWithVowel = vowels.Any(v => p.ToUpper().EndsWith(v)) 
                let fourCharactersLong = p.Length == 4 
                let fiveCharactersLong = p.Length == 5 
            where 
                (startsWithVowel || endsWithVowel) && 
                (fourCharactersLong || fiveCharactersLong) 
                select p).ToList();

Entity Framework: Why not use Bindingsource.AddNew for creating new objects...

Assuming that you have a Detail Form with a Bindingsource, and the Bindingsource has as Datasource a IQueryable or something derived from that.

image BAD

If you use the Bindingsource to create new objects like this:
      bindingSource.AddNew(); 
your record is detached, and your business rules fire on Save (probably to late for Winforms)

image BETTER

Use
      bindingSource.DataSource = Business.AddNewObject(); 
Your record is attached, and business rules fire OnChange

Assuming you have a Business method like this.

      public Patients AddNewObject()
      {
          MyObject p = new MyObject ();
          // SetDefaultValues(p);
          DBConnection.AddToMyObject(p);
          return p;
      }

Entity Framework: How to set Defaultvalues for DateTime fields

It's easy I thought, just select the Property of the EntityType and set the Default Value in the Properties window to: DateTime.Now
image

But it's not

Defaultvalues get validated on Compile time not on Design time.
So after Rebuild you get:

Error      1              
Error 54: 
Default value (System.DateTime.Now) is not valid for DateTime. The value must be in the form 'yyyy-MM-dd HH:mm:ss.fffZ'.                
C:\DataPeterGfader\ProjectsTFS\ImportantClient\Business\Entities\MyImportantModel.edmx 

How to set then the default value?

Use the constructor of the Entity.
Yes I know that it's not nice to to this manually!

Example code

    public partial class Transactions
    {
        public Transactions()
        {
            //HACK: To prevent this error: {"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."}
            this.LastModified = System.DateTime.Now;
        }
    }

Memos of my LinqDatasource experience

How to group by more than 1 table column (attribute, property)?

GroupBy="new(Roles.RoleName, Roles.RoleId)" Access with: Key.RoleName, (for example in your listview or gridview)

<asp:LinqDataSource runat="server" ID="ldsUsers"
ContextTypeName="MyPrivateProject.Test.DataAccess.MyPersonalPrivateDataContext" TableName="Users" GroupBy="new(Roles.RoleName, Roles.RoleId)" OrderGroupsBy="Key.RoleName" OrderBy="UserName, Lastname, Firstname" Select="new(Key, Count() As RecordCount, It As Users)" > </asp:LinqDataSource>

How to use Guids in LinqDataSource where Parameters?

Use DbType="Guid" instead of Type="xxx". TODO: Check input param! e.g. MyPage.aspx?UserIdInput=xxxasd-grrrr-blabla-TEXT-FFAILS --> throws an error

<asp:LinqDataSource runat="server" ID="ldsUsers"
   ContextTypeName="MyPrivateProject.Test.DataAccess.MyPersonalPrivateDataContext"
   TableName="Users"
   Where="UserId = @UserIdInput" EnableUpdate="True">
     <WhereParameters>
          <asp:QueryStringParameter
                 DefaultValue="none" Name="UserIdInput"
                 QueryStringField="UserId" 
              DbType="Guid"  />
     </WhereParameters>
</asp:LinqDataSource>

Syntax highligthing provided by FaziBear's Google widget But removed cause it doesnt support Highlighting of single lines nor sections

basta.net 2007 Fazit

Resume

  • basta.net 2007 war interessant, abwechlsungsreich, SEHR lehrreich und hat sich voll ausgezahlt!
  • Essen war super, aber man brauchte schon Jongleurkünste um mit 1er Hand das Essen aufzulegen und dann zu seinem Tisch zu spazieren...
  • Rückflug wurde gecancelt aber Taxi brachte uns nach Innsbruck, 2h Verspätung. Passt eh.

Ich mag keine Session mit:

  • Agenda Vorleser
  • Lahmsieder, Vortrag beginnt langsam, und das Interessante das zum Schluss kommen würde wird nicht mehr gezeigt weil Zeit fertig

Sessions die ich besucht hab
  • c# 3.5 Nachfolger von c#3.0
  • Continuos Integration Software Engineering Praktik --> Tägliches Checkin and Build
  • ASP.net und AJAX, Dino Esposito Partial Rendering und Scritp Services
  • LINQ, Jens Häupel Language Integrated Query sind Language Extensions (Spracherweiterungen) und NICHT Framework Extensions
  • Visual Studio 2005 Tips and Tricks, Dirk Primbs Coole unterhaltsame Session mit zahlreichen TipsTricks
  • xml in .net 3.5
  • .net Security Top Fehler 1. Regel: KEINE Krypto Algorithmen selber schreiben
  • Silverlight + AJAX, Dino Esposito Präsentation von Silverlight
  • LINQ to SQL, Jens K. Süßmeyer
  • Powershell, Holger Schwichtenberg
  • web2.0 auf Mobiles, Thorsten Weber
  • ADO.net 2007 + Zukunft, Andreas Kosch Daten != Objekte, sagte Anders Hejlsberg
  • Bluffer Guide to C#3.0, Oliver Sturm Sehr gute Intro in C#3.0
  • C# 3.0 Advanced, Oliver Sturm wieder SEHR interessant
  • CAB, SCSF, Acropolis, Benjamin Gopp Bis es zum interessanten Teil kam war fertig
Jeden Tag 13 Stunden .net, da haut man sich gern ins Bett... Details zu den Sessions in den nächsten Posts...

Latest Posts

Popular Posts