Perl Hacks: Googleicious!
At last, here's my little googleicious, my first attempt at merging google results with del.icio.us tags. I did it some months ago to see if I could automatically retrieve information from del.icio.us. Well... it's possible :)
What does this script do? It takes a word (or a collection of words) from the command line and searches for it on google; then it takes the search results, extracts URLs from it and searches for them within del.icio.us, showing the tags that have been used to classify them. Even if the script is quite simple, there are many details you can infer from its output:
- you can see how many top results in Google are actually interesting for people
- you can use tags to give meaning to some obscure words (try for instance ESWC and you'll see it's a conference about semantic web, or screener and you'll learn it's not a term used only by movie rippers)
- starting from the most used tags returned by del.icio.us, you can search for similar URLs which haven't been returned by Google
Now some notes:
- I believe this is very near to a limit both Google and del.icio.us don't want you to cross, so please behave politely and avoid bombing them with requests
- For the very same reason, this version is quite limited (it just takes the first page of results) but lacks some more controls I put in later versions such as sleep time to avoid request bursts. So, again, be polite please ^__^
- I know that probably one zillion people have already used the term googleicious and I don't want to be considered as its inventor. I just invented the script, so if you don't like the name just call it foobar or however you like: it will run anyway.
Ah, yes, the code is here!
Perl Hacks: SukaSudoku!
Hi everybody :)
As I decided to post one "perl hack" each week, and as after two posts I lost my inspiration, I've decided to recycle an old project which hasn't been officially published yet. Its name is sukasudoku, and as the name implies it sucks sudokus from a website (http://www.dailysudoku.com) and saves them on your disk.
BUT...
Hah! It doesn't just "save them on your disk", but creates a nice book in PDF with all the sudokus it has downloaded. Then you can print the book, bind it and either play your beloved sudokus wherever you want or give it to your beloved ones as a beautiful, zero-cost Xmas gift.
Of course, the version I publish here is limited just to avoid having every script kiddie download the full archive without efforts. But you have the source, and you have the knowledge. So what's the problem?
The source file is here for online viewing, but to have a working version you should download the full package. In the package you will find:
- book and clean, two shell scripts to create the book from the LaTeX source (note: of course, you need latex installed in your system to make it work!) and to remove all the junk files from the current dir
- sudoku.sty, the LaTeX package used to create sudoku grids
- suka.pl, the main script
- tpl_sudoku.tex, the template that will be used to create the latex document containing sudokus
Perl Hacks: ESSID-dependant networking startup
Problem: you have a laptop, which has to work with different wifi networks. For each one, you need different startup options (ie. you have to start openvpn at work, you have to set encryption at home, you just have to run networking in all other cases). You would like to autodetect the network you're in and run configs accordingly.
Solution: instead of the usual networking startup script, run this one. Then create some scripts inside /etc/nw directory, one for each ESSID network you've specified inside the Perl script and a default one. For instance:
/etc/nw/wlanHOME:
#!/bin/sh iwconfig eth1 essid wlanHOME iwconfig eth1 key 917AD823B4EA3395E214BC258B /etc/init.d/networking start
/etc/nw/wlanWORK:
#!/bin/sh /etc/init.d/networking start /etc/init.d/openvpn start
/etc/nw/default:
#!/bin/sh /etc/init.d/networking start
... of course it's not perfect, but it's quite easy to understand and I hope that with the code under your hands you'll be able to create something useful.
Have phun,
+mala
Perl Hacks: Random Wallpaper
Time ago I was searching for a random wallpaper changer for Gnome, but I couldn't find one which was ok for me. These were the features I wanted:
- it had to be as simple as a random wallpaper changer should be (you know, something like you click and it changes your wallpaper. Oh yes, randomly!)
- it had to deal not just with the background image (stretching or shrinking it) but also with all the other background properties I'm used to set with Gnome
- it didn't have to scan a whole directory and get all the pix inside it, but just to select the few images I liked most
Well, I decided to solve the problem myself with a quick and dirty Perl script: randomwp.pl. It just scans your backgrounds.xml file where your various backgrounds settings are saved, chooses one background randomly and then applies it. Quick? Yes. Dirty? Yes. But most of all working! :-)
How to install:
- just make sure you have perl installed in your system with the XML::Simple package. For instance, run
perl -MXML::Simple -e 'print "Everything ok.n"'
on your command line and if the answer is "Everything ok" then... well, I suppose everything is ok! Otherwise, you should install XML::Simple package using CPAN. What? You don't know how to do it? Go and learn it! A hint?
perl -MCPAN -e 'install XML::Simple'
- download the script and modify it so it matches your home backgrounds.xml file location (a hint: start changing the $HOME path)
- make sure you have at least two backgrounds configured within Gnome
- run the script from the command line:
perl randomwp.pl
If everything works fine, you can customize it in many ways:
- make it run automatically at system startup or every few minutes
- put a new icon on the panel and make it executable with one click
- create many backgrounds.xml and pass them as parameters, so you have different wallpaper pools to choose from
- unleash your imagination :)