root/platform/pragger/Feed/nicovideo_find.rb @ 28583

Revision 28583, 2.3 kB (checked in by yasuyuki, 4 years ago)

取得対象タグ追加
取得ページ数を3から4に変更

Line 
1## search nicovideo by tag. returning array of HTML page.
2##
3## If is not specified mail and password, your e-mail address and password must be set into ~/.netrc as machine nicovideo.jp.
4##
5## - module: Feed::nicovideo_find
6##   config:
7##     tag: IDOLM@STER (must)
8##     maii: your mail address (optional)
9##     password: your password (optional)
10##
11## or give some tags by a list:
12##
13## - module: Feed::nicovideo_find
14##   config:
15##     tag:
16##       - IDOLM@STER
17##       - IM@S
18##
19## Copyright (C) 2007, TADA Tadashi <sho@spc.gr.jp>,
20## 2007-2008 INOUE Yasuyuki <inoue.yasuyuki0@gmail.com>
21## Original version is written by TADA Tadashi <sho@spc.gr.jp>
22## You can redistribute it and/or modify it under GPL3 or any later version.
23
24begin
25        require 'rubygems'
26rescue LoadError
27end
28require 'mechanize'
29require 'cgi'
30
31def nicovideo_find( config, data )
32        plugin_name = "Feed::nicovideo_find"
33       
34        data = [] if config['reset']
35
36        mail = config['mail']
37        password = config['password']
38        unless mail then
39                require 'net/netrc'
40                if netrc = Net::Netrc.locate( 'nicovideo.jp' ) then
41                        mail, password = netrc.login, netrc.password
42                else
43                        $stderr.puts "#{plugin_name}: no nicovideo.jp entry in .netrc."
44                        return []
45                end
46        end
47
48        begin
49                nico = "http://www.nicovideo.jp/"
50                #sort = '?page=#{count}&sort=f'
51                agent = WWW::Mechanize::new
52
53                # login sequence
54                page = agent.get( nico )
55                login_form = page.forms.with.name( 'login_bar' ).first
56                login_form.mail = mail
57                login_form.password = password
58                agent.submit( login_form )
59        rescue NameError # no login form
60                $stderr.puts "#{plugin_name}::nicovideo_find: login failure."
61                return nil
62        end
63
64        # getting each tag pages
65        config['tag'].each do |tag|
66                (1..5).each do |count|
67                        retry_count = 0
68                        uri = "#{nico}tag/#{CGI::escape( tag )}?page=#{count}&sort=f"
69                        begin
70                                page = agent.get( uri )
71                                tmp_data = page.body
72                                data << tmp_data
73                                break unless tmp_data.include? '次のページ&nbsp;&gt;&gt;</a>'
74                        rescue EOFError # unknown error
75                                if retry_count < 10 then
76                                        retry_count += 1
77                                        $stderr.puts "#{plugin_name}: retry #{tag} (#{retry_count})."
78                                        sleep 10
79                                        retry
80                                end
81                                $stderr.puts "#{plugin_name}::nicovideo_find: unknown error. retry aborted."
82                        end
83                        sleep 30
84                end
85        end
86        data
87end
Note: See TracBrowser for help on using the browser.