Module: DK::Posts

Included in:
Client
Defined in:
lib/draftking/posts.rb,
lib/draftking/posts/posts_helpers.rb

Overview

Helper Methods

Instance Method Summary collapse

Instance Method Details

#all_posts(last_id: 0, offset: 0) ⇒ [Post]

Collect all Posts

Parameters:

  • last_id (Int) (defaults to: 0)

    ID of post to begin reading from (for reading Drafts)

  • offset (Int) (defaults to: 0)

    Post index to start reading from (for reading Queue)

  • blog_url (String)

    URL of blog to read from

  • source (Symbol)

    Get posts from :draft or :queue

Returns:

  • ([Post])

    Array of Post Hash data



192
193
194
195
196
# File 'lib/draftking/posts.rb', line 192

def all_posts(last_id: 0, offset: 0)
  chunk = some_posts(before_id: last_id, offset: offset)
  return chunk if chunk.empty?
  chunk + all_posts(last_id: chunk.last['id'], offset: offset + chunk.size)
end

#auto_poster(options = {}) ⇒ nil

Publish posts at an interval, first in first out

Parameters:

  • options (:comment) (defaults to: {})
    String

    String to add as comment

  • options (:source) (defaults to: {})
    Symbol

    Target posts from :draft or :queue

  • options (:simulate) (defaults to: {})
    Bool

    Simulation?

  • options (:add_tags) (defaults to: {})
    String

    Tags to add

  • options (:keep_tags) (defaults to: {})
    Bool

    Preserve old tags?

  • options (:keep_tree) (defaults to: {})
    Bool

    Preserve old comments?

Returns:

  • (nil)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/draftking/posts.rb', line 219

def auto_poster(options = {})
  process_options(options)
  act_on_blog(name: @blog_name)
  pprint "Retrieving posts...(can take a while for large queues)\r"
  posts = all_posts.reverse # FIFO
  total = posts.size
  pputs "Found #{total} posts in #{@source.capitalize}#{'s' if @source[0] == 'd'}."
  pputs 'Press CTRL + C to exit.'
  interval = 432 # 200 posts / 24 hours = 432sec
  posts.each_with_index do |current, idx|
    pprint "Publishing post #{idx}/#{total}.\r"
    post = Post.new(current, keep_tree: @keep_tags)
    post.change_state(DK::PUBLISH)
    post.replace_comment_with(@comment)
    post.generate_tags(keep_tags: @keep_tags,
                       add_tags:  @tags,
                       exclude:   @comment,
                       credit:    true) if @auto_tag
    unless post.save(client: @client, simulate: @simulate) > 0
      pputs "Error at Index: #{idx}. Unable to save post!"
      pputs "reblog_key: #{post.reblog_key}, id: #{post.post_id}"
      pputs 'Quitting auto-poster.'
      exit 1
    end
    pprint "Published #{idx}/#{total} posts. Next post at #{Time.now + interval}\r"
    sleep interval unless idx == total
  end # End of auto-posting
  pputs 'Auto-Poster has completed!'
end

#calculate_result(result_q) ⇒ Object

Determine number of modified posts



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/draftking/posts.rb', line 67

def calculate_result(result_q)
  mod_count = 0
  mod_posts = []
  return [mod_count, mod_posts] if result_q.empty?
  while post = result_q.pop
    mod_count += post.saved
    mod_posts << post if post.saved > 0
    break if result_q.empty?
  end
  [mod_count, mod_posts]
end

#call_source(options) ⇒ Object

Dashboard integration



159
160
161
162
163
164
165
166
# File 'lib/draftking/posts.rb', line 159

def call_source(options)
  begin
    return @client.send(@source, options).first[1] if dashboard? || likes?
    @client.send(check_for_publish(@source), @blog_url, options)
  rescue
    retry
  end
end

#check_for_publish(source) ⇒ Object



168
169
170
171
# File 'lib/draftking/posts.rb', line 168

def check_for_publish(source)
  return source unless source.eql?(PUBLISH)
  :posts
end

#comment_posts(options = {}) ⇒ int

Add a comment to Posts

Parameters:

  • options (:credit) (defaults to: {})
    Bool

    Give dk credit?

  • options (:comment) (defaults to: {})
    String

    String to add as comment

  • options (:limit) (defaults to: {})
    Int

    Max number of modified posts

  • options (:message) (defaults to: {})
    String

    Message to display during processing

  • options (:source) (defaults to: {})
    Symbol

    Target posts from :draft or :queue

  • options (:simulate) (defaults to: {})
    Bool

    Simulation?

  • options (:mute) (defaults to: {})
    String

    Suppress progress indicator

Returns:

  • (int)

    Number of modified posts



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/draftking/posts.rb', line 88

def comment_posts(options = {})
  src = source_string(options[:source])
  options[:message] = "Adding #{src} comment \'#{comment}\': "
  post_operation(options) do |post, _|
    post.replace_comment_with(@comment)
    post.generate_tags(keep_tags: @keep_tags,
                       add_tags:  @tags,
                       exclude:   @comment,
                       credit:    @credit) if @auto_tag
  end
