randys.org

Wasting your precious bandwidth since 1999

Archive for January, 2007

Photo: Duck Grass

Duck Grass

Random photo from flickr. A duck sleeping at the San Diego Zoo

• • •

Scrobble This: last.fm recent tracks AJAX style

So, I’ve been reading up a bit on prototype.js and its Ajax helpers. It’s an amazing tool and helped me write the bit of info at the top of the page. It’s pretty basic, but here’s the code that does most of the work:

function lastfm()
{
    new Ajax.Request('/as/recenttracks.xml',
    {
        method: 'get',
        onLoading: function() {

        },
        onLoaded: function(transport) {
            if (transport.overrideMimeType) {
                transport.overrideMimeType('application/xml');
            }
        },
        onSuccess: function(transport) {
            var response = transport.responseXML.documentElement;
            updateLastfm(response);
        },
        onFailure: failedLastfm()
    });
}

The only issue I ran into was that I was originally using the RSS flavor of recent tracks, however it didn’t split up the artist and track information. It displays it as <title>[artist] – [track]</title>. That en-dash in the middle was preventing me from using title.split() on the JavaScript side of things. Really weird.

Also, since I’m a complete newbie with Ruby, I couldn’t figure out how (read: didn’t take the time to learn) to grab the content from a remote server and serve it up to the JavaScript. I’m sure it’s pretty simple… but I was at work and in a hurry. So, being that I know PHP, I just created a script to download the file and save it to the local disk and setup a cronjob.

Here’s the PHP script:

class lastfm
{
    private $user;
    private $reports;
    private $basews;
    public $saveto;

    function __construct($user)
    {
        $this->user = $user;
        $this->basews = 'http://ws.audioscrobbler.com/1.0/user/';
        $this->reports = array(
            'recenttracks.xml',
            'weeklyartistchart.xml',
            'weeklytrackchart.xml',
            'topartists.xml'
        );
        $this->saveto = '.';
    }

    public function go()
    {
        for ($i = 0; $i < count($this->reports); $i++)
        {
            try
            {
                $opts = array('http' => array('method' => 'GET', 'header' => 'Content-type: text/plain; charset=utf-8'));
                $context = stream_context_create($opts);

                $fp = fopen($this->saveto.DIRECTORY_SEPARATOR.$this->reports[$i], 'w+');
                $stream = fopen($this->basews.$this->user.'/'.$this->reports[$i], 'r', false, $context);
                $string = stream_get_contents($stream);
                fwrite($fp, $string);
                fclose($fp);
                fclose($stream);
                echo 'Saved ' . $this->saveto.DIRECTORY_SEPARATOR.$this->reports[$i] . "\n";
            }
            catch (Exception $e)
            {
                echo $e->getMessage() . "\n";
            }
        }
    }

    public function setSaveto($path)
    {
        if (is_dir($path))
        {
            if (ereg('\/$', $path))
            {
                $path = ereg_replace('\/$', '', $path);
            }
            $this->saveto = $path;
        }
        else
        {
            $this->createDir($path);
            $this->setSaveto($path);
        }
    }

    private function createDir($path)
    {
        if (!is_dir($path))
        {
            $res = `mkdir -p $path`;
        }
    }
}

I’ll hit up the Rails API Docs one of these days and write a simple Ruby script that does all the work of the PHP script. Even better would be to process the XML document using ruby that just returns a string of HTML and use Ajax.PeriodicalUpdater($(e), ...).

• • •

Photo: The Cats Eye

The Cats Eye

This is Mojo. He enjoys used Q-tips and other various items from the bathroom garbage can. He’s also a sadist.

• • •

Photo: Cat Fight (Basil vs. Mojo)

Cat Fight - 0255

Mojo was incapacitated after the right hook to the noggin. He hasn’t been the same since.

• • •

Photo: Echo

Echo

This is Echo. He is an Irish Wolf Hound stuck in a Chihuahua’s body. He enjoys (trying to) tackle his big sister and general mayhem. I like to call him Little Napoleon… or snaggle tooth.

• • •

Photo: Muggz

Muggz

This is Muggzy. He likes frozen soy sausages and knows how to open the freezer. He also likes McShitty Poo Biscuits and frequently humps beagles.

