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
-
#delete_from_db(user_id) ⇒ Object
The method that is used to delete a user from the database after unfollowing the user.
-
#handle_database_creation ⇒ Object
Handles the creation and connection of a database and its tables.
-
#print_action_sum ⇒ Object
Prints action sum.
-
#print_error_message(**params) ⇒ Object
Prints error message for a specified action.
-
#print_login_message(**params) ⇒ Object
Prints login status message for the account.
-
#print_success_message(**params) ⇒ Object
Prints success message for a specified action.
-
#print_tag_message(tag) ⇒ Object
Prints out when the current is set to some specified tag.
-
#print_time_stamp ⇒ Object
Prints out the current time.
-
#print_try_message(**params) ⇒ Object
Prints messages when trying to take some action.
-
#refresh_db_related ⇒ Object
Reassigns the database related variables to the first entry in the database.
- #sleep_rand(min, max) ⇒ Object
-
#unfollow_threshold_past?(last_time) ⇒ Boolean
Calculates whether the given threshold is past or not to start unfollowing users from the database.
Instance Method Details
#delete_from_db(user_id) ⇒ Object
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.
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_creation ⇒ Object
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 |
#print_action_sum ⇒ Object
Prints action sum. Used before logging out.
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 |
#print_error_message(**params) ⇒ Object
Prints error message for a specified action.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/botinsta/helpers.rb', line 34 def (**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 |
#print_login_message(**params) ⇒ Object
Prints login status message for the account
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 |
#print_success_message(**params) ⇒ Object
Prints success message for a specified action.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/botinsta/helpers.rb', line 17 def (**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 |
#print_tag_message(tag) ⇒ Object
Prints out when the current is set to some specified tag.
95 96 97 98 |
# File 'lib/botinsta/helpers.rb', line 95 def (tag) print_time_stamp puts 'Current tag is set to '.colorize(:blue) + '#' + tag end |
#print_time_stamp ⇒ Object
Prints out the current time
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 |
#print_try_message(**params) ⇒ Object
Prints messages when trying to take some action.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/botinsta/helpers.rb', line 66 def (**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 |
#refresh_db_related ⇒ Object
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 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.
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 |