Class: Instagram::NativeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/instagram/native_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, dg: '4da9753f-4663-4c89-a99a-7c1bc6d188fe', ua: 'Instagram 6.14.1 Android (16/4.1.2; 240dpi; 540x960; Sony/SEMC; LT22i; LT22i; st-ericsson; ru_RU)', device_id: nil) ⇒ NativeClient

Returns a new instance of NativeClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/instagram/native_client.rb', line 7

def initialize(proxy, dg: '4da9753f-4663-4c89-a99a-7c1bc6d188fe', ua: 'Instagram 6.14.1 Android (16/4.1.2; 240dpi; 540x960; Sony/SEMC; LT22i; LT22i; st-ericsson; ru_RU)', device_id: nil)
  @device_guid = generate_device_guid #dg
  @device_id = device_id || generate_device_id
  @useragent = ua
  m = proxy.match(/(\w+):(\w+)@([\w\.]+):(\d+)/) if proxy
  @proxy = proxy
  if m
    host_port = "http://#{m[3]}:#{m[4]}"
    @http = HTTPClient.new(host_port, @useragent)
    @http.set_proxy_auth(m[1], m[2])
  else
    if proxy
      puts "proxy is: #{proxy}"
      host_port = "http://#{proxy}"
      @http = HTTPClient.new(host_port, @useragent)
    else
      @http = HTTPClient.new(nil, @useragent)
    end
  end
  @http.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv3
end

Instance Attribute Details

#csrftokenObject

Returns the value of attribute csrftoken.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def csrftoken
  @csrftoken
end

#device_guidObject

Returns the value of attribute device_guid.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def device_guid
  @device_guid
end

#device_idObject

Returns the value of attribute device_id.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def device_id
  @device_id
end

#httpObject

Returns the value of attribute http.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def http
  @http
end

#ig_user_idObject

Returns the value of attribute ig_user_id.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def ig_user_id
  @ig_user_id
end

#ig_user_nameObject

Returns the value of attribute ig_user_name.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def ig_user_name
  @ig_user_name
end

#private_keyObject

Returns the value of attribute private_key.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def private_key
  @private_key
end

#proxyObject

Returns the value of attribute proxy.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def proxy
  @proxy
end

#useragentObject

Returns the value of attribute useragent.



5
6
7
# File 'lib/instagram/native_client.rb', line 5

def useragent
  @useragent
end

Instance Method Details

#change_profile_picture(picture_path = 'http://lorempixel.com/150/150/') ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/instagram/native_client.rb', line 163

def change_profile_picture(picture_path = 'http://lorempixel.com/150/150/')
  filename = get_temp_filename
  begin
    file = File.new(filename, 'wb')
    res = open(picture_path) { |f| f.read }
    file.write(res.to_s)
    file.close
    file = File.open(filename)
    picture_params = {
      :_csrftoken => @csrftoken,
      :profile_pic => file
    }
    @http.cookies.each {|c| c.domain = 'i.instagram.com'}
    res = @http.post("http://i.instagram.com/api/v1/accounts/change_profile_picture/", picture_params)
  ensure
    file.close
    File.unlink filename
  end
end

#check_username(uname) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/instagram/native_client.rb', line 209

def check_username(uname)
   = {
                 :_csrftoken => @csrftoken,
                 :username => uname
               }
  parsed = post_struct "https://instagram.com/api/v1/users/check_username/", 
  parsed['status'] == 'ok' && parsed['available']
end

#create_account(uname, pass, email = nil, first_name = "") ⇒ Object

Raises:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/instagram/native_client.rb', line 218

def (uname, pass, email = nil, first_name = "")
  email ||= generate_email(uname)
   = {
                   :_csrftoken => @csrftoken,
                   :first_name => first_name,
                   :username => uname,
                   :email => email,
                   :guid => @device_guid,
                   :password => pass,
                   :device_id => @device_id
                 }
  parsed = post_struct "https://instagram.com/api/v1/accounts/create/", 
  raise RegisterException.new(uname, pass, email, @proxy) unless parsed['status'] == 'ok'
end

#direct_inboxObject



243
244
245
246
# File 'lib/instagram/native_client.rb', line 243

