Class: Tumblr4r::Site

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

Overview

ConnectionオブジェクトとParserオブジェクトを組み合わせて、TumblrAPIとRubyオブジェクトの相互変換を行うTODO: private な post だけを取得する API が無いのだなぁ

  • Webから更新したものがAPIで取得できるデータに反映されるには少しタイムラグがあるようだ

  • Webから更新しちゃうと、POST日時の秒が丸められてしまう

Constant Summary collapse

API_READ_MAX_ALLOWED_COUNT =
50
SLEEP_SECONDS_FOR_EVERY_FETCH =

API manual says “Requests are rate-limited to one every 10 seconds.”

10.0
@@default_log_level =
Logger::DEBUG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, email = nil, password = nil, http = nil, logger = nil) ⇒ Site

Returns a new instance of Site.



64
65
66
67
68
69
70
71
72
73
# File 'lib/tumblr4r.rb', line 64

def initialize(hostname, email=nil, password=nil, http = nil, logger = nil)
  @hostname = hostname
  @email = email
  @password = password
  @logger = logger || Logger.new(STDERR)
  @logger.level = @@default_log_level
  @conn = XMLConnection.new(http || @hostname, email, password, @logger)
  @parser = XMLParser.new
  self.site_info
end

Instance Attribute Details

#cnameObject

Returns the value of attribute cname.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def cname
  @cname
end

#descriptionObject

Returns the value of attribute description.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def description
  @description
end

#emailObject

Returns the value of attribute email.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def email
  @email
end

#feedsObject

Returns the value of attribute feeds.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def feeds
  @feeds
end

#hostnameObject

Returns the value of attribute hostname.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger.



43
44
45
# File 'lib/tumblr4r.rb', line 43

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def name
  @name
end

#passwordObject

Returns the value of attribute password.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def password
  @password
end

#timezoneObject

Returns the value of attribute timezone.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



41
42
43
# File 'lib/tumblr4r.rb', line 41

def title
  @title
end

Class Method Details

.find(hostname, email = nil, password = nil, http = nil, &block) ⇒ Object

TODO: unit test



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tumblr4r.rb', line 51

def find(hostname, email=nil, password=nil, http=nil, &block)
  site = self.new(hostname, email, password, http)
  result = site.find(:all)
  if block_given?
    result.each do |post|
      yield post
    end
  else
    return result
  end
end

Instance Method Details

#count(options = { }) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tumblr4r.rb', line 199

def count(options = { })
  params = { }
  [:id, :type, :filter, :tagged, :search].each do |option|
    params[option] = options[option] if options[option]
  end
  params[:num] = 1
  params[:start] = 0
  xml = @conn.get(params)
  posts, start, total = @parser.posts(xml)
  return total
end

#dashboard(options = { }) ⇒ Array<Post>

, :search,

Parameters:

  • options (Hash) (defaults to: { })

    :offset, :limit, :type, :filter

Returns:



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
164
165
166
167
# File 'lib/tumblr4r.rb', line 134

def dashboard(options = { })
  limit = options[:limit] ? options[:limit].to_i : nil
  offset = options[:offset].to_i
  result = []
  params = {:likes => "1" }
  [:type, :filter].each do |option|
    params[option] = options[option] if options[option]
  end

  total = 1000 # 明記されてないがたぶん1000件ぐらいが上限?
  last_fetched_at = nil
  each_fetch(limit, offset, API_READ_MAX_ALLOWED_COUNT, total) do |offset, num|
    params[:start] = offset
    params[:num] = num
    sleep_secs = last_fetched_at ? SLEEP_SECONDS_FOR_EVERY_FETCH - (Time.now - last_fetched_at) : 0
    if sleep_secs > 0
      logger.debug("sleeping #{sleep_secs} secs.")
      sleep sleep_secs
    end
    xml = @conn.dashboard(params)
    last_fetched_at = Time.now
    posts, start, total = @parser.posts(xml)
    result += posts
    if posts.size == 0
      # Tumblr API の total で得られる値は全く信用ならない。
      # 検索条件を考慮した件数を返してくれない。
      # (つまり、goalは信用ならない)ので、posts.sizeも終了判定に利用する。
      # TODO: もしくは:numの値を足し合わせていって、それとgoalを比較する?
      break
    end
    posts.size
  end
  result
end

