Class: Niconico

Inherits:
Object
  • Object
show all
Defined in:
lib/niconico.rb,
lib/niconico/live.rb,
lib/niconico/video.rb,
lib/niconico/mylist.rb,
lib/niconico/channel.rb,
lib/niconico/ranking.rb,
lib/niconico/version.rb,
lib/niconico/live/api.rb,
lib/niconico/nico_api.rb,
lib/niconico/live/util.rb,
lib/niconico/deferrable.rb,
lib/niconico/live/client.rb,
lib/niconico/live/mypage.rb,
lib/niconico/live/client/search_result.rb,
lib/niconico/live/client/search_filters.rb

Defined Under Namespace

Modules: Deferrable Classes: Live, LoginError, MylistParseError, NicoAPI, Video

Constant Summary collapse

URL =
{
  top: 'http://www.nicovideo.jp/',
  login: 'https://secure.nicovideo.jp/secure/login?site=niconico',
  watch: 'http://www.nicovideo.jp/watch/',
  getflv: 'http://flapi.nicovideo.jp/api/getflv',
  my_mylist: 'http://www.nicovideo.jp/my/mylist'
}
VERSION =
"1.8.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Niconico

Returns a new instance of Niconico.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/niconico.rb', line 21

def initialize(*args)
  case args.size
  when 2
    @mail, @pass = args
  when 1
    if args.first.kind_of?(Hash)
      @mail, @pass, @token = args.first.values_at(:mail, :password, :token)
    else
      @token = args.first
    end
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for 1..2)"
  end

  @logged_in = false

  @agent = Mechanize.new.tap do |agent|
    agent.user_agent = "Niconico.gem (#{Niconico::VERSION}, https://github.com/sorah/niconico)"

    agent.cookie_jar.add(
      HTTP::Cookie.new(
        domain: '.nicovideo.jp', path: '/',
        name: 'lang', value: 'ja-jp',
      )
    )
  end
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



16
17
18
# File 'lib/niconico.rb', line 16

def agent
  @agent
end

Instance Method Details

#channel_videos(ch) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/niconico/channel.rb', line 8

def channel_videos(ch)
   unless logged_in?

  rss = Nokogiri::XML(open("http://ch.nicovideo.jp/#{ch}/video?rss=2.0", &:read))

  rss.search('channel item').map do |item|
    title = item.at('title').inner_text
    link = item.at('link').inner_text
    Video.new(self, link.sub(/^.+\/watch\//, ''), title: title)
  end
end

#inspectObject



67
68
69
# File 'lib/niconico.rb', line 67

def inspect
  "#<Niconico: #{@mail || '(token)'}, #{@logged_in ? "" : "not "}logged in>"
end

#live(live_id) ⇒ Object



6
7
8
# File 'lib/niconico/live.rb', line 6

def live(live_id)
  Live.new(self, live_id)
end

#live_clientObject



5
6
7
# File 'lib/niconico/live/client.rb', line 5

def live_client
  Live::Client.new(self.agent)
end

#live_mypageObject



6
7
8
# File 'lib/niconico/live/mypage.rb', line 6

def live_mypage
  Niconico::Live::Mypage.new(self)
end

#logged_in?Boolean Also known as: logined

Returns:

  • (Boolean)


18
# File 'lib/niconico.rb', line 18

def logged_in?; @logged_in; end

#login(force = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/niconico.rb', line 49

def (force=false)
  return false if !force && @logged_in

  if @token && @mail && @pass
    begin
      
    rescue LoginError
      
    end
  elsif @token
    
  elsif @mail && @pass
    
  else
    raise ArgumentError, 'Insufficient options for logging in (token or/and pair of mail and password required)'
  end
end

#mylist(i) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/niconico/mylist.rb', line 5

def mylist(i)
   unless logged_in?

  page = @agent.get(url = "http://www.nicovideo.jp/mylist/#{i.to_s.sub(/^mylist\//,"")}")
  #require 'ir_b'; ir b
  if page.search("script").map(&:inner_text).find{|x| /\tMylist\.preload/ =~ x }.match(/\tMylist\.preload\(\d+, (.+?)\);\n/)
    json = JSON.parse($1)
    #require 'pp'; pp json
    json.sort_by{|item| item["create_time"].to_f }.map do |item|
      Video.new(self, item["item_data"]["video_id"],
                title: item["item_data"]["title"],
                mylist_comment: item["description"])
    end
  else
    raise MylistParseError
  end
end

#nico_apiObject



78
79
80
81
82
# File 'lib/niconico.rb', line 78

def nico_api
  return @nico_api if @nico_api
   unless logged_in?
  @nico_api = NicoAPI.new(self)
end

#ranking(category = 'all', options = {}) ⇒ Object

options -> :hourly, :daily, :weekly, :monthly, :total

             or -> :hour, :day, :week, :month, :all
default: daily

options -> :fav, :view, :comment, :mylist

(or :all)        (or :res)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/niconico/ranking.rb', line 10

def ranking(category = 'all', options={})
   unless logged_in?

  span = options[:span] || :daily
  span = :hourly  if span == :hour
  span = :daily   if span == :day
  span = :weekly  if span == :week
  span = :monthly if span == :month
  span = :total   if span == :all

  method = options[:method] || :fav
  method = :res if method == :comment
  method = :fav if method == :all

  page = @agent.get(url = "http://www.nicovideo.jp/ranking/#{method}/#{span}/#{category}")
  page.search(".ranking.itemTitle a").map do |link|
    Video.new(self, link['href'].sub(/^.*?watch\//,""), title: link.inner_text)
  end
end

#tokenObject



71
72
73
74
75
76
# File 'lib/niconico.rb', line 71

def token
  return @token if @token
   unless logged_in?

  @token = agent.cookie_jar.each('https://www.nicovideo.jp').find{|_| _.name == 'user_session' }.value
end

#video(video_id) ⇒ Object



6
7
8
9
# File 'lib/niconico/video.rb', line 6

def video(video_id)
   unless logged_in?
  Video.new(self, video_id)
end