WATANABE Hiroaki

NAME

Net::NicoVideo - Perl Interface for accessing Nico Nico Douga

VERSION

This is an alpha version. The API is still subject to change. Many features have not been implemented yet.

SYNOPSIS

    use Net::NicoVideo;

    my $video_id = $ARGV[0] or die;

    my $nnv = Net::NicoVideo->new({
        email    => 'your-nicovideo@email.address',
        password => 'and-password',
        });

    my $info = $nnv->fetch_thumbinfo( $video_id );
    my $flv  = $nnv->fetch_flv( $video_id );

    printf "download: %s\n". $info->title;
    if( $flv->is_economy ){
        warn "now economy time, skip\n";
    }else{
        my $save_path = sprintf '%s/Movies/%s.%s',
            $ENV{HOME}, $video_id, $info->movie_type;
    
        $nnv->fetch_watch( $video_id );
        $nnv->fetch_video( $flv, $save_path );
        
        if( -s $save_path == $info->size_high ){
            print "ok\n";
        }else{
            print "finished, but it may have broken.\n";
        }
    }

DESCRIPTION

Nico Nico Douga (ニコニコ動画, lit. "Smile Videos") is a popular video sharing website in Japan managed by Niwango, a subsidiary of Dwango.

A Distribution Net-NicoVideo is Perl Interface for accessing Nico Nico Douga. This provides the consistent access method, and contents are encapsulated and give facilities to clients.

And this class Net::NicoVideo is an utility which summarized the procedure for obtaining each object compactly.

An instance of this class uses Net::NicoVideo::UserAgent in the inside. In other words, the client can use Net::NicoVideo::UserAgent for work of a low level.

CONSTRUCTOR

A constructor receives the hash reference which defines the field.

    my $nnv = Net::NicoVideo->new({
        user_agent  => LWP::UserAgent->new,
        email       => 'your-nicovideo@email.address',
        password    => 'and-password',
        delay       => 1,
        });

There are access methods of a same name in each field.

ACCESS METHOD (LOWER LEVEL)

The access method of the low level to the field.

These are for setting and getting directly the field which passes neither allocating default nor validation. When a value is given to an argument, the value is set as the field.

user_agent

Get or set an user agent that $nnv would access to Nico Nico Video via HTTP(s).

The user agent who sets up needs to be an instance of LWP::UserAgent.

    $nnv->user_agent(LWP::UserAgent->new);
    $ua = $nnv->user_agent;

email

Get or set email string for login to Nico Nico Video service.

    $nnv->email($email);
    $email = $nnv->email;

password

Get or set password string for login to Nico Nico Video service.

    $nnv->password($password);
    $password = $nnv->password;

delay

Get or set delay that is waiting seconds for every continuous access to a site.

    $nnv->delay($seconds);
    $seconds = $nnv->delay;

ACCESS METHOD (UPPER LEVEL)

The access method of a high level to the field.

Validation and a default value are prepared compared with the low level.

Access to the field after setting up the field by a constructor usually uses these.

get_user_agent

It creates and returns the instance of Net::NicoVideo::UserAgent.

get_email

Get email that the instance has. If it is not defined, $ENV{NET_NICOVIDEO_EMAIL} is returned instead.

get_password

Get password that the instance has. If it is not defined, $ENV{NET_NICOVIDEO_PASSWORD} is returned instead.

get_delay

Get delay that the instance has. If it is not defined, $ENV{NET_NICOVIDEO_DELAY} is returned instead. Both are not defined, returns 1.

FETCH METHOD

The method group which will get contents object.

The methods of this category returns the instance of Net::NicoVideo::Content. They correspond to each contents of the site.

Please see sub classes under Net::NicoVideo::Content for detail.

fetch_thumbinfo(video_id)

Get an instance of Net::NicoVideo::Content::ThumbInfo for video_id.

fetch_flv(video_id)

Get an instance of Net::NicoVideo::Content::Flv for video_id.

fetch_watch(video_id)

Get an instance of Net::NicoVideo::Content::Watch for video_id.

This means that the agent watches the video, and this behavior is required before fetch_video.

fetch_video(video_id | flv | url, @args)

Get an instance of Net::NicoVideo::Content::Video for video_id, flv or url. The url is value getting via $flv->url, and $flv is a Net::NicoVideo::Content::Flv which is created by $nnv->fetch_flv.

The second parameter, it works like as request() method of LWP::UserAgent, in fact, it is called.

An example, if it is a scalar value then it means that the file path to store contents.

fetch_thread(video_id | flv, \%options)

Get an instance of Net::NicoVideo::Content::Thread for video_id.

The hash reference of the second argument is an option and receives the following key and the pair of a value.

"chats" => number

How many comments fetching from the newest thing. Default is 250.

"fork" => boolean

If it is set the true, then the comment to fetch is limited only video owner's. #' Default is false.

Tag

The methods for tag search.

fetch_tag_rss(keyword, \%params)

The tag specified by keyword performs video search, and it returns results in RSS format.

The hash reference \%params can be given as options. The key and value are as follows.

"sort" => 'f|v|r|m|l'

