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



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

def self.__obj
  @@obj
end

.__obj=(value) ⇒ Object



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

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

.compute_hash(data) ⇒ Object



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

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

.create_md5(data) ⇒ Object



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

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

.generate_device_idObject



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

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

.generate_rank_token(pk) ⇒ Object



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

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

.generate_signature(data) ⇒ Object



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

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

.generate_uuidObject



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

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



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

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

Instance Method Details

#execObject



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

def exec
  http @data
end

#get(url) ⇒ Object



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

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

#http(args) ⇒ Object



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
114
# File 'lib/ig_api/http.rb', line 79

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].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



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

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

#post(url, body = nil) ⇒ Object



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

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

#with(data) ⇒ Object



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

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