Module: Actions

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

Overview

Various actions you can do on Instagram and other related methods.

Instance Method Summary collapse

Instance Method Details

#follow_if_not_in_db(user) ⇒ true, false

Follows the user if it doesn’t exist in the database.

Parameters:

  • user (UserData)

    a UserData instance.

Returns:

  • (true, false)

    returns true on success, false otherwise.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/botinsta/actions.rb', line 95

def follow_if_not_in_db(user)
  return false if user.exists_in_db?(@table_follows)

  if follow_user(user.id)
    @total_follows += 1
    print_success_message(action: :follow, number: @total_follows, data: @user.username)
    user.insert_into_db(@table_follows)
    sleep_rand(28, 36)
    true
  else
    false
  end
end

#follow_user(user_id) ⇒ true, false

Follows user given by user_id.

Parameters:

  • user_id (String)

Returns:

  • (true, false)

    returns true on success, false otherwise.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/botinsta/actions.rb', line 43

def follow_user(user_id)
  url_follow = "https://www.instagram.com/web/friendships/#{user_id}/follow/"
  print_try_message(action: :follow, data: user_id)
  begin
    set_request_params
    response = @agent.post url_follow, @params, @request_headers
  rescue Mechanize::ResponseCodeError
    return false
  end
  response_data = JSON.parse(response.body)
  response.code == '200' && response_data['result'] == 'following' ? true : false
end

#like_if_not_in_db(media) ⇒ true, false

Likes the media if it doesn’t exist in the database.

Parameters:

  • media (MediaData)

    a MediaData instance.

Returns:

  • (true, false)

    returns true on success, false otherwise.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/botinsta/actions.rb', line 77

def like_if_not_in_db(media)
  return false if media.exists_in_db?(@table_likes)

  if like_media(media.id)
    @total_likes += 1
    print_success_message(action: :like, number: @total_likes, data: @media.id)
    media.insert_into_db(@table_likes)
    sleep_rand(28, 36)
    true
  else
    false
  end
end

#like_media(media_id) ⇒ true, false

Likes media given by media id.

Parameters:

  • media_id (String)

Returns:

  • (true, false)

    returns true on success, false otherwise.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/botinsta/actions.rb', line 9

def like_media(media_id)
  url_like = "https://www.instagram.com/web/likes/#{media_id}/like/"
  print_try_message(action: :like, data: media_id)
  begin
    set_request_params
    response = @agent.post url_like, @params, @request_headers
  rescue Mechanize::ResponseCodeError
    return false
  end
  response_data = JSON.parse(response.body)
  response.code == '200' && response_data['status'] == 'ok' ? true : false
end

#unfollow_user(user_id) ⇒ true, false

Unfollows user given by user_id.

Parameters:

  • user_id (String)

Returns:

  • (true, false)

    returns true on success, false otherwise.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/botinsta/actions.rb', line 60

def unfollow_user(user_id)
  url_unfollow = "https://www.instagram.com/web/friendships/#{user_id}/unfollow/"
  print_try_message(action: :unfollow, data: user_id)
  begin
    set_request_params
    response = @agent.post url_unfollow, @params, @request_headers
  rescue Mechanize::ResponseCodeError
    return false
  end
  response_data = JSON.parse(response.body)
  response.code == '200' && response_data['status'] == 'ok' ? true : false
end

#unlike_media(media_id) ⇒ true, false

Unlikes media given by media id.

Parameters:

  • media_id (String)

Returns:

  • (true, false)

    returns true on success, false otherwise.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/botinsta/actions.rb', line 26

def unlike_media(media_id)
  url_unlike = "https://www.instagram.com/web/likes/#{media_id}/unlike/"
  print_try_message(action: :unlike, data: media_id)
  begin
    set_request_params
    response = @agent.post url_unlike, @params, @request_headers
  rescue Mechanize::ResponseCodeError
    return false
  end
  response_data = JSON.parse(response.body)
  response.code == '200' && response_data['status'] == 'ok' ? true : false
end