Method: IgApi::Account#search_for_user

Defined in:
lib/ig_api/account.rb

#search_for_user(user, username) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ig_api/account.rb', line 93

def search_for_user(user, username)
  rank_token = IgApi::Http.generate_rank_token user.session.scan(/ds_user_id=([\d]+);/)[0][0]
  endpoint = 'https://i.instagram.com/api/v1/users/search/'
  param = format('?is_typehead=true&q=%s&rank_token=%s', username, rank_token)
  result = api.get(endpoint + param)
               .with(session: user.session, ua: user.useragent).exec

  json_result = JSON.parse result.body
  if json_result['num_results'] > 0
    user_result = json_result['users'][0]
    user_object = IgApi::User.new username: username
    user_object.data = {
      id: user_result['pk'],
      full_name: user_result['full_name'],
      is_private: user_result['is_prive'],
      profile_pic_url: user_result['profile_pic_url'],
      profile_pic_id: user_result['profile_pic_id'],
      is_verified: user_result['is_verified'],
      is_business: user_result['is_business']
    }
    user_object.session = user.session
    user_object
  end
end