The keyword which sorts search results.

    f ... Contribute date
    v ... Reproduction number
    r ... The number of comments 
    m ... The number of mylists
    l ... Reproduction time

Default is "r".

"order" => a

Sort order, 'a' is ascend.

Default is undef which means descend.

"page" => number

When there are many search results, the result has separated to some pages. In this value, it specifies the page of what position to get.

Default is 1.

Moreover, 1 page is 32 items at the maximum.

fetch_tag_rss_by_recent_post(keyword, page)

It is the shortcut which fixes params and calls fetch_tag_rss so that it may get in descending order of contribution time.

Mylist RSS

The method group which will get "mylist" as RSS format.

fetch_mylist_rss(mylist | mylist_id)

Get an instance of Net::NicoVideo::Content::MylistRSS for mylist.

NicoAPI BASE

The method group for get the base for accessing to NicoAPI.

NicoAPI is the name space of the library implemented by JavaScript, in order to get "mylist" by an AJAX means, and it has methods such as get of the data about "mylist", updating, and deletion.

And an access token is needed for execution of those other than an get method.

fetch_mylist_page

Get an instance of Net::NicoVideo::Content::MylistPage for take a "NicoAPI.token".

fetch_mylist_item(video_id)

Get an instance of Net::NicoVideo::Content::MylistItem. This method is useful for take a "NicoAPI.token" to update Mylist, "item_type" and "item_id" for video_id.

NicoAPI.MylistGroup

The method group of "NicoAPI.MylistGroup" which operates "mylistgroup".

Even if it omits a token, it is taken automatically and used.

list_mylistgroup()

List list of "mylistgroup" for user.

This is equivalent to NicoAPI.MylistGroup#list.

get_mylistgroup(group_id)

Get an instance of Net::NicoVideo::Content::MylistGroup for specified group_id.

This is equivalent to NicoAPI.MylistGroup#get.

add_mylistgroup(mylistgroup, [token])

Add a "mylistgroup" for user.

This is equivalent to NicoAPI.MylistGroup#add

update_mylistgroup(mylistgroup, [token])

Update a "mylistgroup".

This is equivalent to NicoAPI.MylistGroup#update

remove_mylistgroup(mylistgroup, [token])

Remove a "mylistgroup".

This is equivalent to NicoAPI.MylistGroup#remove

delete_mylistgroup(mylistgroup, [token])

An alias of remove_mylistgroup().

NicoAPI.Mylist

The method group of "NicoAPI.Mylist" which operates "mylist".

Even if it omits a token, it is taken automatically and used.

list_mylist(group_id)

Get list of "mylist" items for specified group_id.

This is equivalent to NicoAPI.Mylist#list

add_mylist(group_id, mylistitem, [token])

Add "mylistitem" to "mylistgroup".

This is equivalent to NicoAPI.Mylist#add.

update_mylist(group_id, mylistitem, [token])

Update "mylistitem" in "mylistgroup".

This is equivalent to NicoAPI.Mylist#update.

remove_mylist(group_id, mylistitem, [token])

Remove "mylistitem" from "mylistgroup".

This is equivalent to NicoAPI.Mylist#remove.

delete_mylist(group_id, mylistitem, [token])

alias of remove_mylist().

move_mylist(src-group_id, dst-group_id, mylistitem, [token])

Move "mylistitem" from src "mylistgroup" to dst "mylistgroup".

This is equivalent to NicoAPI.Mylist#move.

copy_mylist(src-group_id, dst-group_id, mylistitem, [token])

Duplicate "mylistitem" from src "mylistgroup" to dst "mylistgroup".

This is equivalent to NicoAPI.Mylist#copy.

UTILITY METHOD

Other utility methods.

through_login(ua)

The user agent who gave the argument is led to a login page, and it logs in. And the original user agent who gave the result is returned.

The returning $ua is the same instance as what was given.

Typically, it is used as follows.

    $res = $ua->request_mylist_rss($mylist);
    unless( $res->is_authflagged ){              # if not logged-in
        $ua = $self->through_login($ua);         # login
        $res = $ua->request_mylist_rss($mylist); # try again
    }

When login goes wrong, then croak.

download(video_id, file)

download() is a shortcut to download video which is identified by video_id.

For busy person, you can download a video by one liner like this:

    $ perl -MNet::NicoVideo -e 'Net::NicoVideo->new->download(@ARGV)' \
        smNNNNNN ./smile.mp4

Note that it is necessary to set environment variables in advance.

Although the media file to download may be MP4, it may not be so. Either MP4, or FLV or SWF is known now.

By "thumbinfo" object which has same video_id can judge type of media.

ENVIRONMENT VARIABLE

    NET_NICOVIDEO_EMAIL
    NET_NICOVIDEO_PASSWORD
    NET_NICOVIDEO_DELAY

These obvious environment variables are effective. If the object has each value as its fields, priority is given to them.

SEE ALSO

LWP::UserAgent Net::NicoVideo::Content Net::NicoVideo::UserAgent

REPOSITORY

Net::NicoVideo is hosted on github https://github.com/hiroaki/Net-NicoVideo

AUTHOR

WATANABE Hiroaki <hwat@cpan.org>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.