Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Zeta Twitter

Rate me:
Please Sign up or sign in to vote.
3.88/5 (31 votes)
5 Nov 2009CPOL5 min read 61.7K   728   23   20
Auto Follow Keywords On Twitter

Image 1

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.

Image 2
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".

Image 3
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)


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
General5/5 - Thanks! Pin
JJMPSP19-Mar-12 7:42
JJMPSP19-Mar-12 7:42 
GeneralTwitter! Pin
Uwe Keim29-Jun-09 8:32
sitebuilderUwe Keim29-Jun-09 8:32 
GeneralRe: Twitter! Pin
bobfox29-Jun-09 9:34
professionalbobfox29-Jun-09 9:34 
GeneralRe: Twitter! Pin
Uwe Keim29-Jun-09 9:39
sitebuilderUwe Keim29-Jun-09 9:39 
GeneralRe: Twitter! Pin
mbaocha25-Aug-09 11:34
mbaocha25-Aug-09 11:34 
GeneralMy vote of 1 Pin
rcollina29-Jun-09 5:18
rcollina29-Jun-09 5:18 
GeneralRequest a rework of the article Pin
One Touch29-Jun-09 1:15
One Touch29-Jun-09 1:15 
GeneralRe: Request a rework of the article Pin
Uwe Keim29-Jun-09 1:31
sitebuilderUwe Keim29-Jun-09 1:31 
GeneralRe: Request a rework of the article Pin
Uwe Keim29-Jun-09 9:04
sitebuilderUwe Keim29-Jun-09 9:04 
GeneralRe: Request a rework of the article Pin
One Touch29-Jun-09 13:25
One Touch29-Jun-09 13:25 
GeneralRe: Request a rework of the article Pin
Uwe Keim29-Jun-09 18:49
sitebuilderUwe Keim29-Jun-09 18:49 
GeneralMy vote of 1 Pin
Saurabh.Garg28-Jun-09 17:48
Saurabh.Garg28-Jun-09 17:48 
GeneralInteresting comments Pin
Hans Dietrich28-Jun-09 10:03
mentorHans Dietrich28-Jun-09 10:03 
GeneralRe: Interesting comments Pin
Uwe Keim28-Jun-09 17:54
sitebuilderUwe Keim28-Jun-09 17:54 
GeneralMy vote of 1 Pin
Rob Graham28-Jun-09 8:57
Rob Graham28-Jun-09 8:57 
GeneralMy vote of 1 [modified] Pin
Joe Woodbury28-Jun-09 8:05
professionalJoe Woodbury28-Jun-09 8:05 
GeneralRe: My vote of 1 Pin
Uwe Keim28-Jun-09 8:31
sitebuilderUwe Keim28-Jun-09 8:31 
GeneralRe: My vote of 1 Pin
Joe Woodbury28-Jun-09 9:01
professionalJoe Woodbury28-Jun-09 9:01 
GeneralMy vote of 1 Pin
TylerBrinks27-Jun-09 14:55
TylerBrinks27-Jun-09 14:55 
GeneralMy reply [modified] Pin
Uwe Keim27-Jun-09 21:56
sitebuilderUwe Keim27-Jun-09 21:56 

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

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