def direct_inbox
  @http.cookies.each {|c| c.domain = 'i.instagram.com'}
  res = @http.get("https://i.instagram.com/api/v1/direct_share/inbox/") #"Content-Type" => "application/json"
end

#follow_user(user_id) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/instagram/native_client.rb', line 248

def follow_user(user_id)
  post_struct = {
                 :_uid => @ig_user_id,
                 :_csrftoken => @csrftoken,
                 :user_id => user_id
                }
  parsed = post_struct "http://i.instagram.com/api/v1/friendships/create/#{user_id}/", post_struct
  parsed['status'] == 'ok'
end

#get_newsObject



238
239
240
241
# File 'lib/instagram/native_client.rb', line 238

def get_news
  @http.cookies.each {|c| c.domain = 'i.instagram.com'}
  res = @http.get("http://i.instagram.com/api/v1/news/inbox/") #, "Content-Type" => "application/json"
end

#get_timelineObject



233
234
235
236
# File 'lib/instagram/native_client.rb', line 233

def get_timeline
  @http.cookies.each {|c| c.domain = 'i.instagram.com'}
  res = @http.get("http://i.instagram.com/api/v1/feed/timeline/") #, "Content-Type" => "application/json"
end

#like_media(media_id) ⇒ Object



268
269
270
271
272
273
274
275
276
277
# File 'lib/instagram/native_client.rb', line 268

def like_media(media_id)
  post_struct = {
                 :_uid => @ig_user_id,
                 :_csrftoken => @csrftoken,
                 :media_id => media_id
                }

  parsed = post_struct "http://i.instagram.com/api/v1/media/#{media_id}/like/", post_struct, {src: "timeline", d: 0}, "application/x-www-form-urlencoded; charset=UTF-8"
  parsed['status'] == 'ok'
end

#login(login, password) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/instagram/native_client.rb', line 30

def (, password)
   = {:_csrftoken => "missing",
                :username => , 
                :guid => @device_guid,
                :password => password, 
                :device_id => @device_id
              }

  result = post_struct "https://i.instagram.com/api/v1/accounts/login/", 
  puts "login result: #{result.inspect}"

  if 'fail' == result['status']
    if result['message'] == 'checkpoint_required'
      raise ItWasMeRequired
    else
      raise result['message']
    end
  else
    begin
      @ig_user_id = result['logged_in_user']['pk']
      @ig_user_name = result['logged_in_user']['username']
      @csrftoken = @http.cookies.select {|c| c.name == 'csrftoken'}.first.value
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error "Server answer: #{result}"
      raise ex
    end
  end
end

#media_by_tag(tag) ⇒ Object



283
284
285
# File 'lib/instagram/native_client.rb', line 283

def media_by_tag(tag)
  @http.get("http://i.instagram.com/api/v1/feed/tag/#{tag}/?max_id=#{(Time.now.to_f * 1000).to_i}")
end

#search_user(query) ⇒ Object



279
280
281
# File 'lib/instagram/native_client.rb', line 279

def search_user(query)
  @http.get("http://i.instagram.com/api/v1/users/search/?is_typeahead=true&rank_token=#{@ig_user_id}_e9730a31-df06-4c97-aaef-56cdc2601c1a&q=#{query}")
end

#send_sms(phone) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/instagram/native_client.rb', line 183

def send_sms(phone)
  struct = {
             :_csrftoken => @csrftoken,
             :_uid => @ig_user_id,
             :phone_number => phone,
             :_uuid => @device_guid
            }
  parsed = post_struct 'https://i.instagram.com/api/v1/accounts/send_sms_code/', struct
  #puts "Parsed sms result: #{parsed.inspect}"
  parsed['status'] == 'ok' && parsed['phone_number_valid']
end

#unfollow_user(user_id) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/instagram/native_client.rb', line 258

def unfollow_user(user_id)
  post_struct = {
                 :_uid => @ig_user_id,
                 :_csrftoken => @csrftoken,
                 :user_id => user_id
                }
  parsed = post_struct "http://i.instagram.com/api/v1/friendships/destroy/#{user_id}/", post_struct
  parsed['status'] == 'ok'
end

#upload_photo(file_path, description) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/instagram/native_client.rb', line 60

