Fork me on GitHub

2010/03/24

はてな
スクリプトを書いたりしてる人にとってはkarmaなんかの評価値は励みになったりもします。
#!/usr/bin/env perl
use strict;
use warnings;
use URI;
use LWP::UserAgent;
use Web::Scraper;

my $user_id = shift || '103';

local $Web::Scraper::UserAgent = LWP::UserAgent->new;
$Web::Scraper::UserAgent->env_proxy;

my $account_url = "http://www.vim.org/account/profile.php?user_id=$user_id";
my $s1      = scraper {
  process '//td/a[contains(@href,"/scripts/script")]', 'karmas[]' => sub {
    my $e1     = shift;
    my $script_url = URI->new_abs( $e1->attr('href'), $account_url );
    my $s2     = scraper {
      process '//title',                         'name'  => 'TEXT';
      process '//td[contains(text(),"Rating")]', 'karma' => sub {
        my $e2 = shift;
        if ( $e2->as_text =~ /Rating ([^\s]*), Downloaded by ([^\s]*)/ )
        {
          return { rating => $1, downloaded => $2 };
        }
      };
    };
    my $staff = $s2->scrape($script_url);
    $staff->{url} = $script_url;
    $staff;
  };
  result 'karmas';
};
my @r = @{ $s1->scrape( URI->new($account_url) ) };

use YAML::Syck;
warn Dump @r;

# vim:set et:

---
karma: 
  downloaded: 25932
  rating: 1355/552
name: "calendar.vim - Calendar : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=52
---
karma: 
  downloaded: 783
  rating: 9/7
name: "which.vim - This is a unix like \"Which\" function. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=139
---
karma: 
  downloaded: 1054
  rating: 9/10
name: "XpMenu - Make vim use WinXP style menu. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=928
---
karma: 
  downloaded: 1884
  rating: 13/13
name: "VS like Class Completion - This script can complete member of cpp or java like Visual Studio. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=675
---
karma: 
  downloaded: 943
  rating: 16/9
name: "ftplugin for Calendar - This is a ftplugin for Calenar. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=683
---
karma: 
  downloaded: 1871
  rating: 92/37
name: "VimTweak - The tweaking dll for GVim.exe. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=687
---
karma: 
  downloaded: 980
  rating: -1/1
name: "VimSpeak - Speak selected text with MS Agent : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=692
---
karma: 
  downloaded: 960
  rating: 11/4
name: "FTP Completion - complete files in command line for ftp. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=963
---
karma: 
  downloaded: 183
  rating: 2/2
name: "Pit Configuration - pit configuration library for vim : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2404
---
karma: 
  downloaded: 1282
  rating: 81/24
name: "Gist.vim - vimscript for gist : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2423
---
karma: 
  downloaded: 805
  rating: 82/29
name: "GoogleReader.vim - vimscript for googlereader : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2678
---
karma: 
  downloaded: 252
  rating: 20/5
name: "FastLadder.vim - vimscript for fastladder : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2683
---
karma: 
  downloaded: 78
  rating: 0/0
name: "GoogleSuggest Complete - complete function using google suggest API. perhaps, you should input japanese w : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2948
---
karma: 
  downloaded: 833
  rating: 101/29
name: "ZenCoding.vim - vim plugins for HTML and CSS hi-speed coding. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2981
ダメダメなスクリプトとか、もう消した方がいいかな...とか思った。

2008/08/19

はてな
HTML::TreeBuilderは便利だけど、データ構造がロジックになってしまうのが難点。
Perlでブックオフの店舗を検索し、結果をハッシュの配列に格納する - As a Futurist...

HTMLを解析する練習です。Perlの配列とかハッシュの扱いも少し分かりました。 以下のブックオフの検索をPerlでやっただけです。

http://blog.riywo.com/2008/03/31/164327
ってことでWeb::Scraperで。

#!/usr/bin/perl -w

use strict;
use warnings;
use URI;
use Web::Scraper;
use YAML;

my $str = shift || '新宿';
my $uri = URI->new( 'http://www.bookoff.co.jp/shop/shop.php' );
$uri->query_form(
    action => 'search',
    station => $str,
    shop_name => $str,
);

warn Dump scraper {
    process '//tr[td]', 'res[]' => scraper {
        process '//td[1]', name => 'TEXT',
        process '//td[2]', time => 'TEXT',
        process '//td[3]', tel => 'TEXT',
        process '//td[4]', place => 'TEXT',
    };
    result qw/res /;
}->scrape( $uri );
Web::Scraper便利。
process書かずに
name => '//td[1]/text()'
とか書けるシンタックスシュガーあったらなぁ...とか思った。