The fusion of technology, business, and life

Archive for May, 2009

Now on Twitter

I love living on the edge with new technology, as long as it can improve my life in some way. Until recently, I wasn't very excited about Twitter, since I didn't think it served much of a purpose. After all, Facebook has "status updates" too, right? Recently, the hosting company I use for CompleteFusion experienced an electrical outage in their data center, and I thought Twitter might yield some helpful information since its "real-time search" technology is frequently hyped. Initially, I didn't know the reason for the outage, and wasn't able to reach the hosting company's site to find out any details because the outage had affected the whole data center. After a quick search on Twitter, I found out a few more details and began sharing information with several other people who were using the same host.

Since I found it useful, I've decided to start regularly using Twitter. For example, I'm now announcing new CompleteFusion posts on Twitter using Jeroen Boelle's twimp-wp, a WordPress plugin for Twitter and bit.ly.

No comments

Automating Exchange Rate Data

It is often useful to be able to retrieve and update exchange rates in an automated manner. Fortunately, the European Central Bank publishes its foreign exchange reference rates on a daily basis, and they're kind enough to provide an XML file with the data. Although the data uses the Euro as the base currency, we can easily recalculate the rates with US Dollars as the base currency.

For example, to get the current exchange rate for Euros to US Dollars, we can run the following:

1
curl --silent http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml | grep -e 'USD' | cut -d \' -f 4

As of May 22, 2009, the reference rate returned for US Dollars was 1.3972. To turn the USD per Euro rate of $1.3972/€ into Euro per USD (USD as the base rate), we simply take the inverse. To do so, we calculate 1/$1.3972, which equals €0.7157/$. To perform the calculation programatically and return the Euro per USD value:

1
echo "scale=4; 1/`curl --silent http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml | grep -e 'USD' | cut -d \' -f 4`" |bc -l

When calculating foreign exchange rates, one must consider the principle of arbitrage, which is expressed mathematically as A-to-C = (A-to-B)*(B-to-C). Therefore, GBP per USD = (GBP per EU)*(EU per USD). To perform the calculation programatically and return the GBP per USD value:

1
2
3
4
5
6
#!/bin/bash
USD_EU=`curl --silent http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml | grep -e 'USD' | cut -d \' -f 4`
GBP_EU=`curl --silent http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml | grep -e 'GBP' | cut -d \' -f 4`
EU_USD=`echo "scale=5; 1/$USD_EU" | bc -l`
GBP_USD=`echo "scale=5; $GBP_EU*$EU_USD" | bc -l`
echo $GBP_USD

For example, using the reference rates provided by the European Central Bank as of May 22, 2009, running our script to calculate the GBP per USD rate yields £0.62846/$.

No comments

Use Google Translate from the command-line with resty

As I mentioned in my last post (Access Google Translate API with PHP), it is fairly trivial to access the Google Translate API using PHP. I recently came across Micha Niskin's resty, a simple commandline tool to make REST requests, and decided to see how easy it would be to use with the Google Translate API.

Ultimately, it wasn't hard at all to use resty to run translations through the Google API. First, I had to download resty, which can be done as follows:

curl http://github.com/micha/resty/raw/master/resty > resty

Then I set the REST host:

resty http://ajax.googleapis.com/ajax/services/language

And created my GET request:

resty GET '/translate?v=1.0&langpair=en%7Ces&q=Test'

Finally, the response from Google:

{"responseData": {"translatedText":"Prueba"}, "responseDetails": null, "responseStatus": 200}

To include a referrer URL (which Google requires when you use the Translate API), just add the curl -e parameter with the referrer URL:

resty GET '/foo' -e http://blah
No comments

Access Google Translate API with PHP

Recently I wanted to utilize Google's translation API, so I came up with the following PHP script. Please note that I didn't build in any error handling, debugging code, security measures, or the full range of features of the Google translation API.. all of which you should probably do if you're going to use this in a public or production environment.

Create a new .php file on your web server, as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Basic request parameters:
// s = source language
// d = destination language
// q = Text to be translated
 
$s = $_REQUEST['s'];
$d = $_REQUEST['d'];
$lang_pair = urlencode($s.'|'.$d);
$q = urlencode($_REQUEST['q']);
 
// Google's API translator URL
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair;
 
// Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php");
$body = curl_exec($ch);
curl_close($ch);
 
$json = json_decode($body, true);
echo $json['responseData']['translatedText'];

Then just load up http://www.yoursite.com/translate.php?s=en&d=es&q=Test and you should see "Prueba" (Spanish for "Test").

8 comments

Debian Lenny apt-cacher reporting bug

With the apt-cacher package in Debian Lenny, my web interface for the cache statistics wasn't being properly updated. Eventually, I tracked down the problem and figured out that I needed to modify the following section of apt-cacher-report.pl to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
#parse logfile:
foreach $logfile_line (@logdata)
{
        #$logfile_line =~ s/ /\+/g;
        @line = split /\|/, $logfile_line;
        $req_date = $line[0];
#       $req_pid = $line[1];
#       $req_ip   = $line[2];
        $req_result = $line[3];
        $req_bytes  = 0;
        $req_bytes  = $line[4] if $line[4] =~ /^[0-9]+$/;
#       $req_object = $line[5];
No comments

Quick tip: Using sort on large files

When using the sort command on a 2GB file, with a small /tmp (mine was 16M at the time), it failed with the following error…

sort: write failed: /tmp/sort9tRGAQ: No space left on device

I briefly considered that I may need to expand my /tmp partition, until I consulted Google and found a much easier solution – make sort use an alternate location for its temporary files with the -T switch, as follows:

sort -T /path/to/alternate/tmp
No comments

Next Page »