Module: Helpers

Included in:
ClassMethods
Defined in:
lib/botinsta/helpers.rb

Overview

Some helper methods to use in main methods for the bot.

Instance Method Summary collapse

Instance Method Details

#delete_from_db(user_id) ⇒ Object

TODO:

This method needs to be replaced with a better and DRY one.

The method that is used to delete a user from the database after unfollowing the user.

Parameters:

  • user_id (String, Integer)

    id of the user to be unfollowed.



144
145
146
# File 'lib/botinsta/helpers.rb', line 144

def delete_from_db(user_id)
  @table_follows.where(user_id: user_id).delete
end

#handle_database_creationObject

Handles the creation and connection of a database and its tables. Sets @table_follows and @table_likes to the related tables for further use.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/botinsta/helpers.rb', line 110

def handle_database_creation
  database = Sequel.sqlite('./actions_db.db') # memory database, requires sqlite3
  database.create_table? :"#{@username}_follows" do
    primary_key :id
    String  :user_id
    String  :username
    Time    :follow_time
  end
  database.create_table? :"#{@username}_likes" do
    primary_key :id
    String :media_id
    String :user_id
    String :shortcode
    Time   :like_time
  end
  @table_follows  = database[:"#{@username}_follows"]
  @table_likes    = database[:"#{@username}_likes"]
end

Prints action sum. Used before logging out.

Examples:

bot.print_action_sum # => 2018-09-19 17:41:11	Liked: 0 Followed: 0 Unfollowed: 0


84
85
86
87
88
89
90
# File 'lib/botinsta/helpers.rb', line 84

def print_action_sum
  string =  'Liked: ' + @total_likes.to_s.colorize(:red) +
            ' Followed: ' + @total_follows.to_s.colorize(:red) +
            ' Unfollowed: ' + @total_unfollows.to_s.colorize(:red)
  print_time_stamp
  puts string
end

Prints error message for a specified action.

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (**params):

  • :action (Symbol)

    The action performed.

  • :data (String)

    The id that the action is performed on.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/botinsta/helpers.rb', line 34

def print_error_message(**params)
  action = case params[:action]
           when :like     then 'like media'
           when :unlike   then 'unlike media'
           when :follow   then 'follow user'
           when :unfollow then 'unfollow user'
           when :comment  then 'comment on media'
           end
  error_string = "There was an error trying to #{action} ".colorize(:red) + params[:data].to_s
  print_time_stamp
  puts error_string

end

Prints login status message for the account

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (**params):

  • :result (Symbol)

    Login status

  • :username (String)


52
53
54
55
56
57
58
59
# File 'lib/botinsta/helpers.rb', line 52

def (**params)
  result = case params[:result]
           when :success  then 'Successfully logged in as '
           when :error    then 'There was an error trying to login as '
           end
  print_time_stamp
  puts result.colorize(:red) + params[:username]
end

Prints success message for a specified action.

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (**params):

  • :action (Symbol)

    The action performed.

  • :data (String)

    The id that the action is performed on.

  • :number (Integer)

    The number of times that the action has been performed.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/botinsta/helpers.rb', line 17

def print_success_message(**params)
  action = case params[:action]
           when :like     then 'liked media'
           when :unlike   then 'unliked media'
           when :follow   then 'followed user'
           when :unfollow then 'unfollowed user'
           when :comment  then 'commented on media'
           end
  success_string = "Successfully #{action} ##{params[:number]} ".colorize(:green) + params[:data].to_s
  print_time_stamp
  puts success_string
end

Prints out when the current is set to some specified tag.

Parameters:

  • tag (String)

    current tag.



95
96
97
98
# File 'lib/botinsta/helpers.rb', line 95

def print_tag_message(tag)
  print_time_stamp
  puts 'Current tag is set to '.colorize(:blue) + '#' + tag
end

Prints out the current time

Examples:

print_time_stamp # => "2018-09-19 12:14:43"


8
9
10
# File 'lib/botinsta/helpers.rb', line 8

def print_time_stamp
  print "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}\t"
end

Prints messages when trying to take some action.

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (**params):

  • :action (Symbol)

    The action that the bot is trying to take.

  • :data (String, Integer)

    The data which will be affected by the specified action.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/botinsta/helpers.rb', line 66

def print_try_message(**params)
  action = case params[:action]
           when :login    then 'Trying to login ...'
           when :logout   then 'Trying to logout ...'
           when :like     then 'Trying to like media '
           when :unlike   then 'Trying to unlike media '
           when :follow   then 'Trying to follow user '
           when :unfollow then 'trying to unfollow user '
           end
  print_time_stamp
  puts action.colorize(:light_red) + "#{params[:data].to_s unless params[:data].nil?}"
  sleep(1)
end

Reassigns the database related variables to the first entry in the database. Used after a deletion from the database.



132
133
134
135
136
137
# File 'lib/botinsta/helpers.rb', line 132

def refresh_db_related
  return if @table_follows.empty?

  @first_db_entry = @table_follows.first
  @last_follow_time = @first_db_entry[:follow_time]
end

#sleep_rand(min, max) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/botinsta/helpers.rb', line 100

def sleep_rand(min, max)
  sleep_time = rand(min..max)
  sleep(1)
  print_time_stamp
  puts "Sleeping for #{sleep_time - 1} seconds ...".colorize(:red)
  sleep(sleep_time - 1)
end

#unfollow_threshold_past?(last_time) ⇒ Boolean

Calculates whether the given threshold is past or not to start unfollowing users from the database.

Parameters:

  • last_time (Time)

    a Time instance, used with @last_follow_time.

Returns:

  • (Boolean)

    true if a day is past since the first follow entry in the database, false otherwise.



154
155
156
157
158
159
160
161
162
# File 'lib/botinsta/helpers.rb', line 154

def unfollow_threshold_past?(last_time)
  days    = @unfollow_threshold[:days]    ||= 0
  hours   = @unfollow_threshold[:hours]   ||= 0
  minutes = @unfollow_threshold[:minutes] ||= 0
  seconds = @unfollow_threshold[:seconds] ||= 0

  total_in_seconds = days * 86_400 + hours * 3600 + minutes * 60 + seconds
  ((Time.now - last_time) / total_in_seconds) >= 1
end