Skip to content

Instantly share code, notes, and snippets.

@clairvy
Created February 2, 2010 12:43
Show Gist options
  • Select an option

  • Save clairvy/292631 to your computer and use it in GitHub Desktop.

Select an option

Save clairvy/292631 to your computer and use it in GitHub Desktop.
dist
log
old
stocks.csv
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
use DateTime::Format::HTTP;
use SSBCSV;
use Config::Pit;
use Encode;
use Data::Dumper;
my $url = "http://book.akahoshitakuya.com";
my $config = pit_get('book.akahoshitakuya.com', require => {
"username" => 'your email',
"password" => 'your password',
});
my $user = $config->{username};
my $pass = $config->{password};
my %state_hash = (
read => sub {
return (url_regex => qr{^/add\.php\?}, n => 1);
}, # add()
reading => sub {
return (url_regex => qr{^/add\.php\?.*type=now});
}, # type=now add_now() type=del_now del_now()
stack => sub {
return (url_regex => qr{^/add\.php\?.*type=tun});
}, # type=tun add_tun() type=del_tun del_tun()
wish => sub {
return (url_regex => qr{^/add\.php\?.*type=pre});
}, # type=pre add_pre()
# type=re readd()
);
my $mech = WWW::Mechanize->new();
$mech->get($url . "/login");
$mech->form_number(2);
$mech->submit_form(
fields => {
mail => $user,
password => $pass,
});
my $ssb = SSBCSV->new("stocks.csv");
my $res = $ssb->get;
#my $i = 0;
foreach my $book (reverse @{$res}){
# last if ($i > 1);
$mech->get($url . "/b/" . $book->{isbn});
print("title : ", encode('utf8', $mech->title), "\n");
my @links = $mech->find_all_links(
url_regex => qr{^/add\.php\?.*type=re},
);
print("re link count : ", scalar(@links), "\n");
next if (@links > 0);
print("state : ", $book->{state}, "\n");
my @links2 = $mech->find_all_links(
$state_hash{$book->{state}}->(),
);
print("matched actions count : ", scalar(@links2), "\n");
next unless (@links2 > 0);
print("update.\n");
$mech->follow_link(
$state_hash{$book->{state}}->(),
);
if($book->{state} eq 'read'){
my $dt = DateTime::Format::HTTP->parse_datetime($book->{date});
my $year = $dt->year;
my $month = sprintf("%02d", $dt->month);
my $day = sprintf("%02d", $dt->day);
$mech->form_number(3);
$mech->select('read_date_y', $year);
$mech->select('read_date_m', $month);
$mech->select('read_date_d', $day);
$mech->submit;
}
sleep(1);
# $i += 1;
}
#!/bin/sh
fname=stocks.csv
ymd=`date +%Y%m%d`
if [ ! -d log ]; then
mkdir log
fi
if [ ! -d old ]; then
mkdir old
fi
if [ ! -d old/$ymd ]; then
mkdir old/$ymd
fi
mv $fname old/$ymd
wget -o log/$ymd.log http://stack.nayutaya.jp/user/clairvy/export/stocks.csv
TAR = tar
PUT = ncftpput -u username hostname dir/$(DISTDIR)
TARFILE = $(DISTDIR)/dokume.tar
DISTDIR = dist
include config.mk
all : dist
ext :
$(TAR) xf $(TARFILE)
dist : $(TARFILE)
$(PUT) $(TARFILE)
tar : $(TARFILE)
$(TARFILE) :
$(TAR) cf $(TARFILE) --exclude $(DISTDIR) --exclude log --exclude old *
clean :
$(RM) $(RMF) $(TARFILE)
package SSBCSV;
use Switch;
use Text::CSV::Simple;
use Encode;
use utf8;
sub new{
my $class = shift;
my $file = shift;
my $self = {
csv_parser => Text::CSV::Simple->new({binary => 1}),
filename => $file,
};
return bless $self, $class;
}
sub get{
my $self = shift;
$self->{csv_parser}->want_fields(0, 6, 7);
$self->{csv_parser}->field_map(qw/isbn state date/);
my @list = $self->{csv_parser}->read_file($self->{filename});
shift @list;
foreach my $book (@list) {
my $s = decode('shiftjis', $book->{state});
switch ($s) {
case 'もう読み終えた' {$book->{state} = 'read'}
case 'いま読んでいる' {$book->{state} = 'reading'}
case 'まだ読んでいない' {$book->{state} = 'stack'}
case 'いつか欲しい' {$book->{state} = 'wish'}
else {}
}
}
return \@list;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment