nicoscraper 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,10 +5,12 @@ require 'rubygems'
5
5
  require 'ruby-debug'
6
6
  require 'kconv'
7
7
 
8
+ require 'namespace.rb'
8
9
  require 'parser.rb'
9
10
  require 'movie.rb'
10
11
  require 'connector.rb'
11
12
 
13
+
12
14
  class Nicos::Mylist
13
15
  def initialize (mylist_id)
14
16
  @mylist_id = mylist_id
@@ -18,6 +20,8 @@ class Nicos::Mylist
18
20
 
19
21
  # 自分に含まれている動画のタイトルをすべての組み合わせにおいて比較し、
20
22
  # 類似度の平均を返す。
23
+ #
24
+ # @return [Fixnum] 編集距離に基づく類似度。上限は1、下限はなし。
21
25
  def getSimilarity
22
26
  l = @movies.length - 1
23
27
  dlc = DamerauLevenshtein
@@ -55,7 +59,8 @@ class Nicos::Mylist
55
59
  return similarity
56
60
  end
57
61
 
58
- def getInfo
62
+ # 自分に含まれている動画のタイトルをすべての組み合わせにおいて比較し、
63
+ def getInfoHtml
59
64
  con = Nicos::Connector::Html.new('mech')
60
65
  reqUrl = 'http://www.nicovideo.jp' +
61
66
  '/mylist/' + @mylist_id.to_s
@@ -139,8 +144,11 @@ class Nicos::Mylist
139
144
  @movies.push(movie)
140
145
  }
141
146
  end
142
-
143
- def getInfoLt
147
+
148
+ # マイリストのAtomフィードから、マイリストとそれに含まれる動画の情報を取得する。
149
+ #
150
+ # @return [Fixnum] 編集距離に基づく類似度。上限は1、下限はなし。
151
+ def getInfo
144
152
  con = Nicos::Connector::MylistAtom.new()
145
153
  host = 'www.nicovideo.jp'
146
154
  puts @mylist_id
@@ -168,6 +176,7 @@ class Nicos::Mylist
168
176
  end
169
177
  end
170
178
 
179
+ # {Movie#set} を参照。
171
180
  def set(paramObj)
172
181
  paramObj.each_key { |key|
173
182
  param = paramObj[key]
@@ -205,19 +214,104 @@ class Nicos::Mylist
205
214
  }
206
215
  end
207
216
 
217
+ # このインスタンスがgetInfo等によって正常に情報を取得できている場合、trueとなる。
218
+ # 各種メソッドの実行には、これがtrueであることが要求される。
219
+ #
220
+ # @return [Boolean]
208
221
  attr_accessor :available
209
-
210
- attr_accessor :mylist_id
211
- attr_accessor :user_id
212
- attr_accessor :title
213
- attr_accessor :description
214
- attr_accessor :public
215
- attr_accessor :default_sort
216
- attr_accessor :create_time
222
+
223
+ # マイリストID
224
+ #
225
+ # @return [Fixnum]
226
+ # <b>取得可能なメソッド</b>
227
+ # {Nicos::Movie#getInfo Mylist::getInfo}
228
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
229
+ attr_accessor :mylist_id
230
+
231
+ # ユーザID
232
+ #
233
+ # @return [Fixnum]
234
+ # <b>取得可能なメソッド</b>
235
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
236
+ attr_accessor :user_id
237
+
238
+ # マイリストのタイトル
239
+ #
240
+ # @return [Fixnum]
241
+ # <b>取得可能なメソッド</b>
242
+ # {Nicos::Movie#getInfo Mylist::getInfo}
243
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
244
+ attr_accessor :title
245
+
246
+ # マイリストの説明文
247
+ #
248
+ # @return [Fixnum]
249
+ # <b>取得可能なメソッド</b>
250
+ # {Nicos::Movie#getInfo Mylist::getInfo}
251
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
252
+ attr_accessor :description
253
+
254
+ # 公開設定
255
+ #
256
+ # 調査中
257
+ # @return [Fixnum]
258
+ # <b>取得可能なメソッド</b>
259
+ # {Nicos::Movie#getInfo Mylist::getInfo}
260
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
261
+ attr_accessor :public
262
+
263
+ # ソート順の設定
264
+ #
265
+ # ソート順の設定
266
+ # @return [Fixnum]
267
+ # <b>取得可能なメソッド</b>
268
+ # {Nicos::Movie#getInfo Mylist::getInfo}
269
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
270
+ attr_accessor :default_sort
271
+
272
+ # マイリスト作成日時
273
+ #
274
+ # @return [Fixnum]
275
+ # <b>取得可能なメソッド</b>
276
+ # {Nicos::Movie#getInfo Mylist::getInfo}
277
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
278
+ attr_accessor :create_time
279
+
280
+ # マイリストの更新日時
281
+ #
282
+ # @return [Fixnum]
283
+ # <b>取得可能なメソッド</b>
284
+ # {Nicos::Movie#getInfo Mylist::getInfo}
285
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
217
286
  attr_accessor :update_time
