Class: Nicovideo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nicovideo/base.rb

Overview

class MyListNotFound < StandardError ; end

Instance Method Summary collapse

Constructor Details

#initialize(mail = nil, password = nil, auto_login = true) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nicovideo/base.rb', line 13

def initialize mail=nil, password=nil, =true
  @mail = mail
  @password = password
  @agent = WWW::Mechanize.new()
  agent_init()
  @agent.(@mail, @password)

  # for parameters current video
  @vp = nil
  self
end

Instance Method Details

#agent_init(auto_login = true) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nicovideo/base.rb', line 25

def agent_init =true
  @agent.instance_eval do
    alias raw_get get
    alias raw_post post
 
    def (mail, password) @mail=mail; @password=password end
    def authenticated?(page)
      page.header['x-niconico-authflag'] != '0'
    end

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

  if 
    @agent.instance_eval do
      @wait_time = 3
      def get(*args) try(:raw_post, *args) end
      def post(*args) try(:raw_post, *args) end
      
      def try(name, *args)
        page = method(name).call(*args)
        unless authenticated?(page)
          self.
          sleep @wait_time
          page = method(name).call(*args)
          raise LoginError unless authenticated?(page)
        end
        page
      end
    end
  end
  
end

#get_comments(video_id, num = 500) ⇒ Object



97
98
99
# File 'lib/nicovideo/base.rb', line 97

def get_comments video_id, num=500
  get_videopage(video_id).comments(num)
end

#get_flv(video_id) ⇒ Object



93
94
95
# File 'lib/nicovideo/base.rb', line 93

def get_flv(video_id)
  get_videopage(video_id).flv
end

#get_tags(video_id) ⇒ Object



81
82
83
# File 'lib/nicovideo/base.rb', line 81

def get_tags(video_id)
  get_videopage(video_id).tags
end

#get_title(video_id) ⇒ Object



85
86
87
# File 'lib/nicovideo/base.rb', line 85

def get_title(video_id)
  get_videopage(video_id).title
end

#get_video(video_id) ⇒ Object



89
90
91
# File 'lib/nicovideo/base.rb', line 89

def get_video(video_id)
  self.get_flv(video_id)
end

#login(mail = nil, password = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/nicovideo/base.rb', line 64

def  mail=nil, password=nil
  @mail     ||= mail
  @password ||= password
  @agent.(@mail, @password)
  @agent.
  self
end

#mylist(mylist_id) ⇒ Object



101
102
103
# File 'lib/nicovideo/base.rb', line 101

def mylist(mylist_id)
  MyList.new(@agent, mylist_id)
end

#openlist(video_id) ⇒ Object



105
106
107
# File 'lib/nicovideo/base.rb', line 105

def openlist(video_id)
  OpenList.new(@agent, video_id)
end

#watch(video_id) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/nicovideo/base.rb', line 72

def watch(video_id)
  videopage = get_videopage(video_id)
  @vp = videopage
  if block_given?
    yield videopage
  end
  videopage
end