=begin # nicovideo_search ニコニコ動画の検索から新着を取得する。 ## ダウンロード http://bmky.net/product/pragger/#nicovideo_search ## 解説 const_listには検索したいタイトルかタグを列挙する。serach/で始まればタイトル検索、 tag/で始まればタグ検索となる。そして、検索結果の**1ページ目から**全ての動画を取得する。 検索結果のページに日付の情報が含まれていないので、取得した動画をキャッシュして、 一度取得した動画は取得しないようにしている。 動作させるにはニコニコ動画のアカウントが必要。動画自体をダウンロードするわけではないので、 アカウントの種類は一般・プレミアムのどちらでも良い。 ## 使い方 - module: const_list config: - search/[検索したいタイトル] - tag/[検索したいタグ] - module: myplugin::nicovideo config: authfile: config/nicovideo_auth.yaml # ログイン情報を含んだファイル ## 設定ファイルの書き方 --- email: xxxxxxx@xxx.xxx password: xxxxxxxxx ## 更新履歴 2008-07-08 : HTMLタグの修正に対応。 2008-05-31 : ログインページがトップページに統合されたので対応。 2008-05-11 : 日付情報を取得するようにしたのでキャッシュいらずになった。 2008-05-01 : sm以外の形式に対応 : キャッシュ未指定の場合は自動的に作成するようにした。 2008-03-10 : 仕様変更に対応 2007-11-30 : リリース =end require 'rubygems' require 'mechanize' require 'fix_mechanize' require 'logger' require 'cgi' def nicovideo_search( config, data ) auth = YAML.load( File.read( config['authfile'] ) ) agent = WWW::Mechanize.new page = agent.get( 'http://www.nicovideo.jp/' ) form = page.forms[1] form.fields.find { |f| f.name == 'mail' }.value = auth["mail"] form.fields.find { |f| f.name == 'password' }.value = auth["password"] form.fields.find { |f| f.name == 'next_url' }.value = '' page = agent.submit( form, form.buttons.first ) items = [] movies = [] titles = config['titles'] data.each do |word| url = 'http://www.nicovideo.jp/' + CGI.escape( word ) + '?sort=f' url.gsub!( /%2F/, "/" ) html = agent.get_file( url ).toutf8 html.gsub( Regexp.new( '
([\s\S]*?)
' ) ) do item = "" capture = $1 if capture =~ Regexp.new( '(.*?)
\s*(.*?)

' ) video_id = $1 smlink = 'http://www.nicovideo.jp/' + video_id smtitle = $2 smdescription = $3 if capture =~ /(\d+\/\d+\/\d+ \d+:\d+)<\/strong> 投稿/ smpostdate = $1 + ":00"; smpostdate.gsub!( /\//, "-" ) item.instance_eval do @link = smlink def link @link end @title = smtitle def title @title end @date = Time.parse( smpostdate ) def date @date end @description = smdescription def description @description end end items << item end end end sleep 3 end return items end