Tuesday, September 28, 2010

De Nederlandse twitterpopulatie per 27 september 2010

Sinds afgelopen zaterdag is twitteraccount khtweets actief. Dit account wordt bijgehouden door de Rijksvoorlichtingsdienst en geeft met name informatie over Koningin Beatrix, de Prins van Oranje en Prinses Máxima. Dit account werd na de eerste 3 dagen dat het actief is reeds door meer dan 20,000 twitteraars gevolgd, waaruit blijkt dat er veel interesse is op Twitter rondom het reilen en zeilen van het Koninklijk Huis.

Interessant bij dit account is dat men aan mag nemen dat vrijwel al deze 20,000 volgers actief twitterende Nederlanders zijn. Als we vervolgens aannemen dat deze twitteraars een aselecte steekproef vormen uit de totale Nederlandse twitterpopulatie, kunnen we conclusies trekken over deze totale populatie. Indien de lezer van mening is dat dit onzinnig is, kunnen de volgende feiten en conclusies alleen gezien worden als geldend voor de volgers van khtweets.

De grafieken zijn verdeeld in groepen van 10% van de populatie, van laag naar hoog voor het betreffende onderwerp, met gemiddelde waardes per groep. Allereerst is gekeken naar de twitterleeftijd in dagen van de twitterpopulatie:


In bovenstaande grafiek zien we dat de gemiddelde leeftijd op Twitter inmiddels 380 dagen bedraagt. De grafiek loopt redelijk gelijkmatig op, waarbij te zien is dat de jongste 10% van de twitteraars gemiddeld een maand actief is, en de oudste 10% al gemiddeld bijna 2.5 jaar actief is op Twitter. Uit de grafiek valt af te leiden dat ruim de helft van de twitteraars inmiddels meer dan een jaar actief is. Een zeer kleine groep twitteraars is inmiddels meer dan 4 jaar actief.


Als naar het aantal volgers wordt gekeken valt op dat 80% van de twitteraars minder dan 100 volgers heeft. De hoogste 10% heeft een gemiddelde van 1,196 volgers. Binnen deze 10% zijn de uitslagen erg groot, daar er een beperkt aantal twitteraars is dat een zeer hoog volgers aantal heeft. Het gemiddeld aantal volgers over de hele populatie bedraagt 159.


Als gekeken wordt naar het aantal mensen dat men zelf volgt, valt op dat voor 90% van de twitteraars geldt dat men zelf meer mensen volgt, dan dat men gevolgd wordt. Een verklaring hiervoor zou kunnen zijn dat binnen een beperkte groep mensen elkaar terugvolgen, terwijl er ook altijd een aantal mensen worden gevolgd, die grote aantallen volgers hebben vanwege hun bekendheid of andere bepaalde specifieke kwaliteiten. Het totaal gemiddeld aantal mensen dat men volgt bedraag 156.


Gemiddeld is door de hele populatie een totaal aantal van 1,068 tweets per account verzonden. Uiteraard is dit aantal sterk afhankelijk van het dagelijks aantal tweets dat gestuurd wordt en het aantal dagen dat men reeds twittert. Duidelijk is wel dat er een beperkte groep twitteraars is, die zeer actief is. De volgende grafiek laat dit duidelijker zien.


In de grafiek is te zien dat het gemiddeld aantal tweets dat per dag verzonden wordt 2.6 bedraagt per account. Dit gemiddelde wordt echter bepaald door een relatief kleine groep twitteraars. 80% van de twitteraars verstuurt gemiddeld minder dan 2.3 tweets per dag, terwijl de meest actieve 10% van de twitteraars bijna 16 tweets per dag verstuurt. Ook binnen deze groep is de frequentie erg divers. Er zijn een aantal twitteraars die meer dan 100 tweets per dag versturen en dit al langere tijd volhouden.

Tot slot kijken we nog naar het moment dat door twitteraars hun laatste tweet verstuurd is:


Hier valt duidelijk te zien dat twitteraars erg actief zijn. 60% heeft niet langer dan 1 dag geleden een tweet verstuurd, terwijl 90% dit nog in de laatste 10 dagen heeft gedaan. De laatste 10% heeft gemiddeld 90 dagen geleden hun laatste tweet verstuurd, maar heeft dus wel de laatste dagen ingelogd op hun account. Er is blijkbaar een grote groep die vooral andere accounts volgt, zonder zelf erg actief tweets te versturen.

Tenslotte willen we nog een gewaagde voorspelling doen omtrent het aantal actieve Nederlandste twitteraccounts. Deze voorspelling doen we onder de aannname dat de totale Nederlandste twitterpopulatie zich hetzelfde gedraagt als het aantal twitteraars dat door ons gevolgd wordt in de Twopcharts. In deze Twopcharts worden op dit moment 8,738 twitteraars gevolgd. Dit zijn allemaal actieve Nederlandstalige accounts die minimaal 350 volgers hebben en/of account nl_twop_1000 volgen.
Van deze groep twitteraars volgt inmiddels 13.3% khtweets. Als we derhalve aannemen dat de eerste 20,000 volgers van khtweets ook 13.3% van de Nederlandse twitterpopulatie vormen komen we op ruim 150,000 actieve Nederlandse twitteraccounts

We gaan er dan voor het gemak vanuit dat het aantal Nederlandse twitteraars dat vooral Engelstalig tweet (en niet in de Twopcharts staat, maar wel Nederlands is) ongeveer gelijk is aan het aantal Belgisch / Vlaamse twitteraars (die wel in de Twopcharts staan, maar niet Nederlands zijn).