def upload_photo(file_path, description)
  File.open(file_path) do |file|
    photo_info = {
               :_csrftoken => @csrftoken,
               :upload_id => Time.now.to_i,
               :photo => file
               }

    @http.cookies.each {|c| c.domain = 'i.instagram.com'}
    res = @http.post("http://i.instagram.com/api/v1/upload/photo/", photo_info)

    puts "Upload photo result: #{res.body}"
    
    result = JSON.parse(res.body)

    if 'fail' == result['status']
      if result['message'] == 'checkpoint_required'
        raise CheckpointRequired
      else
        raise result['message']
      end
    end

    upload_id = result['upload_id']

    caption_info = {:upload_id => upload_id,
                    :source_type => "3",
                    :filter_type => "0",
                    :device => {
                            :manufacturer => "Sony",
                            :model => "LT22i",
                            :android_version => 16,
                            :android_release => "4.1.2"
                           },
                    :_csrftoken => @csrftoken,
                    :_uuid => @device_guid,
                    :_uid => ig_user_id}
    caption_info.merge!({:caption => description}) unless description.blank?

    @http.cookies.each {|c| c.domain = 'i.instagram.com'}
    parsed = post_struct "https://i.instagram.com/api/v1/media/configure/", caption_info

    result_hash = { server_answer: parsed.to_json }

    if parsed['status'] == 'ok'
      result_hash[:social_shortcode] = parsed['media']['code']
      result_hash[:social_id] = parsed['media']['id']
    end
    result_hash
  end
end

#upload_video(file_path, description) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/instagram/native_client.rb', line 112

def upload_video(file_path, description)
  File.open(file_path) do |file|
    upload_id = Time.now.to_i
    video_info = {
               :_csrftoken => @csrftoken,
               :media_type => 2,
               :upload_id => upload_id
               }

    @http.cookies.each {|c| c.domain = 'i.instagram.com'}
    res = @http.post("http://i.instagram.com/api/v1/upload/video/", video_info)
    result = JSON.parse(res.body)

    chunks_number = result['video_upload_urls'].count
    chunk_size = file.size / chunks_number
    bytes_uploaded = 0

    puts "File size: #{file.size}, chunks_number: #{chunks_number}, chunk_size: #{chunk_size}"

    video_result = nil

    puts result['video_upload_urls'].inspect
    
    byebug

    result['video_upload_urls'].each {|upload_struct|
      bytes_rest = file.size - bytes_uploaded
      is_last_chunk = bytes_rest <= chunk_size * 2

      to_upload = is_last_chunk ? bytes_rest : chunk_size
      file_part = file.read(to_upload)

      puts "To upload: #{to_upload}, bytes_rest: #{bytes_rest}, bytes_uploaded: #{bytes_uploaded}"


      #uri = URI.parse upload_struct['url']
      #@http.cookies.each {|c| c.domain = uri.host}
      result = @http.post(upload_struct['url'], file_part, { "job" => upload_struct['job'], 
                                                              "Content-Range" => "bytes #{bytes_uploaded}-#{bytes_uploaded + to_upload - 1}/#{file.size}"
                                                              # "Content-Type" => "application/octet-stream"
                                                           }
                          )
      byebug

      video_result = JSON.parse(result)['result'] if is_last_chunk
      bytes_uploaded += to_upload
    }
    
  end
end

#user_followed(user_id = nil) ⇒ Object



292
293
294
295
# File 'lib/instagram/native_client.rb', line 292

def user_followed(user_id = nil)
  user_id ||= @ig_user_id
  @http.get("http://i.instagram.com/api/v1/friendships/#{user_id}/followers/")
end

#user_follows(user_id = nil) ⇒ Object



287
288
289
290
# File 'lib/instagram/native_client.rb', line 287

def user_follows(user_id = nil)
  user_id ||= @ig_user_id
  @http.get("http://i.instagram.com/api/v1/friendships/#{user_id}/following/")
end

#verify_account(phone, code) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/instagram/native_client.rb', line 195

def (phone, code)
  struct = {
             :verification_code => code,
             :_uid => @ig_user_id,
             :_csrftoken => @csrftoken,
             :phone_number => phone,
             :_uuid => @device_guid
            }
  parsed = post_struct 'https://i.instagram.com/api/v1/accounts/verify_sms_code/', struct
  puts "Parsed sms result: #{parsed.inspect}"
  parsed['status'] == 'ok' && parsed['verified']
end