# -*- mode: perl; coding: utf-8 -*- package WWW::NicoVideo; use utf8; use strict; use warnings; use base qw[Class::Accessor]; use Encode; use Carp; use LWP::UserAgent; use HTTP::Cookies; use URI; use URI::QueryParam; use WWW::NicoVideo::Entry; use WWW::NicoVideo::Scraper; use WWW::NicoVideo::URL; __PACKAGE__->mk_accessors(qw[agent retry retryInterval mail passwd]); our $VERSION = "0.03"; our $AGENT_NAME = "@{[__PACKAGE__]}/$VERSION)"; sub new { my $pkg = shift; my %opts = @_; my $ua = $opts{agent} || new LWP::UserAgent(agent => $AGENT_NAME, timeout => 30, %{$opts{agentOpts}}); $ua->cookie_jar($opts{cookies} || new HTTP::Cookies(%{$opts{cookiesOpts}})); bless {agent => $ua, retry => $opts{retry} || 5, retryInterval => $opts{retryInterval} || 30, mail => $opts{mail}, passwd => $opts{passwd}}, $pkg; } sub login { my $self = shift; my $ua = $self->{agent}; my $cj = $ua->cookie_jar; my $has_cookie = 0; if(not defined $self->{mail} or not defined $self->{passwd}) { confess "mail and passwd required"; } $cj->scan(sub { my($key, $val, $domain, $expires) = @_[1, 2, 4, 8]; if($domain eq ".nicovideo.jp" and time + 60 < $expires) { $has_cookie = 1; } }); my $login_ok = 0; if($has_cookie) { my $res = $ua->get(nicoURL("top")); if($res->is_success and not $res->as_string =~ /