Monday, September 27, 2010

Using Excel as Twitter client with OAuth

Many users of twitter have found out that Excel is an excellent client for Twitter, and I am sure that through this post on Chandoo.org, a lot of curious people opened their VBA editor, tried it, and subsequently posted their first automated tweet. For me this was just the beginning, and after studying the Twitter API documentation, I wrote functions for just about all API methods. Using Excel proved to be great for all kinds of tasks, like scheduling tweets, sending tweets for different accounts from one dashboard, archiving tweets, mentions and direct messages, and many more bigger or little tasks.

The reason that it was so simple to use Excel was because of the use of basic authorization where with just a username and password it was possible to “talk” to the Twitter API. Unfortunately Twitter has switched off Basic Auth since the beginning of September, and many Excels sheets that required authenticated requests, will no longer function. Since transitioning from Basic Auth to OAuth looks complicated, many users of Excel sheets may have given up, and accept that their work is now lost. I also started and stopped looking at the documentation a couple of times, until I finally decided it just had to be possible to make it work. And indeed after a couple of days of hard work (and plenty of frustration) I was able to transition to OAuth. This means that I can take all my Excel sheets and just change the VBA code where the API requests are made, leaving everything else intact.

I will explain here how I did it, so hopefully other Excel users can take advantage of my efforts. I will use a straight forward example, and if you have some understanding of VBA, you should be able to follow the process and later take the code and optimize it for your own needs. In the example I will build an Excel sheet that allows me to send a tweet, going through the process step by step. You can see all the VBA code I used in the Excel sheet which you can download for your own programming pleasure.....

Step 1: Register an application with Twitter

There is no way around this. In order to use OAuth you need application codes and user codes. By registering an application with Twitter you will obtain a consumer key and a consumer secret. For your Twitter account you will get an access token and an access token secret. You will need these four codes before you can continue.

Step 2: Get all required data

For our example the following information is required:

API method = POST
API resource = http://api.twitter.com/1/statuses.update.xml
Oauth consumer key = your_consumer_key
Oauth consumer secret = your_consumer_secret
Oauth signature method = HMAC-SHA1
Oauth version = 1.0
Oauth token = your_token
Oauth token secret = your_token_secret

The API method and resource relate to what you want to do, which in this case is to send a tweet. The signature method and version are given as per the current requirements.

Step 3: Get a nonce (number used once) and a timestamp

You can calculate this yourself by writing a couple of simple functions. The timestamp simply calculates the number of seconds passed since the unix epoch which seems to be January 1, 1970. For the nonce I observed in the Twitter examples that it is 41 characters long and only uses letters and numbers. I just created a string which holds all possible and legal characters. Then I randomly picked a character 41 times from this string and put them all together in an output string which becomes the nonce. Twitter seems happy with that solution. A shorter nonce will probably also be ok, as long as it is unique.

Step 4: Calculate the base string

This is where thing started to get ugly, because it is so easy to do it wrong, and it is not so easy at first to figure out what went wrong. The base string is used to calculate a signature which Twitter requires as part of the API request. If you follow the instructions from Twitter exactly, you should get the correct signature. Important is to keep in mind that there are three parts connected with an ampersand (&), where the second and third parts need to be url encoded. While I was testing my output, I found a very useful tool on http://quonos.nl/oauthTester/ to check the base string.

Step 5: Calculate composite signing key

In this case, where you want to send a status you need to create a string, where your oauth_consumer_secret and oauth_token_secret are combined through an ampersand (&). No url encoding is required here.

Step 6: calculate oauth_signature

After mastering the base string, this was the next part which proved to be tricky. First I needed to understand (a little) what a HMAC-SHA1 signature is and than figure out a way to calculate it. Luckily I found all I needed on http://www.cryptosys.net/, which has a library that you can include as a VBA module. Make sure you do the installation, because there is a DLL file required, and please also check the license document.
When I calculated the signature, it did not match the required signature. The issue was that the obtained signature needed to be converted to bytes first, than base64 encoded and subsequently url encoded. I already had the code for the url encoding but did not have a function to perform the other two tasks. I wrote a simple function to convert the output string to bytes, and also found the correct answer for the base64 encoding. When all this finally worked, I had my moment of joy when the signature was exactly identical to the one calculated in the example in the twitter documentation.

Step 7: calculate authorization header

Creating the authorization header is quite similar to creating the base string. No url encoding is done here, and besides reading the Twitter documentation, I also carefully read Appendix A.5. from the Oauth Core 1.0 document.

Step 8: Send the API request

Finally everything has to be brought together in an API request, which look very similar to the one you probably used before, when basic auth was still working. In my Excel sheet it looks like this:

Set xml = CreateObject("MSXML2.XMLHTTP")
xml.Open cApi_method, cApi_resource & "?status=" & cStatus, False
xml.setRequestHeader "Authorization", cHeader
xml.Send
tResult = xml.responsetext
Debug.Print tResult
Set xml = Nothing

And that's all there is to it!

Please note that I my goal was to get to my required end result, which was to send a tweet. I have not attempted to create the most efficient and robust code and process to get there. I am convinced that there are far more talented programmers out there, who will be able to take this code and improve it. Please go ahead and share this with the rest of us.....

You can download the Excel sheet I created here.