#delete(post_id_or_post) ⇒ Object

Parameters:

  • post_id_or_post (Integer|Post)


223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/tumblr4r.rb', line 223

def delete(post_id_or_post)
  post_id = nil
  case post_id_or_post
  when Tumblr4r::Post
    post_id = post_id_or_post.post_id
  when Integer
    post_id = post_id_or_post
  else
    raise ArgumentError.new("post_id_or_post must be Tumblr4r::Post or Integer, but was #{post_id_or_post}(<#{post_id_or_post.class}>)")
  end
  return @conn.delete(post_id)
end

#each_fetch(limit, offset, max_at_once, total, &block) ⇒ Object



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
# File 'lib/tumblr4r.rb', line 169

def each_fetch(limit, offset, max_at_once, total, &block)
  return if offset && offset.to_i < 0

  # 取得開始位置の初期化
  start = offset || 0
  if limit
    goal = [total - start, limit].min
  else
    goal = total - start
  end
  # 取得件数の初期化
  num = [goal, max_at_once].min
  if num < 0
    return
  end

  all_fetched = 0
  while all_fetched < goal
    fetched_count = yield(start, num)
    @logger.info("size: #{fetched_count}")
    @logger.info("start: #{start}")
    @logger.info("total: #{total}")
    all_fetched += fetched_count
    # 取得開始位置の調整
    start += num
    # 取得件数の調整
    num = [goal - fetched_count, max_at_once].min
  end
end

#find(id_or_type, options = { }) ⇒ Array<Post>|Post

Parameters:

  • id_or_type (Symbol|Integer)

    :all, id

  • options (Hash) (defaults to: { })

    :offset, :limit, :type, :filter, :tagged, :search,

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tumblr4r.rb', line 78

def find(id_or_type, options = { })
  if id_or_type == :all
    normal_find(options)
  elsif id_or_type.kind_of?(Integer)
    xml = @conn.get({:id => id_or_type})
    posts, start, total = @parser.posts(xml)
    @logger.info("size: #{posts.size}")
    @logger.info("start: #{start}")
    @logger.info("total: #{total}")
    return posts[0]
  else
    raise ArgumentError.new("id_or_type must be :all or Integer, but was #{id_or_type}(<#{id_or_type.class}>)")
  end
end

#normal_find(options) ⇒ Object

TODO: ループごとに実行して欲しい処理をblockで渡せるようにするといいかも?そのブロック引数にエラー情報も渡してあげれば、エラーが起きたのならretryだな、みたいな指示ができない、、、、かな



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tumblr4r.rb', line 96

def normal_find(options)
  limit = options[:limit] && options[:limit].to_i
  offset = options[:offset].to_i
  total = self.count(options)
  result = []
  params = { }
  [:type, :filter, :tagged, :search].each do |option|
    params[option] = options[option] if options[option]
  end
  last_fetched_at = nil
  each_fetch(limit, offset, API_READ_MAX_ALLOWED_COUNT, total) do |offset, num|
    params[:start] = offset
    params[:num] = num
    # APIマニュアルにはこっちのスリープ時間については明記されてないが、dashboardと同じ秒数SLEEPしとく
    sleep_secs = last_fetched_at ? SLEEP_SECONDS_FOR_EVERY_FETCH - (Time.now - last_fetched_at) : 0
    if sleep_secs > 0
      logger.debug("sleeping #{sleep_secs} secs.")
      sleep sleep_secs
    end
    xml = @conn.get(params)
    last_fetched_at = Time.now
    posts, start, total = @parser.posts(xml)
    result += posts
    if posts.size == 0
      # Tumblr API の total で得られる値は全く信用ならない。
      # 検索条件を考慮した件数を返してくれない。
      # (つまり、goalは信用ならない)ので、posts.sizeも終了判定に利用する。
      # TODO: もしくは:numの値を足し合わせていって、それとgoalを比較する?
      break
    end
    posts.size
  end
  result
end

#save(post) ⇒ Object



216
217
218
219
220
# File 'lib/tumblr4r.rb', line 216

def save(post)
  post_id = @conn.write(post.params)
  new_post = self.find(post_id)
  return new_post
end

#site_infoObject



211
212
213
214
# File 'lib/tumblr4r.rb', line 211

def site_info
  xml = @conn.get(:num => 1)
  @parser.siteinfo(self, xml)
end