Posts Tagged ‘Silly Code’

Twitter Friend Feed Authentication

October 30th, 2008

I’m a twitter n00b. I joined up a month or so ago just to try it out, and occasionally i post something technology or work related. Turns out that I joined up right after they disallowed being able to subscribe to your “friends feed” feature without authenticating first. This is naturally a PITA because I use Google Reader, and it doesn’t pass down any credentials for me. Shit.

So my first reaction was to write a script in PHP which proxys the request on my behalf and authenticates me at the same time (hey it is silly-code Thursday after all). And that’s what this post was meant to be about. I was planning on blogging about my Thursday night somewhat learning PHP over again. In case you care, the code and links will come later.

As I was writing this to look up some references, I stumbled across this page which describes how you can use Yahoo Pipes to authenticate to Twitter and achieve the same result. What took me a few hours to learn and do in PHP, took me seriously less than 10 minutes using Yahoo Pipes. Absolutely remarkable. I’ll be spending more time playing around with that, now!

So if you care, the PHP version looked something like this:

<?php

include("HttpClient.class.php");

$client = new HttpClient('twitter.com');
//$client->setDebug(true);
$client->setPersistReferers(false);
$client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
$reqHeaders["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$reqHeaders["Keep-Alive"] = "300";
$reqHeaders["Connection"] = "keep-alive";
$client->setRequestHeaders($reqHeaders);

if (!$client->get('/statuses/friends_timeline/16832429.rss')) {
    die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
$requestArray = array_values(explode(';', $client->getHeader('Set-Cookie')));
$cookieArray;

$split = explode('=', $requestArray[0]);
$cookieArray[$split[0]] = $split[1];

$client->setCookies($cookieArray);
$client->setAuthorization('username', 'password');
if (!$client->get('/statuses/friends_timeline.rss')) {
    die('An error occurred: '.$client->getError());
}
echo $pageContents = $client->getContent();

?>

I used the HttpClient class to make the requests, which was the first result that came up in Google. Turns out this code hasn’t been updated since 2003 (incidentally that’s around the same time when I last did PHP), and there was a bug that the Authorization request header came AFTER the Cookie header, which (as it turns out) will be rejected by the webserver with a 401. That oddity alone took me an hour to figure out. *sigh*

Happy Birthday (The C-Sharp way)

October 23rd, 2008

This evening I wanted to wish someone a happy birthday. In my infinate geekdom, I figured writing a tiny app to do it would be a nice way to waste 20 mins this evening. I started off writing in Notepad (because this really isn’t worth opening Visual Studio for), and compiled using CSC jsut to make sure it worked in the end (oops, forgot a few semi-colons…)

using System;

public class BirthdayWisher
{
    public BirthdayWisher(string personName, DateTime birthDate)
    {
      this.PersonName = personName;
      this.BirthDate = birthDate;
    }
    public readonly DateTime BirthDate;
    public readonly string PersonName;

    const string BirthdayMessage = 
        "rnDear {0}! Wishing you a happy birthday for {1}. Congratulations on being {2} years old!";

    public void WishBirthday()
    {
      Console.WriteLine(String.Format(BirthdayMessage, PersonName, BirthDate.ToString("MMMM dd"), DateTime.Today.Year - BirthDate.Year));
    }
}

public class NaomiBirthday
{
  [STAThread]
  public static void Main(string[] args)
  {
    BirthdayWisher naomi = new BirthdayWisher("Naomi", new DateTime(1983, 11, 21));
    naomi.WishBirthday();
    
    Console.WriteLine("rnPress any key to end.");
    Console.ReadKey();
  }
}

So i sent her an IM message with that code and the following instructions to run this:
1. Copy the text above and save it to your desktop as a file called “birthday.cs”
2. Click “Start –> Run” and paste the following into the box and then press enter.

%windir%system32cmd.exe /c “%WINDIR%Microsoft.NETFrameworkv2.0.50727csc /out:”%USERPROFILE%birthday.exe” “%USERPROFILE%desktopbirthday.cs” > null&&”%USERPROFILE%birthday.exe”&& del “%USERPROFILE%birthday.exe” ”

Yes, i am a massive geek, but now I can re-use the same code for someone else’s birthday – yay for reuse! The harsest part about this whole thing wasn’t the code, but in the command-prompt syntax required to chain commands properly and get it to execute as i’d expect. Even after spending maybe 30 mins on it, I don’t think I got it right, and certianly it could be improved upon….I guess that leaves room for Happy Birthday v2 :)

LINQ Raytracer written in 1 statement.

August 14th, 2008

OMFG.

I think that just about sums up the Fully LINQified Raytracer….I missed this when it hit the blogosphere back in October last year, but wow i’m just gobsmacked….

I found this while reading about Mono’s 100% compliance with .NET 3.0

I feel so suddenly inadequate in what I do…..

LOLCode is real!

July 7th, 2008

Noone believed me when I introduced them to the Shakespearean programming language, or the whitespace programming language.

Well now believe me when I say that LOLCode is a true language! It allows you harness the power of a thousand teenage script-kiddies, and emanate elegant code such as :

HAI
CAN HAS STDIO?
I HAS A VAR
GIMMEH VAR
IZ VAR BIGGER THAN 10?
YARLY
BTW this is true
VISIBLE “BIG NUMBER!”
NOWAI
BTW this is false
VISIBLE “LITTLE NUMBER!”
KTHX
KTHXBYE

FINALLY we can get back to real programming…..

Head-Tracking for Virtual 3D Audio Environments – Part 1 (Research)

June 16th, 2008

Inspired by a colleague at work who was required to research an area of audio technology as part of his audio engineering degree, i’ve been dabbling over the last few weeks with a Nintendo Wii Remote and DirectSound.

Basically, what he needed was a piece of software which could simulate a audio-centric virtual environment. The audio simulation would involve a sound being modified based on the head-orientation of the user. If the user looks to the left, the sound “appears” to come from the right side of the head, and the same vice-versa. In order to perform the head-tracking we were going to rely on the capabilities of the Wii remote and all it’s IR camera glory. As with all clients, there really wasn’t anything much clearer specified, and I was left to kind of work out the rest ;)

