September 16, 2008
Recently I said
@Karenyo should know better, if she "England" when she means "UK" one more time, I swear I’ll… tut and roll my eyes (More)
dear ALT.Net community, go to your rooms ’til you can speak to one another in a civil manner. KThnxBye (More)
@Stooshie yes Leuchars was fab, would have been better if the Vulcan had flown but can’t have everything – or so they tell me
(More)
@Stooshie very cool, good for you (More)
@Stooshie Cool, congrats. What will you be doing, anything exciting? (More)
blog: CERN Hack Attack – http://tinyurl.com/5nbls9 (More)
Why not come and join the conversation at http://www.twitter.com/garyshort
Leave a Comment » |
Microblogging |
Permalink
Posted by garyshort
September 16, 2008
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 =
client.OpenRead(
"http://twitter.com/statuses/"
+ "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(
status.Element("created_at").Value,
"ddd MMM dd HH:mm:ss zzzzz yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal),
Href = String.Format(
"http://twitter.com/garyshort/statuses/{0}",
status.Element("id").Value)
};
}
}
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();
Console.WriteLine(errorMessage);
}
}
return new Status[0];
}
}
}
}
class Program {
/// <summary>
/// Program entry point
/// </summary>
/// <param name="args">Commandline arguments</param>
[STAThread]
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 =
DateTime.Now.Date.Subtract(
new TimeSpan(1, 0, 0, 0));
DateTime stopDate = DateTime.Now.Date;
if (status.CreatedDate >= startDate &&
status.CreatedDate <= stopDate) {
builder.AppendLine(
String.Format("{0} "
+ "(<a href='{1}'>More</a>)<p />",
status.Text,
status.Href));
}
}
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);
}
}
}
2 Comments |
C#, Developement |
Permalink
Posted by garyshort
September 16, 2008
Sometimes this country drives me mad.
Almost 50,000 Gurkhas have died fighting for Britain since 1815 and 150,000 have been seriously injured while serving.
Gurkhas are received at Downing Street before handing in their petition
Yet thousands of veterans who retired after July 1, 1997 have been refused visas and have been forced to live in poverty in their homeland of Nepal.
Hundreds of badly injured soldiers have also been barred from travelling here for treatment.
More
After serving for four years or more (like servicemen and women from other Commonwealth countries) Gurkhas should be allowed to stay in this country. It’s a no brainer. It’s not something these guys should have to take to the High Court. Shame on you Gordon Brown!
Technorati tags:
Gurkhas,
High Court
1 Comment |
Personal |
Permalink
Posted by garyshort
September 16, 2008
Well not quite, that would be what we call hype; but, truthfully, AutoCollage 2008 is a great product. Here’s a collage I made after my trip to the Leuchars Airshow last weekend.
Technorati tags:
AutoCollage 2008
Leave a Comment » |
Technology |
Permalink
Posted by garyshort