Category: Web Works


So, yesterday NASA published a collection of sounds from historic spaceflights and current missions.
You can check them out here.

Well, I noticed there was no ‘download ALL’ button there, and because most links at a time were hidden by means of javascript my normal tools didn’t really work.

SO! It was time for some scripting!

First, I opened up the FireBug console [link] and tried to see if jQuery was used on the page.
Of course it was, BUT, with a twist. The $ shortcut was assigned to another library. No matter, I can still use jQuery ‘s full name.

So, let’s get a list with all the links to .mp3 files:

jQuery('a[href$=".mp3"]')

which means, get all anchor elements whose href attribute ends with “.mp3”

What should we do to them then?
Keeping it simple, let’s just print the href attribute for each of them on the console. So we go:

jQuery('a[href$=".mp3"]').each(
    function(){
        console.log(jQuery(this).attr('href'));
    })

Pretty straightforward. It’s a one-liner, but I indented here for clarity.

You’ll notice that at the end of the links, we also get an array of all the elements. Well, that’s because our whole statement also returns the collection.
So let’s copy-paste the links to a text file, let’s name it ‘in’.

........
/mp3/590318main_ringtone_135_launch.mp3
445945....min.js (line 30)
/mp3/577774main_STS-135Launchringtone-v2.mp3
445945....min.js (line 30)
/mp3/590196main_ringtone_135_landingCommanderComments.mp3
445945....min.js (line 30)
/mp3/590316main_ringtone_135_landingNaviusComments.mp3
445945....min.js (line 30)
/mp3/581549main_Apollo-8_Merry-Christmas.mp3
........

Oh bummer! We also got a lot of lines like “445945….min.js (line 30)” on them. Not really a problem, let’s crunch it up with python for the geek-out factor:
I ran this:

with open('in') as f:
    lines = f.readlines()
final = [line for line in lines if line.endswith('.mp3\n')]
with open('out', 'w') as f:
    f.writelines(final)
    f.write('\n')

Again, we only keep that lines that end with ‘.mp3’ (and the newline) as relevant.
What should we do with our list file now you ask? But of course use wget.
We want wget to grab all our .mp3s from the list in the file, prepending ‘http://www.nasa.gov’, as the urls are relative
Here it is:

wget -i b -B http://www.nasa.gov

And voila! Here is our collection of NASA mp3s for our geeky pleasure!

Advertisements

For the past couple of weeks I’m  (re)learning a couple of web frameworks. Namely, Yii for PHP and jQuery for JavaScript.

Almost always, my process when learning something new goes like this:

  1. Find an introductory tutorial or book to get a glimpse of what is possible and get a feeling of how it all works.
  2. Establish a robust source of documentation, which, at least for OSS stuff, will most probably be the guide found on the developers’ site.
  3. Play around with it. For me that almost always means “Make a simple game with it”

And this is what I came up with jQuery after just 2-3 days of getting serious about it:

http://pastehtml.com/view/b6ih9wnt2.html 😉

Such a great and easy framework!!

All code is embedded in the HTML in case you want to take a look.

Nick