The general concept behind the head-tracking is to use two points-of-reference on the user’s head to determine which side is left/right. Just like Johnny Chung Lee had done with his head-tracking exercise. With a little nudge forward from my old high-school electronics days, and $10 at JayCar, I’d soldered and constructed 2 purpose-built head-mounted IR LEDs. Blu Tack is an amazing product, and there’s nothing I found it won’t stick to. I won’t go into the detail of making one of these. It took me 20 mins, and the following article on making an IR LED pen was sufficient enough. As a tip, to check if your IR light is working correctly, you can use a digital camera and point the IR light toward the camera. Our eyes can’t pick up infra-red light, but the CCD on a camera can.

Post-hardware construction, the first phase of this project basically entailed a proof of concept, and an opportunity for me to learn about the two major components of this project i knew close to nothing about:

  1. Connecting and using the the Wii remote on a PC
  2. Using DirectSound to control audio

But first there would be more pressing matters. Like getting my Soleil Bluetooth receiver to work under VirtualBox on Linux. I don’t run any development software under my windows-boot partition. My windows partition has one intended purpose….To cut a long story short, it looked like it would be a bigger hassle trying to get the BT dongle to work under linux than to just install VS in my gamebox partition…So that’s that.

The BlueSoleil software worked like a charm, and within minutes I was hooking up the Wiimote to the laptop. no problems. The Wiimote is detected as just another HID, so it pretty much worked out of the box on XP.

At this point, I went out to find *how* to actually do each of the individual unknowns for this project. And it’s at this point I need to credit two articles which pretty much gave me everything I needed to get going. I was fortunate enough to find this article on CodeProject on using DirectSound, and an MSDN blog with a managed library for controlling the Wiimote. Too easy.

Hooked up the sample Wiimote library, turned on the Wiimote, turned on my IR lights and voila – worked a treat.

And that’s it for this post….next post, i’ll describe in a bit more detail the putting together of the software – certainly more interesting than all this drivel – and with pictures…..