end

#dashboard?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/draftking/posts.rb', line 203

def dashboard?
  @source == :dashboard
end

#generate_worker(*data, block) ⇒ Object

Work queue processor



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/draftking/posts.rb', line 29

def generate_worker(*data, block)
  work, results, total = data
  begin
    while post = work.pop(true)
      po = Post.new(post, keep_tree: @keep_tree)
      block.call(po, results.size) # Do work on Post
      po.save(client: @client, simulate: @simulate)
      results.push(po)
      show_progress(current: results.size, total: total, message: message) unless @mute
    end
  rescue ThreadError # Queue empty
  end
end

#get_posts[Post]

Determine draft data to use.

Parameters:

  • options (:test_data)
    [Hash]

    Array of post hash data

  • options (:limit)
    Int

    Limit # of posts selected

  • options (:blog_url)
    String

    URL of blog to read from

  • options (:source)
    Symbol

    Get posts from :draft or :queue

  • options (:before_id)
    Int
    :draft

    ID of post to begin reading from

  • options (:offset)
    Int
    :queue

    Post index to start reading from

Returns:

  • ([Post])

    Array of Post Hash data



128
129
130
131
132
133
134
135
# File 'lib/draftking/posts.rb', line 128

def get_posts
  pprint "Getting posts...\r"
  return some_test_data if @test_data
  return some_posts(offset: @offset) if dashboard?
  return all_posts.uniq if @greedy || @limit.nil?
  return some_posts(offset: @offset, before_id: @before_id) if @limit <= 50
  limited_posts
end

#index_within_limit?(index, limit) ⇒ Boolean

index < limit

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/draftking/posts/posts_helpers.rb', line 60

def index_within_limit?(index, limit)
  return true if limit.nil? || limit.zero?
  index < limit
end

#likes?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/draftking/posts.rb', line 207

def likes?
  @source == :likes
end

#limited_postsObject

Get @limit # of Posts



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/draftking/posts.rb', line 174

def limited_posts
  result = []
  until result.size >= @limit
    chunk = some_posts(offset: @offset, before_id: @before_id)
    break if chunk.empty?
    result += chunk
    @offset    = chunk.size
    @before_id = chunk.last['id']
  end
  result.take(@limit)
end

#post_operation(options, &block) ⇒ int

Common code for Post operations

Parameters:

  • options (:limit)
    Int

    Maximum number of posts to process

  • options (:message)
    String

    Message to display during processing

  • options (:shuffle)
    Bool

    Randomize order of posts

  • options (:blog_name)
    String

    Name of blog to target

  • options (:mute)
    String

    Suppress progress indicator

  • options (:test_data)
    [Hash]

    Array of post hash data

  • options (:simulate)
    Bool

    Simulation?

Returns:

  • (int)

    Number of modified posts



17
18
19
20
21
22
23
24
25
26
# File 'lib/draftking/posts.rb', line 17

def post_operation(options, &block)
  work, total, results, reporter = setup_operation(options)
  workers = (0...DK::MAX_THREADS).map { Thread.new { generate_worker(work, results, total, block) } }
  workers.map(&:join)
  mod_count, mod_posts = calculate_result(results)
  show_progress(message: message, done: true, modified: mod_count) unless @mute
  reporter.new(objects: mod_posts, title: REPORT_TITLE, fields: REPORT_FIELDS, simulate: @simulate).show unless @mute
  act_on_blog(name: @blog_name) # Refresh account info
  [mod_count, mod_posts]
end

#posts_to_queue(posts) ⇒ Object

Create queue of Posts for worker threads



60
61
62
63
64
# File 'lib/draftking/posts.rb', line 60

def posts_to_queue(posts)
  work_q = Queue.new
  posts.each { |p| work_q.push(p) }
  work_q
end

#pprint(str) ⇒ Object



8
9
10
# File 'lib/draftking/posts/posts_helpers.rb', line 8

def pprint(str)
  puts str unless @mute || !@show_pi
end

#pputs(str) ⇒ Object



4
5
6
# File 'lib/draftking/posts/posts_helpers.rb', line 4

def pputs(str)
  puts str unless @mute || !@show_pi
end

#setup_done(modified) ⇒ Object

Values for displaying completed process



27
28
29
30
31
32
# File 'lib/draftking/posts/posts_helpers.rb', line 27

def setup_done(modified)
  indicator  = ''
  newline    = "\n"
  progress   = "(#{modified} modified)"
  [indicator, newline, progress]
end

#setup_operation(options) ⇒ Object

Common initialization for post operations



44
45
46
47
48
49
50
51
52
53
# File 'lib/draftking/posts.rb', line 44

