Class: Bnicovideo::Mylist

Inherits:
Object
  • Object
show all
Defined in:
lib/bnicovideo/mylist.rb

Overview

Mylist class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_session, mylist_id) ⇒ Mylist

Init from user session and mylist ID

user_session

Bnicovideo::UserSession

mylist_id

Mylist ID



25
26
27
28
29
# File 'lib/bnicovideo/mylist.rb', line 25

def initialize(user_session, mylist_id)
  @got = false
  @user_session = user_session
  @mylist_id = mylist_id
end

Instance Attribute Details

#authorObject (readonly)

Mylist author



21
22
23
# File 'lib/bnicovideo/mylist.rb', line 21

def author
  @author
end

#mylist_idObject (readonly)

Mylist ID



13
14
15
# File 'lib/bnicovideo/mylist.rb', line 13

def mylist_id
  @mylist_id
end

#subtitleObject (readonly)

Mylist description



19
20
21
# File 'lib/bnicovideo/mylist.rb', line 19

def subtitle
  @subtitle
end

#titleObject (readonly)

Mylist title



17
18
19
# File 'lib/bnicovideo/mylist.rb', line 17

def title
  @title
end

#videosObject (readonly)

Videos’ hash. Video key is Bnicovideo::Video class object, and Content key is mylist description.



15
16
17
# File 'lib/bnicovideo/mylist.rb', line 15

def videos
  @videos
end

Instance Method Details

#read(refresh = false) ⇒ Object

Read information

refresh

If this is true, force read. Otherwise, read unless not read.



58
59
60
61
# File 'lib/bnicovideo/mylist.rb', line 58

def read(refresh = false)
  return if @got && !refresh
  read!
end

#read!Object

Force read information



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
# File 'lib/bnicovideo/mylist.rb', line 31

def read!
  @videos = []
  resp = nil
  Net::HTTP.start('www.nicovideo.jp') do |http|
    resp = http.get('/mylist/' + @mylist_id.to_s + '?rss=atom',
      {'Cookie' => 'user_session=' + @user_session.session_id})
  end
  resp.value
  xml = REXML::Document.new(resp.body)
  root = xml.root
  @title = root.elements['title'].text
  @subtitle = root.elements['subtitle'].text
  @author = root.elements['author/name'].text
  root.elements.each('entry') do |entry|
    video_link = entry.elements['link'].attributes['href']
    uri = URI.parse(video_link)
    next unless uri.scheme == 'http' && uri.host == 'www.nicovideo.jp'
    video_id = uri.path.split('/')[-1]
    @videos.push({
        'Video' => Bnicovideo::Video.new(@user_session, video_id),
        'Content' => entry.elements['content'].text
      })
  end
  @got = true
end