Microblogging Twitter Script

This evening I thought to myself, wouldn’t it be cool to include my microblogging post (posts on Twitter) in my full blog? So I set about writing a script to fetch all of the previous day’s tweets (well all that are within the 20 pervious that the Twitter API supports of course) and copy them to the clipboard so that I can paste the HTML into my favourite blogging tool. I could have posted them straight onto my blog I suppose, but I’m not that confident in the Twitter API ; so for now, I’m happy to paste the HTML. Anyway, just in case the script is any use to you, I’ve posted it below. As always the code is optimised for a blog post not for production, so feel free to refactor to your hearts content 🙂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
using System.Windows.Forms;

namespace Microblogging {
    /// <summary>
    /// I am a class that represents a Twitter Status
    /// </summary>
    public class Status {
        private string _Text;
        public string Text {
            get { return _Text; }
            set {
                _Text = value;

        private string _Id;
        public string Id {
            get { return _Id; }
            set {
                _Id = value;

        private DateTime _CreatedDate;
        public DateTime CreatedDate {
            get { return _CreatedDate; }
            set {
                _CreatedDate = value;

        private string _Href;
        public string Href {
            get { return _Href; }
            set {
                _Href = value;

    /// <summary>
    /// I am a class that represents the Twitter API
    /// </summary>
    public class Twitter {

        private NetworkCredential credential;
        public Twitter(string uid, string pwd) {
            credential = new NetworkCredential(uid, pwd);

        /// <summary>
        /// I answer an IEnumerable of the latest 20 tweets
        /// </summary>
        /// <returns>IEnumerable<Status></returns>
        public IEnumerable<Status> GetRecentTwits() {
            using (
                //GS - Fetch the statuses
                WebClient client = new WebClient()) {
                client.Credentials = credential;
                try {
                    using (Stream stream = 
                                + "user_timeline.xml")) {
                        //GS - Instantiate a XDocument from the return
                        XmlReader reader = XmlReader.Create(stream);
                        XDocument document = XDocument.Load(reader);

                        //GS - Query the XDocument and return the statuses
                        return from status in document.Descendants("status")
                               select new Status() {
                                   Text = status.Element("text").Value,
                                   Id = status.Element("id").Value,
                                   CreatedDate = DateTime.ParseExact(
                                    "ddd MMM dd HH:mm:ss zzzzz yyyy", 
                                   Href = String.Format(
                catch (WebException ex) {
                    //GS - Display the error as returned by Twitter
                    using (Stream errorStream = 
                        ex.Response.GetResponseStream()) {
                        using (StreamReader reader = 
                            new StreamReader(errorStream)) {
                            string errorMessage = reader.ReadToEnd();
                    return new Status[0];

    class Program {
        /// <summary>
        /// Program entry point
        /// </summary>
        /// <param name="args">Commandline arguments</param>
        static void Main(string[] args) {
            //GS - Instantiate our Twitter API class
            Twitter twitter = new Twitter("uid", "pwd");

            //GS - Retrieve the latest 20 tweets as per the API
            var statuses = twitter.GetRecentTwits();

            //GS - Create a string to paste into our blogging tool
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<p>Recently I said</p>");
            foreach (Status status in statuses) {
                //GS - For blogging on a daily basis we only 
                //want yesterday's tweets
                DateTime startDate = 
                        new TimeSpan(1, 0, 0, 0));

                DateTime stopDate = DateTime.Now.Date;
                if (status.CreatedDate >= startDate && 
                    status.CreatedDate <= stopDate) {
                            String.Format("{0} "
                                + "(<a href='{1}'>More</a>)<p />", 
            builder.AppendLine("<p>Why not come and join the "
                + "conversation at <a href='http://www.twitter."
                    + "com/garyshort'>http://www.twitter.com/"
                        + "garyshort</a>");

            //GS - Copy the text to the clipboard to be pasted 
            //into our blogging tool
            Clipboard.SetDataObject(builder.ToString(), true);


Digg This
This entry was posted in C#, Developement. Bookmark the permalink.

9 Responses to Microblogging Twitter Script

  1. Peter says:

    What language is this in? I want to use this to get my tweets and paste them to facebook (I know I’ll have to make/find something to use the clipboard and do so).

  2. Gary Short says:

    It’s written in C#

  3. Fantastically written. This excellent article ended up being really
    enlightening. continue the awesome blogging.

  4. It’s really a great and useful piece of information. I am satisfied that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  5. Josefa says:

    Hello There. I found your blog using msn. This
    is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will definitely return.

  6. Helpful info. Lucky me I found your site by accident, and I am surprised why this twist of fate didn’t took place in advance! I bookmarked it.

  7. Anonymous says:

    Good day! I simply wish to give a huge thumbs up for the good info
    youve got right here on this post. I will be coming
    again to your weblog for more soon.

  8. Hi there to every one, since I am truly keen of reading this website’s post to be updated daily. It includes nice data.

  9. monavie says:

    Heya i’m for the first time here. I came across this board and I find It really helpful & it helped me out much.
    I hope to provide one thing again and help others like you aided me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s