crown 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +11 -3
- data/README.rdoc +23 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/crown.gemspec +28 -7
- data/example/cgmcount.rb +65 -0
- data/example/feedcount.rb +50 -0
- data/example/sbmcount.rb +5 -1
- data/lib/crown.rb +4 -14
- data/lib/crown/backtype.rb +29 -25
- data/lib/crown/backtype/counter.rb +79 -0
- data/lib/crown/buzzurl.rb +15 -28
- data/lib/crown/buzzurl/counter.rb +88 -0
- data/lib/crown/cgm.rb +98 -0
- data/lib/crown/cgm/countable.rb +130 -0
- data/lib/crown/cgm/summarizable.rb +130 -0
- data/lib/crown/delicious.rb +14 -29
- data/lib/crown/delicious/counter.rb +88 -0
- data/lib/crown/facebook.rb +32 -27
- data/lib/crown/facebook/counter.rb +76 -0
- data/lib/crown/facebook/entry.rb +86 -0
- data/lib/crown/hatena/bookmark.rb +26 -168
- data/lib/crown/hatena/bookmark/counter.rb +82 -0
- data/lib/crown/hatena/bookmark/entry.rb +108 -0
- data/lib/crown/hatena/bookmark/linktrace.rb +135 -0
- data/lib/crown/http-wrapper.rb +201 -0
- data/lib/crown/livedoor/clip.rb +15 -18
- data/lib/crown/livedoor/clip/counter.rb +96 -0
- data/lib/crown/livedoor/reader.rb +16 -19
- data/lib/crown/livedoor/reader/counter.rb +82 -0
- data/lib/crown/topsy.rb +20 -21
- data/lib/crown/topsy/counter.rb +78 -0
- data/lib/crown/tweetmeme.rb +20 -21
- data/lib/crown/tweetmeme/counter.rb +78 -0
- data/lib/crown/twitter.rb +14 -31
- data/lib/crown/{http.rb → twitter/uri.rb} +24 -39
- data/lib/crown/twitter/uri/counter.rb +86 -0
- data/lib/crown/twitter/user.rb +76 -0
- data/lib/crown/twitter/user/counter.rb +85 -0
- data/lib/crown/twitter/user/entry.rb +96 -0
- data/lib/crown/yahoo.rb +7 -3
- data/lib/crown/yahoo/bookmark.rb +23 -28
- data/lib/crown/yahoo/bookmark/counter.rb +84 -0
- data/test/{crown_test.rb → crown-test.rb} +46 -21
- data/test/{test_helper.rb → test-helper.rb} +1 -1
- metadata +30 -9
- data/lib/crown/amazon.rb +0 -157
- data/lib/crown/amazon/ecs.rb +0 -385
@@ -0,0 +1,82 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# --------------------------------------------------------------------------- #
|
3
|
+
#
|
4
|
+
# hatena/bookmark/counter.rb
|
5
|
+
#
|
6
|
+
# Copyright (c) 2008 - 2012, clown.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions
|
10
|
+
# are met:
|
11
|
+
#
|
12
|
+
# - Redistributions of source code must retain the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer.
|
14
|
+
# - Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in the
|
16
|
+
# documentation and/or other materials provided with the distribution.
|
17
|
+
# - No names of its contributors may be used to endorse or promote
|
18
|
+
# products derived from this software without specific prior written
|
19
|
+
# permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
27
|
+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
# --------------------------------------------------------------------------- #
|
34
|
+
module Crown
|
35
|
+
module Hatena
|
36
|
+
# --------------------------------------------------------------- #
|
37
|
+
#
|
38
|
+
# BookmarkCounter
|
39
|
+
#
|
40
|
+
# �w�肵�� URL �̃u�b�N�}�[�N�����擾����N���X�D
|
41
|
+
#
|
42
|
+
# --------------------------------------------------------------- #
|
43
|
+
class BookmarkCounter
|
44
|
+
require 'cgi'
|
45
|
+
require 'crown/cgm/countable'
|
46
|
+
|
47
|
+
# --------------------------------------------------------------- #
|
48
|
+
# Mix-in modules
|
49
|
+
# --------------------------------------------------------------- #
|
50
|
+
include Crown::CGM::Countable
|
51
|
+
|
52
|
+
private
|
53
|
+
# --------------------------------------------------------------- #
|
54
|
+
# address
|
55
|
+
# --------------------------------------------------------------- #
|
56
|
+
def address()
|
57
|
+
return 'api.b.st-hatena.com'
|
58
|
+
end
|
59
|
+
|
60
|
+
# --------------------------------------------------------------- #
|
61
|
+
# path
|
62
|
+
# --------------------------------------------------------------- #
|
63
|
+
def path()
|
64
|
+
return '/entry.count'
|
65
|
+
end
|
66
|
+
|
67
|
+
# --------------------------------------------------------------- #
|
68
|
+
# query
|
69
|
+
# --------------------------------------------------------------- #
|
70
|
+
def query(uri, options)
|
71
|
+
return '?url=' + CGI.escape(uri)
|
72
|
+
end
|
73
|
+
|
74
|
+
# --------------------------------------------------------------- #
|
75
|
+
# parse
|
76
|
+
# --------------------------------------------------------------- #
|
77
|
+
def parse(body)
|
78
|
+
return body.to_i
|
79
|
+
end
|
80
|
+
end # BookmarkCounter
|
81
|
+
end # Hatena
|
82
|
+
end # Crown
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# --------------------------------------------------------------------------- #
|
3
|
+
#
|
4
|
+
# hatena/bookmark/entry.rb
|
5
|
+
#
|
6
|
+
# Copyright (c) 2008 - 2012, clown.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions
|
10
|
+
# are met:
|
11
|
+
#
|
12
|
+
# - Redistributions of source code must retain the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer.
|
14
|
+
# - Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in the
|
16
|
+
# documentation and/or other materials provided with the distribution.
|
17
|
+
# - No names of its contributors may be used to endorse or promote
|
18
|
+
# products derived from this software without specific prior written
|
19
|
+
# permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
27
|
+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
# --------------------------------------------------------------------------- #
|
34
|
+
module Crown
|
35
|
+
module Hatena
|
36
|
+
# --------------------------------------------------------------- #
|
37
|
+
# BookmarkEntry
|
38
|
+
# --------------------------------------------------------------- #
|
39
|
+
class BookmarkEntry
|
40
|
+
require 'cgi'
|
41
|
+
require 'crown/cgm/summarizable'
|
42
|
+
require 'rubygems'
|
43
|
+
require 'json'
|
44
|
+
|
45
|
+
# --------------------------------------------------------------- #
|
46
|
+
# Mix-in modules
|
47
|
+
# --------------------------------------------------------------- #
|
48
|
+
include Crown::CGM::Summarizable
|
49
|
+
|
50
|
+
# --------------------------------------------------------------- #
|
51
|
+
# structures
|
52
|
+
# --------------------------------------------------------------- #
|
53
|
+
User = Struct.new(:tags, :date, :comment)
|
54
|
+
Related = Struct.new(:eid, :uri, :title, :count)
|
55
|
+
Response = Struct.new(:eid, :uri, :image, :title, :count, :users, :related)
|
56
|
+
|
57
|
+
private
|
58
|
+
# --------------------------------------------------------------- #
|
59
|
+
# address
|
60
|
+
# --------------------------------------------------------------- #
|
61
|
+
def address()
|
62
|
+
return 'b.hatena.ne.jp'
|
63
|
+
end
|
64
|
+
|
65
|
+
# --------------------------------------------------------------- #
|
66
|
+
# path
|
67
|
+
# --------------------------------------------------------------- #
|
68
|
+
def path()
|
69
|
+
return '/entry/json/'
|
70
|
+
end
|
71
|
+
|
72
|
+
# --------------------------------------------------------------- #
|
73
|
+
# query
|
74
|
+
# --------------------------------------------------------------- #
|
75
|
+
def query(uri, options)
|
76
|
+
return '?url=' + CGI.escape(uri)
|
77
|
+
end
|
78
|
+
|
79
|
+
# --------------------------------------------------------------- #
|
80
|
+
# parse
|
81
|
+
# --------------------------------------------------------------- #
|
82
|
+
def parse(body)
|
83
|
+
json = JSON.parse(body)
|
84
|
+
|
85
|
+
# ���[�U���̉��
|
86
|
+
users = Hash.new
|
87
|
+
json['bookmarks'].each { |item|
|
88
|
+
tags = Array.new
|
89
|
+
item['tags'].each { |tag|
|
90
|
+
tags.push(tag)
|
91
|
+
}
|
92
|
+
date = Time.local(*item['timestamp'].split(/\W/))
|
93
|
+
entry = User.new(tags, date, item['comment'])
|
94
|
+
users[item['user']] = entry
|
95
|
+
}
|
96
|
+
|
97
|
+
# �֘A�L���̉��
|
98
|
+
related = Array.new()
|
99
|
+
json['related'].each { |item|
|
100
|
+
entry = Related.new(item['eid'].to_i, item['url'], item['title'], item['count'].to_i)
|
101
|
+
related.push(entry)
|
102
|
+
}
|
103
|
+
|
104
|
+
return Response.new(json['eid'].to_i, json['url'], json['screenshot'], json['title'], json['count'].to_i, users, related)
|
105
|
+
end
|
106
|
+
end # BookmarkEntry
|
107
|
+
end # Hatena
|
108
|
+
end # Crown
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# --------------------------------------------------------------------------- #
|
3
|
+
#
|
4
|
+
# hatena/bookmark/linktrace.rb
|
5
|
+
#
|
6
|
+
# Copyright (c) 2008 - 2011, clown.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions
|
10
|
+
# are met:
|
11
|
+
#
|
12
|
+
# - Redistributions of source code must retain the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer.
|
14
|
+
# - Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in the
|
16
|
+
# documentation and/or other materials provided with the distribution.
|
17
|
+
# - No names of its contributors may be used to endorse or promote
|
18
|
+
# products derived from this software without specific prior written
|
19
|
+
# permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
27
|
+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
# --------------------------------------------------------------------------- #
|
34
|
+
module Crown
|
35
|
+
module Hatena
|
36
|
+
module Bookmark
|
37
|
+
# --------------------------------------------------------------- #
|
38
|
+
#
|
39
|
+
# LinkTrace
|
40
|
+
#
|
41
|
+
# �w�肵�� Web �T�C�g�̂͂Ăȃu�b�N�}�[�N�ꗗ���珇�� URL ��
|
42
|
+
# �擾����N���X�D2 �y�[�W�ڈȍ~�̌��ʂ́Chtml �̃w�b�_����
|
43
|
+
# next ��������y�[�W�� URL �𐄑����Ď擾����D
|
44
|
+
#
|
45
|
+
# --------------------------------------------------------------- #
|
46
|
+
class LinkTrace
|
47
|
+
@@reserved = [ "/hotentry", "/entrylist", "/news", "/video", "/asin" ]
|
48
|
+
|
49
|
+
# ----------------------------------------------------------- #
|
50
|
+
# initialize
|
51
|
+
# ----------------------------------------------------------- #
|
52
|
+
def initialize(path, proxy_host = nil, proxy_port = nil)
|
53
|
+
@session = Crown::HTTPWrapper.new("b.hatena.ne.jp", 80, proxy_host, proxy_port)
|
54
|
+
@basename = String.new
|
55
|
+
@basename.concat("/") if (path[0] != 47)
|
56
|
+
@basename.concat(path)
|
57
|
+
@basename.concat("/") if (@basename.match(/^\/[\w\-]+$/) != nil && !@@reserved.include?(@basename))
|
58
|
+
@path = String.new(@basename)
|
59
|
+
end
|
60
|
+
|
61
|
+
# ----------------------------------------------------------- #
|
62
|
+
# reset
|
63
|
+
# ----------------------------------------------------------- #
|
64
|
+
def reset()
|
65
|
+
@path = String.new(@basename)
|
66
|
+
@session.finish if (@session.active?)
|
67
|
+
return self
|
68
|
+
end
|
69
|
+
|
70
|
+
# ----------------------------------------------------------- #
|
71
|
+
#
|
72
|
+
# more?
|
73
|
+
#
|
74
|
+
# �܂��擾�ł��� URL �����݂��邩�ǂ����肷��Dmore?()
|
75
|
+
# �� html �� next �������玟�̃y�[�W�������ł��Ă��邩
|
76
|
+
# �ǂ����Ŕ��肵�Ă���D
|
77
|
+
#
|
78
|
+
# ----------------------------------------------------------- #
|
79
|
+
def more?()
|
80
|
+
return @path != nil
|
81
|
+
end
|
82
|
+
|
83
|
+
# ----------------------------------------------------------- #
|
84
|
+
#
|
85
|
+
# get
|
86
|
+
#
|
87
|
+
# �ΏۂƂȂ�͂Ăȃu�b�N�}�[�N URL ��Ԃ��Dget() �� html ��
|
88
|
+
# next �������琄�����ꂽ�u���݂̃y�[�W�v�ɗ���Ă���
|
89
|
+
# �S�Ă̂͂Ăȃu�b�N�}�[�N URL ��z��Ɋi�[���ĕԂ��D
|
90
|
+
#
|
91
|
+
# ----------------------------------------------------------- #
|
92
|
+
def get()
|
93
|
+
result = Array.new
|
94
|
+
|
95
|
+
return result if (@path == nil)
|
96
|
+
response = @session.get(@path)
|
97
|
+
return result if (response == nil || response.code.to_i != 200)
|
98
|
+
guess(response.body)
|
99
|
+
|
100
|
+
html = Nokogiri::HTML.parse(response.body)
|
101
|
+
block = html.search('//ul[starts-with(@class, "hotentry")]')
|
102
|
+
block = html.search('//ul[starts-with(@class, "videolist")]') if block.empty?
|
103
|
+
return result if block.empty?
|
104
|
+
block[0].search("h3/a").each { |node|
|
105
|
+
result.push(node['href'])
|
106
|
+
}
|
107
|
+
|
108
|
+
# �������[�v����ꍇ������̂ŁC���݂̃y�[�W���� 1�������ʂ�
|
109
|
+
# �擾�ł��Ȃ��Ƃ��͂���ȏ�̌������~�߂�D
|
110
|
+
@path = nil if (result.empty?)
|
111
|
+
|
112
|
+
return result
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
# ----------------------------------------------------------- #
|
117
|
+
# guess
|
118
|
+
# ----------------------------------------------------------- #
|
119
|
+
def guess(str)
|
120
|
+
link = str.scan(/<link rel="next" href="(.+?)".*?>/m).to_s
|
121
|
+
if (link.empty?)
|
122
|
+
@path = nil
|
123
|
+
elsif (link[0] == 47)
|
124
|
+
@path = link
|
125
|
+
elsif (link[0] == 63)
|
126
|
+
@path.gsub!(/\?.*$/, "")
|
127
|
+
@path.concat(link)
|
128
|
+
else
|
129
|
+
@path = @basename + link
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end # LinkTrace
|
133
|
+
end # Bookmark
|
134
|
+
end # Hatena
|
135
|
+
end # Crown
|
@@ -0,0 +1,201 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# --------------------------------------------------------------------------- #
|
3
|
+
#
|
4
|
+
# http-wrapper.rb
|
5
|
+
#
|
6
|
+
# Copyright (c) 2008 - 2012, clown.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions
|
10
|
+
# are met:
|
11
|
+
#
|
12
|
+
# - Redistributions of source code must retain the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer.
|
14
|
+
# - Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in the
|
16
|
+
# documentation and/or other materials provided with the distribution.
|
17
|
+
# - No names of its contributors may be used to endorse or promote
|
18
|
+
# products derived from this software without specific prior written
|
19
|
+
# permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
27
|
+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
# --------------------------------------------------------------------------- #
|
34
|
+
module Crown
|
35
|
+
require 'net/http'
|
36
|
+
Net::HTTP.version_1_2
|
37
|
+
|
38
|
+
# ------------------------------------------------------------------- #
|
39
|
+
# HTTPWrapper
|
40
|
+
# ------------------------------------------------------------------- #
|
41
|
+
class HTTPWrapper
|
42
|
+
require 'uri'
|
43
|
+
|
44
|
+
# ------------------------------------------------------------------- #
|
45
|
+
# accessor
|
46
|
+
# ------------------------------------------------------------------- #
|
47
|
+
attr_accessor :retry_limit, :retry_interval
|
48
|
+
|
49
|
+
# ------------------------------------------------------------------- #
|
50
|
+
# initialize
|
51
|
+
# ------------------------------------------------------------------- #
|
52
|
+
def initialize(address, port = 80, proxy_addr = nil, proxy_port = nil)
|
53
|
+
@http = Net::HTTP.new(address, port, proxy_addr, proxy_port)
|
54
|
+
@retry_limit = 3
|
55
|
+
@retry_interval = 2.0
|
56
|
+
end
|
57
|
+
|
58
|
+
# ------------------------------------------------------------------- #
|
59
|
+
# HTTPWrapper.start
|
60
|
+
# ------------------------------------------------------------------- #
|
61
|
+
def HTTPWrapper.start(address, port = 80, proxy_addr = nil, proxy_port = nil, &block)
|
62
|
+
return HTTPWrapper.new(address, port, proxy_addr, proxy_port).start(&block)
|
63
|
+
end
|
64
|
+
|
65
|
+
# ------------------------------------------------------------------- #
|
66
|
+
#
|
67
|
+
# get
|
68
|
+
#
|
69
|
+
# サーバの状況によって失敗する事があるので,最大で retry_limit 回
|
70
|
+
# 通信が成功するまで get() を試す.また,3xx 系のうち以下の 3 つの
|
71
|
+
# ステータスコードが返ってきた場合は,ロケーション・フィールド
|
72
|
+
# によって与えられた URI へ再度 GET を試みる.
|
73
|
+
#
|
74
|
+
# - 301: Moved Permanently
|
75
|
+
# - 302: Moved Temporarily
|
76
|
+
# - 303: See Other
|
77
|
+
#
|
78
|
+
# ------------------------------------------------------------------- #
|
79
|
+
def get(path, header = nil)
|
80
|
+
n = 0
|
81
|
+
begin
|
82
|
+
@http.start if (!@http.active?)
|
83
|
+
response = @http.get(path, header)
|
84
|
+
code = response.code.to_i
|
85
|
+
while (code == 301 || code == 302 || code == 303)
|
86
|
+
@http.finish if (@http.active?)
|
87
|
+
|
88
|
+
# リダイレクト先へ接続
|
89
|
+
uri = URI.parse(response['Location'])
|
90
|
+
@http = Net::HTTP.new(uri.host, uri.port, @http.proxy_address, @http.proxy_port)
|
91
|
+
path = String.new(uri.path)
|
92
|
+
path.concat("?").concat(uri.query) if (uri.query != nil)
|
93
|
+
|
94
|
+
@http.start
|
95
|
+
response = @http.get(path, header)
|
96
|
+
code = response.to_i
|
97
|
+
end
|
98
|
+
return response
|
99
|
+
rescue Exception => e
|
100
|
+
puts(e.message)
|
101
|
+
@http.finish if (@http.active?)
|
102
|
+
if (n < @retry_limit)
|
103
|
+
sleep(@retry_interval)
|
104
|
+
n += 1
|
105
|
+
retry
|
106
|
+
end
|
107
|
+
raise e
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# ------------------------------------------------------------------- #
|
112
|
+
# start
|
113
|
+
# ------------------------------------------------------------------- #
|
114
|
+
def start
|
115
|
+
@http.start
|
116
|
+
if (block_given?)
|
117
|
+
yield self
|
118
|
+
@http.finish if (@http.active?)
|
119
|
+
end
|
120
|
+
return self
|
121
|
+
end
|
122
|
+
|
123
|
+
# ------------------------------------------------------------------- #
|
124
|
+
# active?
|
125
|
+
# ------------------------------------------------------------------- #
|
126
|
+
def active?
|
127
|
+
return @http.active?
|
128
|
+
end
|
129
|
+
|
130
|
+
# ------------------------------------------------------------------- #
|
131
|
+
# address
|
132
|
+
# ------------------------------------------------------------------- #
|
133
|
+
def address
|
134
|
+
return @http.address
|
135
|
+
end
|
136
|
+
|
137
|
+
# ------------------------------------------------------------------- #
|
138
|
+
# port
|
139
|
+
# ------------------------------------------------------------------- #
|
140
|
+
def port
|
141
|
+
return @http.port
|
142
|
+
end
|
143
|
+
|
144
|
+
# ------------------------------------------------------------------- #
|
145
|
+
# open_timeout
|
146
|
+
# ------------------------------------------------------------------- #
|
147
|
+
def open_timeout
|
148
|
+
return @http.open_timeout
|
149
|
+
end
|
150
|
+
|
151
|
+
# ------------------------------------------------------------------- #
|
152
|
+
# open_timeout=
|
153
|
+
# ------------------------------------------------------------------- #
|
154
|
+
def open_timeout=(n)
|
155
|
+
return @http.open_timeout = n
|
156
|
+
end
|
157
|
+
|
158
|
+
# ------------------------------------------------------------------- #
|
159
|
+
# read_timeout
|
160
|
+
# ------------------------------------------------------------------- #
|
161
|
+
def read_timeout
|
162
|
+
return @http.read_timeout
|
163
|
+
end
|
164
|
+
|
165
|
+
# ------------------------------------------------------------------- #
|
166
|
+
# read_timeout=
|
167
|
+
# ------------------------------------------------------------------- #
|
168
|
+
def read_timeout=(n)
|
169
|
+
return @http.read_timeout = n
|
170
|
+
end
|
171
|
+
|
172
|
+
# ------------------------------------------------------------------- #
|
173
|
+
# finish
|
174
|
+
# ------------------------------------------------------------------- #
|
175
|
+
def finish
|
176
|
+
@http.finish
|
177
|
+
return self
|
178
|
+
end
|
179
|
+
|
180
|
+
# ------------------------------------------------------------------- #
|
181
|
+
# proxy?
|
182
|
+
# ------------------------------------------------------------------- #
|
183
|
+
def proxy?
|
184
|
+
return @http.proxy?
|
185
|
+
end
|
186
|
+
|
187
|
+
# ------------------------------------------------------------------- #
|
188
|
+
# proxy_address
|
189
|
+
# ------------------------------------------------------------------- #
|
190
|
+
def proxy_address
|
191
|
+
return @http.proxy_address
|
192
|
+
end
|
193
|
+
|
194
|
+
# ------------------------------------------------------------------- #
|
195
|
+
# proxy_port
|
196
|
+
# ------------------------------------------------------------------- #
|
197
|
+
def proxy_port
|
198
|
+
return @http.proxy_port
|
199
|
+
end
|
200
|
+
end # HTTPWrapper
|
201
|
+
end # Crown
|