Class: Nicovideo::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nicovideo/base.rb', line 10

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 Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



22
23
24
# File 'lib/nicovideo/base.rb', line 22

def agent
  @agent
end

Instance Method Details

#agent_init(auto_login = true) ⇒ Object



24
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
# File 'lib/nicovideo/base.rb', line 24

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_get, *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



96
97
98
# 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) ⇒ Object



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

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

#get_tags(video_id) ⇒ Object



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

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

#get_title(video_id) ⇒ Object



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

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

#get_video(video_id) ⇒ Object



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

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

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



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

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

#mylist(mylist_id) ⇒ Object



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

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

#newarrival(pagenum = 1) ⇒ Object



112
113
114
# File 'lib/nicovideo/base.rb', line 112

def newarrival(pagenum=1)
  Newarrival.new(@agent,pagenum)
end

#openlist(video_id) ⇒ Object



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

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

#randomObject



108
109
110
# File 'lib/nicovideo/base.rb', line 108

def random()
  Random.new(@agent)
end

#ranking(type = 'mylist', span = 'daily', category = 'all', pagenum = nil) ⇒ Object

type : ‘mylist’, ‘view’ or ‘res’ span : ‘daily’, ‘newarrival’, ‘weekly’, ‘monthly’, ‘total’ category : ‘all’, ‘music’ … and more



119
120
121
# 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) ⇒ Object

keyword : search keyword sort : nil -> published date

'v' -> playback times
'n' -> commented date
'r' -> comment number
'm' -> mylist number


129
130
131
# File 'lib/nicovideo/base.rb', line 129

def search(keyword, sort=nil, order=nil, pagenum=1)
  Search.new(@agent, keyword, sort, order, pagenum)
end

#tagsearch(keyword, sort = nil, order = nil, pagenum = 1) ⇒ Object



133
134
135
# File 'lib/nicovideo/base.rb', line 133

def tagsearch(keyword, sort=nil, order=nil, pagenum=1)
  TagSearch.new(@agent, keyword, sort, order, pagenum)
end

#watch(video_id) ⇒ Object



71
72
73
74
75
76
77
78
# 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