Module: Login

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

Overview

Contains login and logout methods for the bot.

Instance Method Summary collapse

Instance Method Details

#loginObject

Login method to log the user in. Prints success message on successful login,

error message otherwise.

Examples:

Login example

bot. # => 2018-09-19 17:39:45	Trying to login ...
          # => 2018-09-19 17:39:47	Successfully logged in as andreyuhai


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/botinsta/login.rb', line 10

def 
  @agent = Mechanize.new

  # Navigate to classic login page
   = @agent.get 'https://www.instagram.com/accounts/login/?force_classic_login'

  # Get the login form
   = .forms.first

  # Fill in the login form
  ['username'] = @username
  ['password'] = @password

  # Submit the form and if couldn't login raise an exception.
  print_try_message(action: :login)
  response = .submit
  if response.code != 200 && response.body.include?('not-logged-in')
     = false
  else
    (result: :success, username: @username)
     = true
  end
  raise StandardError unless 
rescue StandardError
  (result: :error, username: @username)
  # TODO: logger to log these kind of stuff
  exit
end

#logoutObject

Prints action sum and then logs the user out.

Examples:

Logout example

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


43
44
45
46
47
# File 'lib/botinsta/login.rb', line 43

def logout
  print_action_sum
  print_try_message(action: :logout)
  @agent.get 'https://instagram.com/accounts/logout/'
end