Parent

Files

Nicovideo::Base

Attributes

agent[R]

Public Class Methods

new(mail=nil, password=nil, auto_login=true) click to toggle source
# File lib/nicovideo/base.rb, line 10
def initialize mail=nil, password=nil, auto_login=true
  @mail = mail
  @password = password
  @agent = WWW::Mechanize.new()
  agent_init(auto_login)
  @agent.set_account(@mail, @password)

  # for parameters current video
  @vp = nil
  self
end

Public Instance Methods

agent_init(auto_login=true) click to toggle source
# File lib/nicovideo/base.rb, line 24
def agent_init auto_login=true
  @agent.instance_eval do
    alias raw_get get
    alias raw_post post
 
    def set_account(mail, password) @mail=mail; @password=password end
    def authenticated?(page)
      page.header['x-niconico-authflag'] != '0'
    end

    def login
      raise ArgError unless (@mail && @password)
      account = {'mail' => @mail, 'password' => @password }
      res = raw_post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
      raise LoginError unless authenticated?(res)
    end
  end

  if auto_login
    @agent.instance_eval do
      @wait_time = 3
      def get(*args) try(:raw_get, *args) end
      def post(*args) try(:raw_post, *args) end
      
      def try(name, *args)
        page = method(name).call(*args)
        unless authenticated?(page)
          self.login
          sleep @wait_time
          page = method(name).call(*args)
          raise LoginError unless authenticated?(page)
        end
        page
      end
    end
  end
  
end
authenticated?(page) click to toggle source
# File lib/nicovideo/base.rb, line 30
def authenticated?(page)
  page.header['x-niconico-authflag'] != '0'
end
get(*args) click to toggle source
# File lib/nicovideo/base.rb, line 45
def get(*args) try(:raw_get, *args) end
get_comments(video_id, num=500) click to toggle source
# File lib/nicovideo/base.rb, line 96
def get_comments video_id, num=500
  get_videopage(video_id).comments(num)
end
get_flv(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 92
def get_flv(video_id)
  get_videopage(video_id).flv
end
get_tags(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 80
def get_tags(video_id)
  get_videopage(video_id).tags
end
get_title(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 84
def get_title(video_id)
  get_videopage(video_id).title
end
get_video(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 88
def get_video(video_id)
  self.get_flv(video_id)
end
login() click to toggle source
# File lib/nicovideo/base.rb, line 34
def login
  raise ArgError unless (@mail && @password)
  account = {'mail' => @mail, 'password' => @password }
  res = raw_post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
  raise LoginError unless authenticated?(res)
end
mylist(mylist_id) click to toggle source
# File lib/nicovideo/base.rb, line 100
def mylist(mylist_id)
  MyList.new(@agent, mylist_id)
end
newarrival(pagenum=1) click to toggle source
# File lib/nicovideo/base.rb, line 112
def newarrival(pagenum=1)
  Newarrival.new(@agent,pagenum)
end
openlist(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 104
def openlist(video_id)
  OpenList.new(@agent, video_id)
end
post(*args) click to toggle source
# File lib/nicovideo/base.rb, line 46
def post(*args) try(:raw_post, *args) end
random() click to toggle source
# File lib/nicovideo/base.rb, line 108
def random()
  Random.new(@agent)
end
ranking(type='mylist', span='daily', category='all', pagenum=nil) click to toggle source

type : 'mylist', 'view' or 'res' span : 'daily', 'newarrival', 'weekly', 'monthly', 'total' category : 'all', 'music' ... and more

# File lib/nicovideo/base.rb, line 119
def ranking(type='mylist', span='daily', category='all', pagenum=nil)
  Ranking.new(@agent, type, span, category, pagenum).to_a
end
search(keyword, sort=nil, order=nil, pagenum=1) click to toggle source

keyword : search keyword sort : nil -> published date

'v' -> playback times
'n' -> commented date
'r' -> comment number
'm' -> mylist number
# File lib/nicovideo/base.rb, line 129
def search(keyword, sort=nil, order=nil, pagenum=1)
  Search.new(@agent, keyword, sort, order, pagenum)
end
set_account(mail, password) click to toggle source
# File lib/nicovideo/base.rb, line 29
def set_account(mail, password) @mail=mail; @password=password end
tagsearch(keyword, sort=nil, order=nil, pagenum=1) click to toggle source
# File lib/nicovideo/base.rb, line 133
def tagsearch(keyword, sort=nil, order=nil, pagenum=1)
  TagSearch.new(@agent, keyword, sort, order, pagenum)
end
try(name, *args) click to toggle source
# File lib/nicovideo/base.rb, line 48
def try(name, *args)
  page = method(name).call(*args)
  unless authenticated?(page)
    self.login
    sleep @wait_time
    page = method(name).call(*args)
    raise LoginError unless authenticated?(page)
  end
  page
end
watch(video_id) click to toggle source
# File lib/nicovideo/base.rb, line 71
def watch(video_id)
  videopage = get_videopage(video_id)
  @vp = videopage
  if block_given?
    yield videopage
  end
  videopage
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.