module Helpers

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

Public Instance Methods

delete_from_db(user_id) click to toggle source

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

@todo This method needs to be replaced with a better and DRY one. @param user_id [String, Integer] id of the user to be unfollowed.

# File lib/botinsta/helpers.rb, line 144
def delete_from_db(user_id)
  @table_follows.where(user_id: user_id).delete
end
handle_database_creation() click to toggle source

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

# 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
print_action_sum() click to toggle source

Prints action sum. Used before logging out.

@example

bot.print_action_sum # => 2018-09-19 17:41:11     Liked: 0 Followed: 0 Unfollowed: 0
print_error_message(**params) click to toggle source

Prints error message for a specified action.

@option params [Symbol] :action The action performed. @option params [String] :data The id that the action is performed on.

print_login_message(**params) click to toggle source

Prints login status message for the account

@option params [Symbol] :result Login status @option params [String] :username

print_success_message(**params) click to toggle source

Prints success message for a specified action.

@option params [Symbol] :action The action performed. @option params [String] :data The id that the action is performed on. @option params [Integer] :number The number of times that the action has been performed.

print_tag_message(tag) click to toggle source

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

@param tag [String] current tag.

print_time_stamp() click to toggle source

Prints out the current time @example

print_time_stamp # => "2018-09-19 12:14:43"
print_try_message(**params) click to toggle source

Prints messages when trying to take some action.

@option params [Symbol] :action The action that the bot is trying to take. @option params [String, Integer] :data The data which will be

affected by the specified action.
sleep_rand(min, max) click to toggle source
# 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) click to toggle source

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

@param last_time [Time] a Time instance, used with @last_follow_time. @return [Boolean] true if a day is past since

the first follow entry in the database, false otherwise.
# 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