def setup_operation(options)
  pprint "Setup\r"
  process_options(options)
  act_on_blog(name: @blog_name)
  posts = @shuffle ? shufflex(get_posts.reverse, 3) : get_posts.reverse
  posts = posts.take(@limit) if @limit
  work = posts_to_queue(posts)
  reporter = options[:reporter] || DK::Reporter
  [work, work.size, Queue.new, reporter]
end

#setup_undone(current, total) ⇒ Object

Values for displaying in-progress process



35
36
37
38
39
40
41
42
# File 'lib/draftking/posts/posts_helpers.rb', line 35

def setup_undone(current, total)
  tildes = current.to_i % 4
  indicator  = "~#{'~' * tildes}#{' ' * (3 - tildes)}> "
  newline    = nil
  percentage = total > 0 ? ((current.to_f / total.to_f) * 100).round : 0
  progress   = "#{current} / #{total} [#{percentage}\%] "
  [indicator, newline, progress]
end

#show_progress(current: 0, total: 0, message: '', done: false, modified: 0) ⇒ Object

Display progress percentage

Parameters:

  • current (Int) (defaults to: 0)

    Progress Counter

  • total (Int) (defaults to: 0)

    # Items to be processed

  • message (String) (defaults to: '')

    Display message for process

  • done (Bool) (defaults to: false)

    Processing Complete?

  • modified (Int) (defaults to: 0)

    # of items modified



18
19
20
21
22
23
24
# File 'lib/draftking/posts/posts_helpers.rb', line 18

def show_progress(current: 0, total: 0, message: '', done: false, modified: 0)
  return unless @show_pi
  indicator, newline, progress = setup_done(modified) if done
  indicator, newline, progress = setup_undone(current, total) unless done
  pprint "#{indicator}#{message}#{progress}#{' ' * 30}\r#{newline}"
  $stdout.flush unless done
end

#shufflex(arr, num) ⇒ Object



55
56
57
# File 'lib/draftking/posts.rb', line 55

def shufflex(arr, num)
  (0..num).to_a.inject(arr) { |m, _| m = m.shuffle; m }
end

#some_posts(before_id: 0, offset: 0, max_id: nil, since_id: nil) ⇒ [Post]

Get up to 50 Posts

Parameters:

  • before_id (Int) (defaults to: 0)
    :draft

    ID of post to begin reading from

  • offset (Int) (defaults to: 0)
    :queue

    Post index to start reading from

Returns:

  • ([Post])

    Array of Post Hash data



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/draftking/posts.rb', line 141

def some_posts(before_id: 0, offset: 0, max_id: nil, since_id: nil)
  options = { limit: [(@limit || 50), 50].min }
  options[:max_id]   = max_id   if max_id
  options[:since_id] = since_id if since_id
  options[@source == :draft ? :before_id : :offset] =
    (@source == :draft ? before_id : offset) unless dashboard?
  options[:type] = @type if @type

  begin
    retries ||= 0
    result = call_source(options)['posts']
    result.is_a?(Integer) ? (raise TypeError) : result
  rescue TypeError
    (retries += 1) > MAX_RETRY ? [] : retry
  end
end

#some_test_dataObject

Handle limits for test data



199
200
201
# File 'lib/draftking/posts.rb', line 199

def some_test_data
  @limit ? @test_data.take(@limit) : @test_data
end

#source_string(symbol) ⇒ Object

Convert source symbol to string

Parameters:

  • symbol (Symbol)

    Source Symbol



54
55
56
57
# File 'lib/draftking/posts/posts_helpers.rb', line 54

def source_string(symbol)
  return 'draft' unless symbol
  symbol.to_s
end

#tag_posts(options) ⇒ int

Returns Number of modified posts.

Parameters:

  • options (:credit)
    Bool

    Give dk credit?

  • options (:source)
    Symbol

    Target posts from :draft or :queue

  • options (:mute)
    String

    Suppress progress indicator

  • options (:blog_name)
    String

    Name of blog to target

  • options (:keep_tags)
    Bool

    Preserve existing post tags

  • options (:keep_tree)
    Bool

    Preserve existing post comments

  • options (:simulate)
    Bool

    Simulation?

  • options (:comment)
    String

    Exclude :comment from tags

Returns:

  • (int)

    Number of modified posts



109
110
111
112
113
114
115
116
117
118
# File 'lib/draftking/posts.rb', line 109

def tag_posts(options)
  src = source_string(options[:source])
  options[:message] = "Tagging #{src} with #{options[:add_tags]}: "
  post_operation(options) do |post, _|
    post.generate_tags(keep_tags: @keep_tags,
                       add_tags:  @tags,
                       exclude:   @comment,
                       credit:    @credit) if @auto_tag
  end
end

#tumblr_url(blog_name) ⇒ Object

Construct Tumblr URL string

Parameters:

  • blog_name (String)

    Blog Name



46
47
48
49
50
# File 'lib/draftking/posts/posts_helpers.rb', line 46

def tumblr_url(blog_name)
  return '' unless blog_name
  blog_name += '.tumblr.com' unless blog_name.include?('.')
  blog_name
end