Module: Nicos::Parser::Xml

Defined in:
lib/classes/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getThumbInfo(xml) ⇒ HashObj

getThumbInfoが返すXMLを解析し、ハッシュオブジェクトにして返します。

Returns:

  • (HashObj)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/classes/parser.rb', line 121

def getThumbInfo(xml)
  doc = XML::Reader.string(
    xml,
    :options => XML::Parser::Options::NOBLANKS |
    XML::Parser::Options::NOENT
  )
          
  n = -1
  parsed = {}

  while doc.read
    unless doc.node_type == XML::Reader::TYPE_END_ELEMENT
      row = case doc.name
      when "video_id"       then parseRow(:video_id,      :String,  doc)
      when "title"          then parseRow(:title,         :String,  doc)
      when "description"    then parseRow(:description,   :String,  doc)
      when "thumbnail_url"  then parseRow(:thumbnail_url, :String,  doc)
      when "movie_type"     then parseRow(:movie_type,    :String,  doc)
      when "last_res_body"  then parseRow(:last_res_body, :String,  doc)
      when "watch_url"      then parseRow(:watch_url,     :String,  doc)
      when "thumb_type"     then parseRow(:thumb_type,    :String,  doc)

      when "size_high"      then parseRow(:size_high,     :Fixnum,  doc) 
      when "size_low"       then parseRow(:size_low,      :Fixnum,  doc) 
      when "view_counter"   then parseRow(:view_counter,  :Fixnum,  doc) 
      when "comment_num"    then parseRow(:comment_num,   :Fixnum,  doc) 
      when "mylist_counter" then parseRow(:mylist_counter,:Fixnum,  doc) 
      when "embeddable"     then parseRow(:embeddable,    :Fixnum,  doc) 
      when "no_live_play"   then parseRow(:no_live_play,  :Fixnum,  doc) 
      when "user_id"        then parseRow(:user_id,       :Fixnum,  doc) 
      when "first_retrieve" then parseRow(:first_retrieve,:ISO8601, doc)       
      when "length"         then parseRow(:length,        :Time,    doc) 
      when "tags"           then parseRow(:tags,          :Tags,    doc) 
      when "tag"            then parseRow(:tag,           :Tag,     doc) 
      end

      parsed.update(row) if row != nil
    end
  end

  doc.close 
  parsed
end

.mylistAtom(xml) ⇒ HashObj

マイリストのAtomフィードが返すXMLを解析し、ハッシュオブジェクトにして返します。

Returns:

  • (HashObj)


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/classes/parser.rb', line 232

