Class: Nicovideo::Base
- Inherits:
-
Object
- Object
- Nicovideo::Base
- Defined in:
- lib/nicovideo/base.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
Instance Method Summary collapse
- #agent_init(auto_login = true) ⇒ Object
- #get_comments(video_id, num = 500) ⇒ Object
- #get_flv(video_id) ⇒ Object
- #get_tags(video_id) ⇒ Object
- #get_title(video_id) ⇒ Object
- #get_video(video_id) ⇒ Object
-
#initialize(mail = nil, password = nil, auto_login = true) ⇒ Base
constructor
A new instance of Base.
- #login(mail = nil, password = nil) ⇒ Object
- #mylist(mylist_id) ⇒ Object
- #newarrival(pagenum = 1) ⇒ Object
- #openlist(video_id) ⇒ Object
- #random ⇒ Object
-
#ranking(type = 'mylist', span = 'daily', category = 'all', pagenum = nil) ⇒ Object
type : ‘mylist’, ‘view’ or ‘res’ span : ‘daily’, ‘newarrival’, ‘weekly’, ‘monthly’, ‘total’ category : ‘all’, ‘music’ …
-
#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.
- #tagsearch(keyword, sort = nil, order = nil, pagenum = 1) ⇒ Object
- #watch(video_id) ⇒ Object
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, 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 |
Instance Attribute Details
#agent ⇒ Object (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 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 |
#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 (video_id) get_videopage(video_id). 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 login mail=nil, password=nil @mail ||= mail @password ||= password @agent.set_account(@mail, @password) @agent.login 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 |
#random ⇒ Object
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 (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 |