218
- attr_accessor :icon_id
219
- attr_accessor :sort_order
220
- attr_accessor :author
221
-
222
- attr_accessor :movies
287
+
288
+ # アイコンの色?
289
+ #
290
+ # @return [Fixnum]
291
+ # <b>取得可能なメソッド</b>
292
+ # {Nicos::Movie#getInfo Mylist::getInfo}
293
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
294
+ attr_accessor :icon_id
295
+
296
+ # 現在のソート順
297
+ #
298
+ # @return [Fixnum]
299
+ # <b>取得可能なメソッド</b>
300
+ # {Nicos::Movie#getInfo Mylist::getInfo}
301
+ # {Nicos::Movie#getInfo Mylist::getHtmlInfo}
302
+ attr_accessor :sort_order
303
+
304
+ # 作成者の名前
305
+ #
306
+ # @return [Fixnum]
307
+ # <b>取得可能なメソッド</b>
308
+ # {Nicos::Movie#getInfo Mylist::getInfo}
309
+ attr_accessor :author
310
+
311
+ # マイリストが含む動画インスタンスの配列
312
+ #
313
+ # getInfo等のメソッドを利用した際に、そのマイリストが含む動画の
314
+ # インスタンスが配列として自動的に作られ、moviesに収められる。
315
+ # @return [Array<Movie>]
316
+ attr_accessor :movies
223
317
  end
@@ -0,0 +1,3 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Nicos end
@@ -4,9 +4,14 @@ $:.unshift File.dirname(__FILE__)
4
4
  require 'rubygems'
5
5
  require 'xml'
6
6
  require 'time'
7
+
8
+ require 'namespace.rb'
7
9
  require 'converter.rb'
8
10
 
9
11
  module Nicos::Parser
12
+ # getThumbInfoが返すXMLを解析し、ハッシュオブジェクトにして返します。
13
+ #
14
+ # @return [HashObj]
10
15
  def getThumbInfo(xml)