def mylistAtom(xml)    
  doc = XML::Reader.string(
    xml,
    :options => XML::Parser::Options::NOBLANKS |
    XML::Parser::Options::NOENT
  )
          
  n = 0
  parsed = { :mylist => {}, :entry => [{}] }

  while doc.read
    break if doc.name === "entry"

    unless doc.node_type == XML::Reader::TYPE_END_ELEMENT
      row = case doc.name
      when "title" then
        /(マイリスト )(.+)(‐ニコニコ動画)/ =~ parseRow(:title, :String,  doc)[:title]
        { :title => $2 }
      when "id"       then parseRow(:mylist_id,   :mylistId,doc)
      when "subtitle" then parseRow(:description, :String,  doc)
      when "updated"  then parseRow(:updated,     :ISO8601, doc)
      when "name"     then parseRow(:author,      :String,  doc)
      end

      parsed[:mylist].update(row) if row != nil           
    end
  end

  while doc.read
    unless doc.node_type == XML::Reader::TYPE_END_ELEMENT
      # bump up the page number
      if doc.name === "entry"
        n += 1
        parsed[:entry][n] = {}              
      end

      row = case doc.name
      # <title> and <id> are marked up both in mylist and
      # each entry's node. So we need to assign the value to the
      # appropriate variable in accordance with node's location.
      when "title" then parseRow(:title,    :String,  doc)
      when "link"  then parseRow(:video_id, :videoId, doc)
      when "id"    then parseRow(:item_id,  :itemId,  doc)
      when "content"
        doc.read
        html = doc.value   

        /(<p\sclass=\"nico-memo\"\>)([^\<]{1,})/ =~ html
        memo = $2
                 
        /(<p\sclass=\"nico-thumbnail\">.+src=\")(http:\/\/[^\"]{1,})/ =~ html
        thumbnail_url = $2
        
        /(<p\sclass=\"nico-description\"\>)([^\<]{1,})/ =~ html
        description = $2

        /(<strong\sclass\=\"nico-info-length\"\>)([^\<]{1,})/ =~ html
        length = Nicos::Converter.toSeconds($2)

        /(<strong\sclass\=\"nico-info-date\"\>)([^\<]{1,})/ =~ html
        first_retrieve = Nicos::Converter.japToUnix($2)

        /(<strong\sclass\=\"nico-numbers-view\"\>)([^\<]{1,})/ =~ html
        view = Nicos::Converter.commaRemover($2)

        /(<strong\sclass\=\"nico-numbers-res\"\>)([^\<]{1,})/ =~ html
        res = Nicos::Converter.commaRemover($2)

        /(<strong\sclass\=\"nico-numbers-mylist\"\>)([^\<]{1,})/ =~ html
        mylist = Nicos::Converter.commaRemover($2)
        
        {
          :memo             => memo,
          :thumbnail_url    => thumbnail_url,
          :description      => description,
          :length           => length,
          :first_retrieve   => first_retrieve,
          :view             => view,
          :res              => res,
          :mylist           => mylist
        } 
      end 

      parsed[:entry][n].update(row) if row != nil  
    end
  end

  doc.close 
  parsed
end

.parseRow(symbol, type, doc) ⇒ Object



14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/classes/parser.rb', line 14

def parseRow(symbol, type, doc)
  hash = {}

  value = case type
  # common
  when :Fixnum  then
    doc.read
    doc.value.to_i
  when :String  then 
    doc.read
    doc.value
  when :ISO8601 then 
    doc.read
    Nicos::Converter.iso8601ToUnix(doc.value) 
  when :JapDate then 
    doc.read
    Nicos::Converter.japToUnix(doc.value)
  when :Time    then 
    doc.read
    Nicos::Converter.toSeconds(doc.value)

  # for Mylist Atom
  when :mylistId then
    doc.read
    Nicos::Extractor.mylistId(doc.value)
  when :itemId then
    doc.read
    Nicos::Extractor.itemId(doc.value)
  when :videoId then
    doc.move_to_attribute("href")
    Nicos::Extractor.videoId(doc.value) 

  # for getThumbInfo
  when :Tags    then
    doc.move_to_attribute("domain")
    symbol = case doc.value
    when "jp" then :tags_jp
    when "tw" then :tags_tw
    when "de" then :tags_de
    when "es" then :tags_es
    end

    tags = []
    lockedTags = []
    category = nil
    locked = false
    prev = nil
    now = nil

    while doc.read
      unless doc.node_type == XML::Reader::TYPE_END_ENTITY
        # 終了を判別。もっと環境に依存しない上手いやり方があるはず。
        break if doc.name == "tags"

        if prev == :end
          category = false
          locked = false
        end

        doc.move_to_attribute("category")
        category = true if doc.name == "category"

        doc.move_to_attribute("lock")
        locked = true if doc.name == "lock"

        # ノードの開始、値、終了を判別する。
        # 例えば<tag>と<tag lock="1"/>が、どちらも'2'と解釈され、開始と終了が区別しづらい。
        #http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
        nt = doc.node_type 
        now = if (nt == 2 || nt == 1) && prev != :val then :start
        elsif (nt == 2 || nt == 15) && prev == :val then :end
        elsif nt == 3 then :val
        end

        val = doc.read_outer_xml

        #puts
        #puts now              
        #puts val
        #puts "cat:#{category} locked:#{locked}"

        if now == :val
          obj = { :name => val }
          obj.merge!({ :locked => true }) if locked == true
          obj.merge!({ :category => true }) if category == true

          tags.push(obj)
        end

        prev = now
      end
    end

    tags
  end 

  hash[symbol] = value
  hash
end

.tagAtom(xml) ⇒ HashObj

タグ検索のAtomフィードが返すXMLを解析し、ハッシュオブジェクトにして返します。

Returns:

  • (HashObj)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/classes/parser.rb', line 168

def tagAtom(xml)
  doc = XML::Reader.string(
    xml,
    :options => XML::Parser::Options::NOBLANKS |
    XML::Parser::Options::NOENT
  )
          
  n = -1
  parsed = [{}]

  while doc.read
    unless doc.node_type == XML::Reader::TYPE_END_ELEMENT
      case doc.name
      when "entry"
        n += 1
        parsed[n] = {}
      when "title"
        doc.read
        parsed[n][:title] = doc.value
      when "link"
        doc.move_to_attribute("href")
        parsed[n][:video_id] = doc.value.split('/')[4]
      when "published", "updated"
        label = doc.name
        doc.read
        parsed[n][label] = Nicos::Converter.iso8601ToUnix(doc.value)
      when "p"
        doc.move_to_attribute("class")
        case doc.value
        when "nico-thumbnail"
          doc.read
          doc.move_to_attribute("src")
          parsed[n][:thumbnail_url] = doc.value
        when "nico-description"
          doc.read
          parsed[n][:description] = doc.value
        end
      when "strong"
        doc.move_to_attribute("class")
        case doc.value
        when "nico-info-length"
          doc.read
          parsed[n][:length] = Nicos::Converter.toSeconds(doc.value)
        when "nico-info-date"
          label = doc.name
          doc.read
          parsed[n][:first_retrieve] = Nicos::Converter.japToUnix(doc.value)
        when "nico-numbers-view", "nico-numbers-res",
              "nico-numbers-mylist"
          label = doc.value
          doc.read
          parsed[n][label.slice(13,99)] = Nicos::Converter::commaRemover(doc.value)
        end
      end   
    end
  end

  doc.close    
  parsed
end

Instance Method Details

#parseTagObject



115
116
# File 'lib/classes/parser.rb', line 115

def parseTag
end