#!/usr/bin/perl -w # Copyright (C) 2005 Andrea Schweer # # # Queries the licquor price database at systembolaget.se and returns the # result as a WML page # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA use strict; use CGI::WML; my $query = new CGI::WML; my $looking_for = $query->param('looking_for'); my ($id, $title, $content); if ($looking_for) { $looking_for =~ s/[^\w\d%\s]//g; my @results = find_results($looking_for); $id = "results"; $title = "Results for "$looking_for""; $content = "

name, volume, price SEK, price EUR:

\n"; foreach (@results) { $content .= join(", ", @$_) . "
\n"; } $content .= "

\n"; } else { $id = "queryform"; $title = "Query"; $content = <<'END';

Enter your query here:
Send

END } print $query->header(), $query->start_wml(), $query->template(-content=>$query->prev() . ""), $query->card(-id=>$id, -title=>$title, -content=>$content), $query->card(-id=>"about", -title=>"About", -content=>"

Price data from systembolaget.se.
" . "Currency conversion rate as of July 5th, 2005 (1 SEK = " . sek_to_eur(1) . ").

\n

Brought to you by Andrea. More information (sorry, no WAP version).

"), $query->end_wml(); # queries systembolaget.se sub find_results { my $looking_for = shift; my @lines = split /\n/, do_query($looking_for); my @results; foreach (@lines) { if (/]+>]+><\/td>]+>[^<>]*<\/td>]+>[^<>]*<\/td>]+>]+>([^<>]*)<\/b>[^<>]*<\/a>[^<>]*<\/td>]+>[^<>]*<\/td>]+>[^<>]*<\/td>]+>[^<>]*<\/td>]+>[^<>]*<\/td>]+>[^<>]*<\/td>]+>([^<]*)<\/td>]+>[^<>]*<\/td>]+>([\d.]*).*<\/b><\/td>]+>.*<\/td>/) { my $price_sek = $3 . " SEK"; my $price_eur = sek_to_eur($price_sek); push(@results, [$1, $2, $price_sek, $price_eur]); } } unless (@results) { my ($name, $volume, $price_sek, $price_eur); foreach (@lines) { if (/([^<>]*)<\/span>/) { $name = $1; } elsif (/]+>]+><\/td>]+>[^<>]*<\/td>]+>([^<>]*)<\/td>]+>[^<>]*<\/td>]+>([\d.]*).*<\/td>/) { $volume =$1; $price_sek = $2 . " SEK"; $price_eur = sek_to_eur($price_sek); } } push(@results, [$name, $volume, $price_sek, $price_eur]); } return @results; } sub do_query { my $param = shift; use LWP::Simple; my $content = get("http://systembolaget.se/Applikationer/Sok/ResultatLista.htm?SokKriteria=$param&SortKol=pris&Asc=1"); return $content; } sub sek_to_eur { my $arg = shift; return sprintf("%.2f EUR", $arg * 0.105928); }