Skip to content

Instantly share code, notes, and snippets.

@typester
Created January 20, 2011 01:20
Show Gist options
  • Select an option

  • Save typester/787232 to your computer and use it in GitHub Desktop.

Select an option

Save typester/787232 to your computer and use it in GitHub Desktop.

Revisions

  1. typester created this gist Jan 20, 2011.
    78 changes: 78 additions & 0 deletions check_amazon.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    #!/usr/bin/env perl

    use strict;
    use warnings;

    use Getopt::Long;
    use Pod::Usage;

    use WWW::Mechanize;
    use Cocoa::EventLoop;
    use Cocoa::Growl ':all';

    GetOptions(
    \my %options,
    qw/help wait=i/
    );
    pod2usage(0) if $options{help};

    my $asin = $ARGV[0] or die 'require ASIN';

    die 'growl is not installed' unless growl_installed;
    die 'growl is not runnning' unless growl_running;

    growl_register(
    app => 'Amazon checker',
    notifications => ['Default'],
    );

    my $m = WWW::Mechanize->new( stack_depth => 1 );
    $m->agent_alias('Windows IE 6');

    my $u = "http://www.amazon.co.jp/dp/${asin}";

    my $check; $check = sub {
    $m->get($u);
    if ($m->content =~ /addToCartSpan/) {
    growl_notify(
    name => 'Default',
    title => 'ktkr!',
    description => $u,
    on_click => sub {
    system 'open', $u;
    },
    );
    }

    Cocoa::EventLoop->timer(
    after => $options{wait} || 60,
    cb => $check,
    );
    };
    $check->();

    Cocoa::EventLoop->run;

    __END__
    =head1 NAME
    check_amazon.pl - check amazon item availability
    =head1 SYNOPSIS
    check_amazon.pl [OPTIONS...] ASIN
    options:
    -h --help show this help
    -w --wait polling interval (default 60)
    =head1 DESCRIPTION
    check specified amazon item and show growl notification if the item is available.
    =head1 AUTHOR
    Daisuke Murase <typester@cpan.org>
    =cut