Class: Instagram::NativeClient
- Inherits:
-
Object
- Object
- Instagram::NativeClient
- Defined in:
- lib/instagram/native_client.rb
Instance Attribute Summary collapse
-
#csrftoken ⇒ Object
Returns the value of attribute csrftoken.
-
#device_guid ⇒ Object
Returns the value of attribute device_guid.
-
#device_id ⇒ Object
Returns the value of attribute device_id.
-
#http ⇒ Object
Returns the value of attribute http.
-
#ig_user_id ⇒ Object
Returns the value of attribute ig_user_id.
-
#ig_user_name ⇒ Object
Returns the value of attribute ig_user_name.
-
#private_key ⇒ Object
Returns the value of attribute private_key.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#useragent ⇒ Object
Returns the value of attribute useragent.
Instance Method Summary collapse
- #change_profile_picture(picture_path = 'http://lorempixel.com/150/150/') ⇒ Object
- #check_username(uname) ⇒ Object
- #create_account(uname, pass, email = nil, first_name = "") ⇒ Object
- #direct_inbox ⇒ Object
- #follow_user(user_id) ⇒ Object
- #get_news ⇒ Object
- #get_timeline ⇒ Object
-
#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
constructor
A new instance of NativeClient.
- #like_media(media_id) ⇒ Object
- #login(login, password) ⇒ Object
- #media_by_tag(tag) ⇒ Object
- #search_user(query) ⇒ Object
- #send_sms(phone) ⇒ Object
- #unfollow_user(user_id) ⇒ Object
- #upload_photo(file_path, description) ⇒ Object
- #upload_video(file_path, description) ⇒ Object
- #user_followed(user_id = nil) ⇒ Object
- #user_follows(user_id = nil) ⇒ Object
- #verify_account(phone, code) ⇒ Object
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. |= OpenSSL::SSL::OP_NO_SSLv3 end |
Instance Attribute Details
#csrftoken ⇒ Object
Returns the value of attribute csrftoken.
5 6 7 |
# File 'lib/instagram/native_client.rb', line 5 def csrftoken @csrftoken end |
#device_guid ⇒ Object
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_id ⇒ Object
Returns the value of attribute device_id.
5 6 7 |
# File 'lib/instagram/native_client.rb', line 5 def device_id @device_id end |
#http ⇒ Object
Returns the value of attribute http.
5 6 7 |
# File 'lib/instagram/native_client.rb', line 5 def http @http end |
#ig_user_id ⇒ Object
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_name ⇒ Object
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_key ⇒ Object
Returns the value of attribute private_key.
5 6 7 |
# File 'lib/instagram/native_client.rb', line 5 def private_key @private_key end |
#proxy ⇒ Object
Returns the value of attribute proxy.
5 6 7 |
# File 'lib/instagram/native_client.rb', line 5 def proxy @proxy end |
#useragent ⇒ Object
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
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/instagram/native_client.rb', line 154 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..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
200 201 202 203 204 205 206 207 |
# File 'lib/instagram/native_client.rb', line 200 def check_username(uname) login_info = { :_csrftoken => @csrftoken, :username => uname } parsed = post_struct "https://instagram.com/api/v1/users/check_username/", login_info parsed['status'] == 'ok' && parsed['available'] end |
#create_account(uname, pass, email = nil, first_name = "") ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/instagram/native_client.rb', line 209 def create_account(uname, pass, email = nil, first_name = "") email ||= generate_email(uname) account_info = { :_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/", account_info raise RegisterException.new(uname, pass, email, @proxy) unless parsed['status'] == 'ok' end |
#direct_inbox ⇒ Object
234 235 236 237 |
# File 'lib/instagram/native_client.rb', line 234 def direct_inbox @http..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
239 240 241 242 243 244 245 246 247 |
# File 'lib/instagram/native_client.rb', line 239 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_news ⇒ Object
229 230 231 232 |
# File 'lib/instagram/native_client.rb', line 229 def get_news @http..each {|c| c.domain = 'i.instagram.com'} res = @http.get("http://i.instagram.com/api/v1/news/inbox/") #, "Content-Type" => "application/json" end |
#get_timeline ⇒ Object
224 225 226 227 |
# File 'lib/instagram/native_client.rb', line 224 def get_timeline @http..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
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/instagram/native_client.rb', line 259 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 login(login, password) login_info = {:_csrftoken => "missing", :username => login, :guid => @device_guid, :password => password, :device_id => @device_id } result = post_struct "https://i.instagram.com/api/v1/accounts/login/", login_info 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..select {|c| c.name == 'csrftoken'}.first.value rescue => ex Rails.logger.error ex. Rails.logger.error "Server answer: #{result}" raise ex end end end |
#media_by_tag(tag) ⇒ Object
274 275 276 |
# File 'lib/instagram/native_client.rb', line 274 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
270 271 272 |
# File 'lib/instagram/native_client.rb', line 270 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
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/instagram/native_client.rb', line 174 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
249 250 251 252 253 254 255 256 257 |
# File 'lib/instagram/native_client.rb', line 249 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 |
# 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..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) 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..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
103 104 105 106 107 108 109 110 111 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 |
# File 'lib/instagram/native_client.rb', line 103 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..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
283 284 285 286 |
# File 'lib/instagram/native_client.rb', line 283 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
278 279 280 281 |
# File 'lib/instagram/native_client.rb', line 278 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
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/instagram/native_client.rb', line 186 def verify_account(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 |