Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

Sun Java 6 on Ubuntu Jaunty

Yes I know I am supposed to be upgrading to Karmic; I already downloaded the ISO images but I don't have the time this week or next.

Since I am on a liberal local cap, I thought I might as well just install all the Java 6 packages except the documentation, as that needs to be handled separately.

sudo aptitude install sun-java6-? sun-java6-doc:

Then you have to make sure you have the JAVA_HOME environment variable set up correctly:

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.16

(Just check that dir, it might be a bit different on your system.)

Zoopy PHP CLI Upload

I recently had quite a number of pictures I wanted to upload to Zoopy and was looking for an easier way to upload an entire directly of images straight from the command line. I ended up writing the following PHP script and making use of their RESTful API which ended up working fairly nicely.

#!/usr/bin/php
<?php

$username = 'charlvn';
$password = 'test123';

if (!isset($_SERVER['argv'][1])) {
    echo "Please enter a title.\n";
    exit(1);
}

$dir     = new DirectoryIterator(getcwd());
$userpwd = sprintf('%s:%s', rawurlencode($username), rawurlencode($password));

foreach ($dir as $item) {
    if ($item->isFile()) {
        echo sprintf('Uploading %s... ', $item->getBasename());

        $params          = array();
        $params['file']  = '@' . $item->getRealPath();
        $params['title'] = $_SERVER['argv'][1];

        if (isset($_SERVER['argv'][2])) {
            $params['tags'] = $_SERVER['argv'][2];
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, 'http://api.zoopy.com/rest/media/upload.json');
        curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        $response = curl_exec($curl);

        if (is_string($response)) {
            $data = json_decode($response, true);

            if ($data['result'] == 'success') {
                echo "Done\n";
            } elseif ($data['result'] == 'error') {
                echo sprintf("Failed\n%s\n%s\n", $data['content']['title'], $data['content']['message']);
                exit(1);
            } else {
                echo sprintf("Failed\nUnknown response from server:\n%s\n", $response);
                exit(1);
            }
        } else {
            echo "Failed\nAn unknown error occurred.\n";
            exit(1);
        }
    }
}

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.