Class: IgApi::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_api/http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__objObject



22
23
24
# File 'lib/ig_api/http.rb', line 22

def self.__obj
  @@obj
end

.__obj=(value) ⇒ Object



18
19
20
# File 'lib/ig_api/http.rb', line 18

def self.__obj=(value)
  @@obj = value
end

.compute_hash(data) ⇒ Object



14
15
16
# File 'lib/ig_api/http.rb', line 14

def self.compute_hash(data)
  OpenSSL::HMAC.hexdigest OpenSSL::Digest.new('sha256'), Constants::PRIVATE_KEY[:SIG_KEY], data
end

.create_md5(data) ⇒ Object



40
41
42
# File 'lib/ig_api/http.rb', line 40

def self.create_md5(data)
  Digest::MD5.hexdigest(data).to_s
end

.generate_device_idObject



44
45
46
47
# File 'lib/ig_api/http.rb', line 44

def self.generate_device_id
  timestamp = Time.now.to_i.to_s
  'android-' + create_md5(timestamp)[0..16]
end

.generate_rank_token(pk) ⇒ Object



115
116
117
# File 'lib/ig_api/http.rb', line 115

def self.generate_rank_token(pk)
  format('%s_%s', pk, IgApi::Http.generate_uuid)
end

.generate_signature(data) ⇒ Object



49
50
51
52
# File 'lib/ig_api/http.rb', line 49

def self.generate_signature(data)
  data = data.to_json
  compute_hash(data) + '.' + data
end

.generate_uuidObject



32
33
34
35
36
37
38
# File 'lib/ig_api/http.rb', line 32

def self.generate_uuid
  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.gsub(/[xy]/) do |c|
    r = (Random.rand * 16).round | 0
    v = c == 'x' ? r : (r & 0x3 | 0x8)
    c.gsub(c, v.to_s(16))
  end.downcase
end

.singletonObject



26
27
28
29
30
# File 'lib/ig_api/http.rb', line 26

def self.singleton
  @@obj = Http.new unless defined? @@obj

  @@obj
end

Instance Method Details

#execObject



69
70
71
# File 'lib/ig_api/http.rb', line 69

def exec
  http @data
end

#get(url) ⇒ Object



73
74
75
76
# File 'lib/ig_api/http.rb', line 73

def get(url)
  @data = {method: 'GET', url: url}
  self
end

#http(args) ⇒ Object



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
111
112
113
# File 'lib/ig_api/http.rb', line 78

def http(args)
  args[:url] = URI.parse(args[:url])
  http = Net::HTTP.new(args[:url].host, args[:url].port,
                       ENV['INSTAGRAM_PROXY_HOST'], ENV['INSTAGRAM_PROXY_PORT'])
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = nil
  if args[:method] == 'POST'
    request = Net::HTTP::Post.new(args[:url].path)
  elsif args[:method] == 'GET'
    request = Net::HTTP::Get.new(args[:url].path + (!args[:url].query.nil? ? '?' + args[:url].query : ''))
  elsif args[:method] == 'MULTIPART'
    request = Net::HTTP::Post::Multipart.new args[:url].path, args[:body],
                                             'User-Agent': args[:ua],
                                             Accept: IgApi::Constants::HEADER[:accept],
                                             'Accept-Encoding': IgApi::Constants::HEADER[:encoding],
                                             'Accept-Language': 'en-US',
                                             'X-IG-Capabilities': IgApi::Constants::HEADER[:capabilities],
                                             'X-IG-Connection-Type': IgApi::Constants::HEADER[:type],
                                             Cookie: args[:session] || ''
  end

  unless args[:method] == 'MULTIPART'
    request.initialize_http_header('User-Agent': args[:ua],
                                   Accept: IgApi::Constants::HEADER[:accept],
                                   'Accept-Encoding': IgApi::Constants::HEADER[:encoding],
                                   'Accept-Language': 'en-US',
                                   'X-IG-Capabilities': IgApi::Constants::HEADER[:capabilities],
                                   'X-IG-Connection-Type': IgApi::Constants::HEADER[:type],
                                   Cookie: args[:session] || '')

    request.body = args.key?(:body) ? args[:body] : nil
  end

  http.request(request)
end

#multipart(url, body = nil) ⇒ Object



59
60
61
62
# File 'lib/ig_api/http.rb', line 59

def multipart(url, body = nil)
  @data = { method: 'MULTIPART', url: url, body: body }
  self
end

#post(url, body = nil) ⇒ Object



54
55
56
57
# File 'lib/ig_api/http.rb', line 54

def post(url, body = nil)
  @data = { method: 'POST', url: url, body: body }
  self
end

#with(data) ⇒ Object



64
65
66
67
# File 'lib/ig_api/http.rb', line 64

def with(data)
  data.each { |k, v| @data[k] = v }
  self
end