• • •

Photo: Catseye

This is Mustafa. He’s a cat. He likes long trek’s away from home and canned cat food.

Testing out flickr’s blog posting capabilities. Seem to work so far. I had to upgrade Typo in the process (to the svn trunk). ow my flickr side bar doesn’t work, which sucks… ah well. I’m tired.

Update OK, got the flickr sidebar working again. They completely moved the sidebar/plugin stuff to a different location… but, I found it.

• • •

Changing Permalinks In Typo

When I switched from Wordpress to Typo, I faced the issue of keeping my Wordpress permalinks in Typo. Unfortunately, there’s no simple way of doing this. I noticed the ‘redirects’ table in the database, but I could never get it to realy do anything. So, I had to dig in and find the bit of code that controlled the permalinks in Typo.

Typo uses ‘articles’ as it’s base for all posts so I looked in config/routes.rb and found what I needed to change. As you can see from the url of this post, I use ‘content’ as the base of my permalink. So, I changed all the references of ‘articles’ to ‘content’ and all seems to be right in the world.

I changed

1
2
map.connect 'articles',
    :controller => 'articles', :action => 'index'

to

1
2
map.connect 'content',
    :controller => 'articles', :action => 'index'

and then changed

1
2
3
map.connect 'articles/:year/:month/:day',
    :controller => 'articles', :action => 'find_by_date',
    :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/

to

1
2
3
map.connect 'content/:year/:month/:day',
    :controller => 'articles', :action => 'find_by_date',
    :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/

and so on.

The only thing I haven’t gotten to work is to map the pages. I can change the ‘pages’ map, but it doesn’t act as desired. I only had 3 pages in Wordpress, so I might as well either alias them or premanent redirect in the Apache configuration.

If anybody has some insight, I’d love to here it. Also, still haven’t figured out why mod_rewrite is working like it should.

• • •

Apache2: mod_rewrite vs. mod_proxy

I can’t quite figure this out. I’m trying to write a simple rule to force randys.org to redirect to www.randys.org but it’s not realy working. Of course, I haven’t tried it on a vhost that isn’t proxying request to mongrel. Here’s the rule:

RewriteEngine On
RewriteCond %{HTTP_HOST}    !^www.randys.org$ [NC]
RewriteCond %{HTTP_HOST}    !^$
RewriteRule ^/(.*)          http://www.randys.org/$1 [R=301,L]

Anyone?

• • •

Typo, Apache & Mongrel, Oh My!

Wow. I’ve been testing the possibility of running Typo on my VPS with the pre-notion that it just wouldn’t work very well. I think I may be wrong. It’s been running for about a week while I tweak some shit and so far, my memory performance has been stable. Of course, thing would probably be different if I threw some real traffic at it… but, then again, I don’t get a lot of that.

The Setup

Initially, I was going to use lighty + fastcgi to serve up the Rails application. So, I had setup lighty to serve up my PHP/MySQl sites and that worked just fine. Then I started reading up on Mongrel and wanted to see what all the hubub was about. But then I got to reading the Mongrel site (more specifically the part where he says don’t use lighty and mod_proxy).

Crap.

Aight, so back to Apache and their proxy setup. no biggie. I racked my brain for several hours one night trying to figure out why the hell I kept getting these 403 Forbidden errors in Apache. Hours. After the millionth Google search, I finally found the issue: The mod_proxy configuration in Ubuntu is turned off by default (well, not really turned off… it just denies traffic to the proxy server).

 8     <Proxy *>
 9         Order deny,allow
10         #Deny from all
11         Allow from .randys.org
12     </Proxy>

Line 8 was initially not commented out. I had to comment that out and add the Allow from .randys.org bit.

Once I got that changed and reloaded Apache, everything is working nicely together.. even PHP.

The Tweaks

So far, the only tweaks I’ve managed to make is to the flickr Sidebar plugin. It now uses Lightbox V2. That was a bit tricky, considering I don’t really know Ruby but, it all worked out in the end.

As I figure out more stuff to do, I’ll post more.

• • •

All content Copyright © Randy Sesser | Hosted by WebFaction
Entries (RSS) | Comments (RSS)

randys.org is Digg proof thanks to caching by WP Super Cache!