Currency Conversion with Javascript Iteration

Missing that handy currency/country conversion link on your favorite page? A quick bookmarklet can fix that.

We will put together some javascript to iterate over our currency elements to do the conversion for us. In this case, we are looking at the bicycle site road.cc and we wish to convert review prices from Pounds to US Dollars.

Screenshot 2013-12-29 22.55.21
road.cc reviews before conversion (http://road.cc/show/review-section/road-bikes/35)
Screenshot 2013-12-29 22.55.31
road.cc reviews after conversion (http://road.cc/show/review-section/road-bikes/35)

 

 

 

 

 

 

 

 

 

We first select all of our data of interest. In this case, our data is in a div with a specific class that merely contains the price.

var data = document.getElementsByClassName("review-price");

Next we will iterate over this data and convert the price using a decent approximation based upon today’s conversion rate. However, we have two considerations; the price div also contains the Pound character which we will first cut out before converting with the replace call and we will also want to round to two decimal places for sanity.

for (var i = 0; i < data.length; i++) {
   data[i].innerHTML = Math.round(data[i].innerText.replace('£', '') * 1.6479 * 100) / 100;
}

If you try and run this, you may notice one caveat – road.cc uses infinite scroll. This means that we will convert material that is currently loaded, however, upon scrolling down and with the loading of additional content, we would have to run the bookmarklet again to convert this newly loaded data. The problem is that we would convert our previously adjusted price data again. To prevent this issue, we can modify our code to convert data that begins with the Pound but not Dollar symbol.

function () {
    var data = document.getElementsByClassName("review-price");
    for (var i = 0; i < data.length; i++) {
        /* change class so we don't suck it up again since we have to re-run the bookmarklet
       again because of infinite scroll
      */
        if (data[i].innerHTML.indexOf("£") != -1) {
            data[i].innerHTML = '$' + Math.round(data[i].innerText.replace('£', '') * 1.6479 * 100) / 100;
        }
    }
}

Now, remember, we wrap this in a javascript declaration to create our bookmarklet. Thus, the final version will result in the following when we place our pieces of code together.

javascript: (

function () {
    var data = document.getElementsByClassName("review-price");
    for (var i = 0; i < data.length; i++) {
        /* change class so we don't suck it up again since we have to re-run the bookmarklet
       again because of infinite scroll
      */
        if (data[i].innerHTML.indexOf("£") != -1) {
            data[i].innerHTML = '$' + Math.round(data[i].innerText.replace('£', '') * 1.6479 * 100) / 100;
        }
    }
}())

Running the bookmarklet should now convert currency data to US Dollars which works with infinite scroll by re-clicking the bookmarklet as new data is loaded.

Cook your RAW photos into JPEG with Linux

NOTE: This is a ported post from the old Mutaku site. Original post information:
Thursday, August 14 2008 @ 04:50 PM EDT Contributed by: xiao_haozi

Shooting in RAW format is great for the photography buff. However, when it is time to share with others, post on your photo gallery, or print at the local photo printing shop, you want something more portable. Here we’ll look at converting RAW photos (specifically Canon’s CRW format) into JPEGs using a batch command-line approach in BASH.

We will be using two tools to do our batch conversion:

1- DCRAW (available here)

2- CJPEG (available here)

CJPEG is part of the standard JPEG library (libjpeg) so you should already have that installed. If not you can find the source here and built as such:

./configure --enabled-shared
 make 
 make install (as root, e.g. sudo make install)

DCRAW is available here and can be built as such:

wget http://www.cybercom.net/~dcoffin/dcraw/archive/dcraw-8.87.tar.gz
tar xzvf dcraw-8.87.tar.gz
cd dcraw-8.87
./configure
make
make install (as root, e.g. sudo make install)

Now that we have the prerequisites out of the way, we can try it out. We’ll manually do the conversion to ensure it works. Make sure you then compare the images and ensure they meet your needs/desires with the settings we use below.

In our example we will assume we have a directory called trip08 on our user’s desktop that contains some CRW raw images as well as others. We will go into this directory, make a CRW directory to copy our RAW images to for converting, search here and all subdirectories for CRW files and copy to our new directory, and then convert them using the two tools above so we will have in our new directory a copy of the original RAW CRW image as well as a new JPEG version.

Here we go:

cd ~/Desktop/trip08 &amp;&amp; mkdir CRW_conversions
find . -name \*.[Cc][Rr][Ww] -exec cp "{}" CRW_conversions/ ";"

First, we went into our trip photo directory and made a directory called ‘CRW_conversions’ that we will work within. Next, we looked in the current trip directory (and subdirectories) for all files that end in ‘.crw’ including and variations on capitalization. The files we found are transfered to our CRW_conversions directory.

cd CRW_conversions
for i in *; do dcraw -c "$i" | cjpeg -quality 100 -optimize -progressive > $(echo $(basename "$i" ".crw").jpg); done

Now we went into our CRW_conversions directory that contains copies of our RAW images. We then constructed a loop which:

1- for i in *; do for each element in this directory do the following
2- dcraw -c “$i” | use dcraw to decode the element and pipe it out
3- cjpeg -quality 100 -optimize -progressive > accept the pipe from dcraw with cjpeg and convert to 100% quality jpeg with optimization and progressive processing and output that
4- $(echo $(basename “$i” “.crw”).jpg) take that output and put in a file called element.jpg where element is the name of the original CRW version without the extension ‘.crw’
5- ; done finish the loop once the iterations are done

We should now have a directory called CRW_conversions in our trip08 directory that contains copies of all our RAW images as well as fresh versions of each in JPEG format. The settings we used to convert have given us fairly high quality images that are still relatively large, so you may wish to alter these to your liking. You can do so by altering the CJPEG part of the conversion and find the options here:

man cjpeg

If you find that you have errors of ‘Empty input file’ and resultant JPEG files that are 0 bytes in size, you most likely have left out the ‘-c’ switch in your DCRAW part. This is needed to send the output with the pipe, or else you will end up with PPM files.

You can place most of this code into a shell script to do the brunt of the work for you without having to type in each time. We are currently working on a Python version which uses Tkinter to provide a GUI interface as well as options to convert and manipulate several image formats and will post source code and a how-to once it is finished. However, if you want to try out a shell version now you might want to try something like:

#!/bin/env bash
#######################################
# crw2jpg                                                           
#      convert canon raw into jpeg                       
#######################################
mkdir CRW_conversions
find . -name \*.[Cc][Rr][Ww] -exec cp "{}" CRW_conversions/ ";"
cd CRW_conversions
for i in *; do dcraw -c "$i" | cjpeg -quality 100 -optimize -progressive > $(echo $(basename "$i" ".crw").jpg); done
print "CONVERSIONS COMPLETE"

This is a very basic script without any real error control or logic, so add to it as you wish/see fit. If you wish to keep as such, you should just make sure that you don’t already have a conversion that was run in the existing directory. To run this:

#(copy the above script)
nano ~/bin/crw2jpg
#(paste and save)
chmod +x ~/bin/crw2jpg
cd to_directory_with_new_photos/
crw2jpg

Enjoy cooking those RAW images, and be sure to check back for our conversion GUI. Good luck!