Class: Crown::Hatena::Bookmark::LinkTrace
- Inherits:
-
Object
- Object
- Crown::Hatena::Bookmark::LinkTrace
- Defined in:
- lib/crown/hatena/bookmark.rb
Overview
————————————————————— #
LinkTrace
指定した Web サイトのはてなブックマーク一覧から順に URL を
取得するクラス.2 ページ目以降の結果は,html のヘッダ情報の
next 属性からページの URL を推測して取得する.
————————————————————— #
Constant Summary collapse
- @@reserved =
[ "/hotentry", "/entrylist", "/news", "/video", "/asin" ]
Instance Method Summary collapse
-
#get ⇒ Object
———————————————————– #.
-
#initialize(path, proxy_host = nil, proxy_port = nil) ⇒ LinkTrace
constructor
———————————————————– # initialize ———————————————————– #.
-
#more? ⇒ Boolean
———————————————————– #.
-
#reset ⇒ Object
———————————————————– # reset ———————————————————– #.
Constructor Details
#initialize(path, proxy_host = nil, proxy_port = nil) ⇒ LinkTrace
———————————————————– #
initialize
———————————————————– #
60 61 62 63 64 65 66 67 |
# File 'lib/crown/hatena/bookmark.rb', line 60 def initialize(path, proxy_host = nil, proxy_port = nil) @session = Net::HTTP.new("b.hatena.ne.jp", 80, proxy_host, proxy_port) @basename = String.new @basename.concat("/") if (path[0] != 47) @basename.concat(path) @basename.concat("/") if (@basename.match(/^\/[\w\-]+$/) != nil && !@@reserved.include?(@basename)) @path = String.new(@basename) end |
Instance Method Details
#get ⇒ Object
———————————————————– #
get
対象となるはてなブックマーク URL を返す.get() は html の
next 属性から推測された「現在のページ」に列挙されてある
全てのはてなブックマーク URL を配列に格納して返す.
———————————————————– #
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/crown/hatena/bookmark.rb', line 100 def get() result = Array.new return result if (@path == nil) response = Crown::HTTP.get(@session, @path) return result if (response == nil || response.code.to_i != 200) guess(response.body) html = Nokogiri::HTML.parse(response.body) block = html.search('//ul[starts-with(@class, "hotentry")]') block = html.search('//ul[starts-with(@class, "videolist")]') if block.empty? return result if block.empty? block[0].search("h3/a").each { |node| result.push(node['href']) } # 無限ループする場合があるので,現在のページから 1件も結果が # 取得できないときはこれ以上の検索を止める. @path = nil if (result.empty?) return result end |
#more? ⇒ Boolean
———————————————————– #
more?
まだ取得できる URL が存在するかどうかを判定する.next?()
は html の next 属性から次のページが推測できているか
どうかで判定している.
———————————————————– #
87 88 89 |
# File 'lib/crown/hatena/bookmark.rb', line 87 def more?() return @path != nil end |
#reset ⇒ Object
———————————————————– #
reset
———————————————————– #
72 73 74 75 76 |
# File 'lib/crown/hatena/bookmark.rb', line 72 def reset() @path = String.new(@basename) @session.finish if (@session.active?) return self end |