Click here to Skip to main content
Click here to Skip to main content

Tagged as

Zeta Twitter

, 5 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Auto Follow Keywords On Twitter

Introduction

In this article I will introduce you a small console application written in C# and .NET 3.5 that enables you to automatically follow Twitter users based on keywords they use in their tweets.

If you don't know Twitter, you may look at their website Twitter.com or read the Wikipedia article about Twitter.

Background

The motivation for developing this tool was to enable some of our company's Twitter accounts to automatically follow other Twitter users based on the keywords they post.

E.g. we maintain a Twitter account for our CMS Zeta Producer Desktop. Now I wanted to do a search for "cms" on Twitter and follow all users (or the first n ones) that were displayed in the search result.

Even though a similar service exists with Twollow, they charge for their services. Since I wanted a free version (although I do have way less features than they have implemented), I started developing Zeta Twitter.

Technical implementation

The application is a .NET 3.5 console application that is intended to be used as a scheduled task in the Windows Task Scheduler. You would e.g. configure the task to run every 30 minutes.


Example screenshot of the output of the ZetaTwitter.exe console application.

I am using the free library tweet# to communicate with the Twitter API.

You can add multiple Twitter accounts and for each account multiple keywords to check. The whole configuration is done within a separate configuration file called "ZetaTwitterConfiguration.xml". This file must be stored in the same folder as the executable "ZetaTwitter.exe".


Example configuration file.

Currently all attributes are mandatory. Adjust the file in the downloaded archive with the XML/text editor of your choice.

Internal function

Since being nicely asked to explain my code a little bit, this chapter outlines the highlights. Again the warning that none of the code is rocket science.

Configuration

The configuration is being stored in an XML file (as stated above) and being read into two classes by simple XmlDocument method calls. I do read it manually, not with any XML to class mapping.

The classes are:

  • Configuration - The root class which has an array of Account objects each for each <account> node in the configuration file.
  • Account - The representation of one account. Has an array of string objects for the keywords specified in the configuration file.

Processing

The processing is done the Program class. Using the IsAlreadyRunning property (which is implemented with a mutex) to quit itself if it is already running. So it is a single-instance console application.

Reason for that is that depending on the configuration file size, one run may take rather long and I wanted to avoid that the Windows Task Scheduler fire multiple instances. Twitter only allows a limited number of requests per IP address per day, so you better play nice here.

The Main method consists of two nested loops, the outer iterates through all Account objects of the configuration, the inner through all keywords of the currently processed account.

Using tweet# is interesting: They heavily rely on .NET 3.5 extension methods to implement most of the functionality. The basic steps are outlined in the online documentation of tweet#:

  1. Create a request of what you want to get and specify your account login credentials.
  2. Retrieve the reply from Twitter and further process the reply.

Example for step 1:

var twitter = 
    FluentTwitter.CreateRequest()
        .AuthenticateAs( account.UserName, account.Password )
        .Search()
        .Query()
        .Containing( keyword )
        .AsJson();
        
response = twitter.Request();

Here in this example, I authenticate an account with username and password, and then query for tweets with the given keyword. The result is then returned a string containing the JSON formatted result.

Example for step 2:

var searchResult = response.AsSearchResult();

The response is transformed into a search result collection. Here you have the ability to e.g. iterate through the results and further process them:

foreach ( var status in searchResult.Statuses )
{
    var userName = status.FromUserScreenName;

    // ... do something with the user name ... 

}

Depending on the kind of query, different object types are being returned. Again, please see the tweet# reference Wiki for full options.

Notes

Currently all output is written to console window only. Ususally next steps would be to use a logging framework like LOG4NET instead to log to various locations like e.g. e-mail or log files to get notified when something goes wrong.

Current state of the tool

The tool was a quick development of approximately 2 hours. It currently does exactly what I wanted it to do. Proably It does not even have any of the features that you want it to have.

Here you have at least two options:

  1. Download the source code and enhance it the way you want it.
  2. Tell me (down here in the comments section) which features are missing that I should add.

Of course option 2. would be helpful to me when enhancing.