11
16
  doc = XML::Reader.string(
12
17
  xml,
@@ -54,10 +59,13 @@ module Nicos::Parser
54
59
  end
55
60
  end
56
61
 
57
- doc.close
62
+ doc.close
58
63
  parsed
59
64
  end
60
-
65
+
66
+ # タグ検索のAtomフィードが返すXMLを解析し、ハッシュオブジェクトにして返します。
67
+ #
68
+ # @return [HashObj]
61
69
  def tagAtom(xml)
62
70
  doc = XML::Reader.string(
63
71
  xml,
@@ -116,7 +124,10 @@ module Nicos::Parser
116
124
  doc.close
117
125
  parsed
118
126
  end
119
-
127
+
128
+ # マイリストのAtomフィードが返すXMLを解析し、ハッシュオブジェクトにして返します。
129
+ #
130
+ # @return [HashObj]
120
131
  def mylistAtom(xml)
121
132
  doc = XML::Reader.string(
122
133
  xml,
@@ -214,7 +225,6 @@ module Nicos::Parser
214
225
  end
215
226
 
216
227
  doc.close
217
-
218
228
  parsed
219
229
  end
220
230
 
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-# -*- encoding: utf-8 -*-
2
- $:.unshift File.dirname(__FILE__)
2
+ $:.unshift File.dirname(__FILE__)
3
3
 
4
4
  require 'rubygems'
5
5
  require 'ruby-debug'
@@ -8,15 +8,17 @@ require 'time'
8
8
  require 'mechanize'
9
9
  require 'kconv'
10
10
 
11
+ require 'namespace.rb'
11
12
  require 'parser.rb'
12
13
 
13
14
  module Nicos::Searcher
15
+ # :nodocs:
14
16
  class ByTagSuper
15
17
  private
16
18
 
17
19
  def get(tag, sort, page, method, waitObj)
18
20
  paramAry = []
19
-
21
+
20
22
  case sort
21
23
  when 'comment_new'
22
24
  sortStr = ''
@@ -43,12 +45,12 @@ module Nicos::Searcher
43
45
  when 'length_short'
44
46
  sortStr = 'sort=l&order=a'
45
47
  end
46
-
48
+
47
49
  paramAry.push("page=#{page}") if page != 1
48
50
  paramAry.push(sortStr)
49
51
  if method == "atom" then paramAry.push("rss=atom&numbers=1") end
50
52
  param = tag + "?" + paramAry.join('&')
51
-
53
+
52
54
  host = 'www.nicovideo.jp'
53
55
  entity = '/tag/' + param
54
56
 
@@ -60,7 +62,8 @@ module Nicos::Searcher
60
62
  termFlag = false
61
63
  page = 1
62
64
  movieObjAry = []
63
-
65
+ order = "continue"
66
+
64
67
  begin
65
68
  response = get(
66
69
  tag,
@@ -73,29 +76,26 @@ module Nicos::Searcher
73
76
  if response["order"] == "success"
74
77
  result = parse(response["body"])
75
78
  result.each { |each|
76
- movie = Nicos::Movie.new(each["video_id"])
79
+ movie = Nicos::Movie.new(each["video_id"])
77
80
  each["available"] = true
78
81
  movie.set(each)
79
82
  movieObjAry.push(movie)
80
83
  }
81
-
82
- termFlag = block.call(movieObjAry, page)
83
- else
84
- termFlag = true
85
84
  end
86
-
85
+
86
+ order = block.call(movieObjAry, page)
87
87
  page += 1
88
- end until termFlag
88
+ end until order != "continue"
89
89
  end
90
90
  end
91
91
 
92
- class ByTag < ByTagSuper
92
+ class ByTagHtml < ByTagSuper
93
93
  def initialize
94
94
  @numOfSearched = 32
95
95
  @incrAmt = 0.2
96
96
 
97
97
  @connector = Nicos::Connector.new('mech')
98
-
98
+
99
99
  # HTML中の各パラメータの所在を示すXPath
100
100
  @videoIdXP = "//div[@class='uad_thumbfrm']/table/tr/td/p/a"
101
101
  @lengthXP = "//div[@class='uad_thumbfrm']/table/tr/td/p[2]/span"
@@ -104,10 +104,12 @@ module Nicos::Searcher
104
104
  @mylistXP = "//div[@class='uad_thumbfrm']/table/tr/td[2]/div/nobr[3]/a/strong"
105
105
  @adXP = "//div[@class='uad_thumbfrm']/table/tr/td[2]/div/nobr[4]/a/strong"
106
106
  end
107
-
107
+
108
+ private
109
+
108
110
  def parse(movieNum)
109
111
  result = []
110
-
112
+
111
113
  video_id = /(sm|nm)[0-9]{1,}/.match(@connector.mech.page.search(@videoIdXP)[movieNum]['href'])[0]
112
114
  lengthStr = @connector.mech.page.search(@lengthXP)[movieNum].text.split(/\:/)
113
115
  length = lengthStr[0].to_i * 60 + lengthStr[1].to_i
@@ -129,27 +131,111 @@ module Nicos::Searcher
129
131
  "ad" => ad
130
132
  })
131
133
  end
132
-
133
- def execute(tag, sort, waitObj, &block)
134
+
135
+ public
136
+
137
+ # @param [String] tag
138
+ # @param [String] sortMethod
139
+ # @param [HashObj] waitConfig
140
+ def execute(tag, sortMethod, waitConfig, &block)
134
141
  loop(tag, sort, "mech", waitObj) { |result, page|
135
142
  block.call(result, page)
136
143
  }
137
144
  end
138
145
  end
139
146
 
140
- class ByTagLt < ByTagSuper
147
+ class ByTag < ByTagSuper
141
148
  def initialize
142
149
  @numOfSearched = 32
143
150
  @incrAmt = 0.2
144
151
  @connector = Nicos::Connector::TagAtom.new()
145
152
  end
146
153
 
154
+ private
155
+
147
156
  def parse(xml)
148
157
  Nicos::Parser.tagAtom(xml)
149
158
  end
150
-
151
- def execute(tag, sort, waitObj, &block)
152
- loop(tag, sort, "atom", waitObj) { |result, page|
159
+
160
+ public
161
+
162
+ # 実行
163
+ #
164
+ # @param [String] tag 検索したいタグ文字列
165
+ # @param [String] sortMethod ソート方法
166
+ #==sortMethod: ソート方法
167
+ # *comment_new*
168
+ # コメントが新しい順
169
+ #
170
+ # *comment_old*
171
+ # コメントが新しい順
172
+ #
173
+ # *view_many*
174
+ # 再生数が多い順
175
+ #
176
+ # *view_few*
177
+ # 再生数が少ない順
178
+ #
179
+ # *comment_many*
180
+ # コメントが多い順
181
+ #
182
+ # *comment_few*
183
+ # コメントが少ない順
184
+ #
185
+ # *mylist_many*
186
+ # マイリスト登録が多い順
187
+ #
188
+ # *mylist_few*
189
+ # マイリスト登録が少ない順
190
+ #
191
+ # *post_new*
192
+ # 登録が新しい順
193
+ #
194
+ # *post_old*
195
+ # 登録が少ない順
196
+ #
197
+ # *length_long*
198
+ # 再生時間が長い順
199
+ #
200
+ # *length_short*
201
+ # 再生時間が短い順
202
+ #
203
+ # @param [HashObj] waitConfig ウェイト設定
204
+ #==waitConfig: ウェイト設定
205
+ # <b>ウェイトの変更に際しては、READMEの注意点と免責事項を事前にお読み下さい。</b>
206
+ #
207
+ # 以下のフォーマットのハッシュオブジェクトを与えて下さい。これはデフォルト設定です。
208
+ # また、ハッシュは以下のキーを全て用意する必要はありません。
209
+ # 変更したい部分のキーと値のみを持つハッシュオブジェクトを作って下さい。
210
+ #
211
+ # @waitConfig = {
212
+ # 'seqAccLimit' => 10, # 連続してリクエストする回数
213
+ # 'afterSeq' => 10, # 連続リクエスト後のウェイト(以下、単位は全て秒)
214
+ # 'each' => 1, # 連続リクエスト時の、1リクエスト毎のウェイト
215
+ # 'increment' => 1, # アクセス拒絶時の、次回以降の1リクエスト毎のウェイトの増加量
216
+ #
217
+ # 'deniedSeqReq'=> { # 連続アクセスを拒否された際の設定(以下同じ)
218
+ # 'retryLimit' => 3, # 再試行の上限回数
219
+ # 'wait' => 120 # 次のアクセスまでのウェイト
220
+ # },
221
+ #
222
+ # 'serverIsBusy'=> { # サーバ混雑時
223
+ # 'retryLimit' => 3,
224
+ # 'wait' => 120
225
+ # },
226
+ #
227
+ # 'serviceUnavailable' => { # 503が返ってきた時
228
+ # 'retryLimit' => 3,
229
+ # 'wait' => 120
230
+ # },
231
+ #
232
+ # 'timedOut' => { # タイムアウト時
233
+ # 'retryLimit' => 3,
234
+ # 'wait' => 10
235
+ # }
236
+ # }
237
+ def execute(tag, sortMethod, waitConfig, &block)
238
+ loop(tag, sortMethod, "atom", waitConfig) { |result, page|
153
239
  block.call(result, page)
154
240
  }
155
241
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nicoscraper}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Masami Yonehara}]
@@ -15,8 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.email = %q{zeitdiebe@gmail.com}
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE.txt",
18
- "README.md",
19
- "README.rdoc"
18
+ "README.md"
20
19
  ]
21
20
  s.files = [
22
21
  ".document",
@@ -24,15 +23,14 @@ Gem::Specification.new do |s|
24
23
  "Gemfile.lock",
25
24
  "LICENSE.txt",
26
25
  "README.md",
27
- "README.rdoc",
28
26
  "Rakefile",
29
27
  "VERSION",
28
+ "index.html",
30
29
  "lib/connector.rb",
31
30
  "lib/converter.rb",
32
31
  "lib/movie.rb",
33
32
  "lib/mylist.rb",
34
- "lib/nicos.rb",
35
- "lib/nicoscraper.rb",
33
+ "lib/namespace.rb",
36
34
  "lib/parser.rb",
37
35
  "lib/searcher.rb",
38
36
  "nicoscraper.gemspec",