As usual, I will enhance, extend and correct the tool over the next weeks and months. Keep the feedback coming!

History

  • 2009-11-06
    Updated to latest tweet# library.
     
  • 2009-06-29
    Added section that explains the internal code structure and some basic ideas behind the code.
     
  • 2009-06-29
    Added to Microsoft Codeplex and to Google code
     
  • 2009-06-28:

    Since at least four people (see their 1-vote comments below) seem to strongly dislike me and/or my article, let me summarize the motivation for this article:

    1. I searched Google for Twitter keyword auto-follow tools.
    2. I searched again.
    3. I found one tool, Twollow.com, which is usable only when paing money.
    4. I decided to write my own, as-fast-as-possible. So I used a third-party-library tweet#.
    5. I wanted to give others that want such a tool the opportunity to not start from scratch but to use my (thin) layer on top of tweet# as a foundation.
    6. I published the code as an article here at CodeProject.com.

    This said, I do know:

    • Neither the article nor the code is rocket science.
    • For me, this article is similar to all other articles I wrote, but somehow I must be mistaken.
    • I do love sharing and using code.
    • I also do love my company (really!) so I put hyperlinks to products of us in my profile section that appears under every article I write, not just this one.

    Thanks for reading!

  • 2009-06-26
    First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Offline-Homepage-Baukasten

Comments and Discussions

 
General5/5 - Thanks! [modified] PinmemberJJMPSP19-Mar-12 7:42 
GeneralTwitter! PinsitebuilderUwe Keim29-Jun-09 8:32 
I finally found the answer, why I made so much people angry with my article:
 
TWITTER!
 
Yes, it is this simple. Many people around here seem to dislike Twitter and related stuff a lot.
 
Multiple times I read Lounge postings like "I don't get that Twitter stuff" or "Twitter is for idiots with too much time on their hands".
 
So yes, herewith I tell you: I do use Twitter a lot and love it!
 
That said, I am aware of further 1 votes. I can stand it. Someone must be the brave guy bringing Twitter into the CodeProject.com universe. If it helps, I will be the one Smile | :) .
 
My personal 24/7 webcam
Zeta Test - Intuitive, competitive Test Management environment for Test Plans and Test Cases. Download now!
Zeta Producer Desktop CMS - Intuitive, very easy to use. Download now!

GeneralRe: Twitter! Pinmemberbobfox29-Jun-09 9:34 
GeneralRe: Twitter! PinsitebuilderUwe Keim29-Jun-09 9:39 
GeneralRe: Twitter! Pinmembermbaocha25-Aug-09 11:34 
GeneralMy vote of 1 PinmemberRoberto Collina29-Jun-09 5:18 
GeneralRequest a rework of the article PinmemberOne Touch29-Jun-09 1:15 
GeneralRe: Request a rework of the article PinsitebuilderUwe Keim29-Jun-09 1:31 
GeneralRe: Request a rework of the article PinsitebuilderUwe Keim29-Jun-09 9:04 
GeneralRe: Request a rework of the article PinmemberOne Touch29-Jun-09 13:25 
GeneralRe: Request a rework of the article PinsitebuilderUwe Keim29-Jun-09 18:49 
GeneralMy vote of 1 PinmemberSaurabh.Garg28-Jun-09 17:48 
GeneralInteresting comments PinmvpHans Dietrich28-Jun-09 10:03 
GeneralRe: Interesting comments PinsitebuilderUwe Keim28-Jun-09 17:54 
GeneralMy vote of 1 PinmemberRob Graham28-Jun-09 8:57 
GeneralMy vote of 1 [modified] PinmemberJoe Woodbury28-Jun-09 8:05 
GeneralRe: My vote of 1 PinsitebuilderUwe Keim28-Jun-09 8:31 
GeneralRe: My vote of 1 PinmemberJoe Woodbury28-Jun-09 9:01 
GeneralMy vote of 1 PinmemberAcoustic27-Jun-09 14:55 
GeneralMy reply [modified] PinsitebuilderUwe Keim27-Jun-09 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 6 Nov 2009
Article Copyright 2009 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid