Class: KpApi::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/kp_api/agent.rb

Instance Method Summary collapse

Instance Method Details

#arr_data(data, name) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/kp_api/agent.rb', line 172

def arr_data(data, name)
  s = dn(data, name)
  if s.nil?
    []
  else
    s.split(',').map { |genre| genre.strip }
  end
end

#bool_data(data, name) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kp_api/agent.rb', line 193

def bool_data(data, name)
  s = dn(data, name)

  if s.class == String
    s = s.to_i
  end

  if s.class == TrueClass || s.class == FalseClass
    s
  else
    !s.nil? && s == 1 ? true : false
  end
end

#current_pageObject



40
41
42
# File 'lib/kp_api/agent.rb', line 40

def current_page
  @page
end

#dataObject



12
13
14
# File 'lib/kp_api/agent.rb', line 12

def data
  @json
end

#data2Object



16
17
18
# File 'lib/kp_api/agent.rb', line 16

def data2
  @json2
end

#dn(data, name) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/kp_api/agent.rb', line 267

def dn(data, name)
  if data.nil?
    r = @json[name]
  elsif data == String
    r = name
  else
    if @json[data].nil?
      r = nil
    else
      r = @json[data][name]
    end
  end
  r
end

#film_hash(h, id = 'filmID') ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/kp_api/agent.rb', line 226

def film_hash(h, id='filmID')
  {
    id:                     int_data(String,  h[id            ]),
    kp_type:                str_data(String,  h['type'        ]),
    name_ru:                str_data(String,  h['nameRU'      ]),
    name_en:                str_data(String,  h['nameEN'      ]),
    slogan:                 str_data(String,  h['slogan'      ]),
    description:            str_data(String,  h['description' ]),
    poster_url:             url_data(String,  h['posterURL'   ], h[id], :film),
    year:                   year_data(String, h['year'        ], :start),
    year_end:               year_data(String, h['year'        ], :end),
    reviews_count:          int_data(String,  h['reviewsCount']),
    duration:               min_data(String,  h['filmLength'  ]),
    countries:              arr_data(String,  h['country'     ]),
    genres:                 arr_data(String,  h['genre'       ]),
    video:                  h['videoURL'],
    is_sequel_or_prequel:   bool_data(String, h['hasSequelsAndPrequelsFilms']),
    is_similar_films:       bool_data(String, h['hasRelatedFilms'           ]),
    is_imax:                bool_data(String, h['isIMAX'                    ]),
    is_3d:                  bool_data(String, h['is3D'                      ]),
    rating_mpaa:            str_data(String,  h['ratingMPAA'                ]),
    minimal_age:            int_data(String,  h['ratingAgeLimits'           ])
  }
end

#int_data(data, name, none = 0, type = Integer) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kp_api/agent.rb', line 111

def int_data(data, name, none=0, type=Integer)
  s = dn(data, name)

  if s.nil?
    none
  else
    r = s
    if r.class == String
      r = s.gsub(/[\ \%\$]/i, '')
    end

    if type == Integer
      r.to_i == 0 ? none : r.to_i
    elsif type == Float
      r.to_f == 0 ? none : r.to_f
    else
      r
    end
  end
end

#json(url = nil, bu = true) ⇒ Object

private



46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/kp_api/agent.rb', line 46

def json(url=nil, bu=true)
  if url.nil?
    uri       = URI(@url)
  else
    uri       = URI(url)
  end

  path       = uri.to_s.gsub(DOMAINS[:api],'')
  time_stamp =  Time.now.to_i.to_s
  key = Digest::MD5.hexdigest(path + time_stamp + DOMAINS[:salt])
  begin
    print "[GET] -> " + uri.to_s
    get_time = Time.now

    http              = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 10
    if uri.scheme == "https"
      http.use_ssl = true
    end

    headers = {
      "Android-Api-Version"   => "19",
      "X-SIGNATURE"           => key,
      "device"                => "android",
      "X-TIMESTAMP"           => time_stamp,
      "User-Agent"            => "Android client (4.4 / api19), ru.kinopoisk/4.2.0 (55)",
    }

    response = http.get(uri.request_uri, headers)

    print " <- [#{(Time.now-get_time).round 3}s] [#{response.code}]\n"

    if KpApi::valid_json?(response.body)
      j = JSON.parse(response.body)
      if j['resultCode'] == 0
        j['data']
      else
        j
      end
    else

      {:resultCode => -1, :message=> "Error method require", :data => { :code => response.code, :body => response.body} }
    end
  rescue StandardError => e
    print "\n[Err] -> " + uri.to_s
    raise KpApi::ApiError.new(0, e)
  end

end

#json2Object



96
97
98
99
100
# File 'lib/kp_api/agent.rb', line 96

def json2
  if @json2.nil?
    @json2 = json(@url2)
  end
end

#min_data(data, name) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/kp_api/agent.rb', line 207

def min_data(data, name)
  s = dn(data, name)
  begin
    if s.nil?
      0
    else
      time = Time.parse(s)
      (time.hour * 60) + time.min
    end
  rescue
  end
end

#next_pageObject

For paginate



25
26
27
28
29
30
31
32
33
34
# File 'lib/kp_api/agent.rb', line 25

def next_page
  if @page < @page_count
    @page += 1
    gen_url
    @json = json
    true
  else
    false
  end
end

#page_countObject



36
37
38
# File 'lib/kp_api/agent.rb', line 36

def page_count
  @page_count
end

#people_hash(h) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/kp_api/agent.rb', line 251

def people_hash(h)
  {
    :id          => int_data(String,      h['id'        ]),
    :kp_type     => str_data(String,      h['type'      ]),
    :name_ru     => str_data(String,      h['nameRU'    ]),
    :name_en     => str_data(String,      h['nameEN'    ]),
    :poster_url  => url_data(String,      h['posterURL' ], @id, :name),
    :sex         => str_data(String,      h['sex'       ]),
    :growth      => int_data(String,      h['growth'    ]),
    :birthday    => time_data(String,     h['birthday'  ]),
    :birthplace  => str_data(String,      h['sex'       ]),
    :has_awards  => bool_data(String,     h['has_awards']),
    :profession  => s2a(str_data(String,  h['profession']))
  }
end

#s2a(str) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/kp_api/agent.rb', line 103

def s2a(str)
  if str.nil?
    []
  else
    str.split(',').map { |i| i.strip }
  end
end

#statusObject



4
5
6
# File 'lib/kp_api/agent.rb', line 4

def status
  @json[:resultCode].nil? ? true : false
end

#status2Object



8
9
10
# File 'lib/kp_api/agent.rb', line 8

def status2
  @json[:resultCode].nil? ? true : false
end

#str_data(data, name) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kp_api/agent.rb', line 181

def str_data(data, name)
  s = dn(data, name)

  if s.class == String
    s
  elsif s.class == NilClass
    nil
  else
    s.to_s
  end
end

#time_data(data, name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/kp_api/agent.rb', line 151

def time_data(data, name)
  s = dn(data, name)
  if !s.nil?

    if s.size == 10
      d=Date.parse(s) rescue nil
      if d.nil?
        s=s.gsub('00.', '01.')
        d=Date.parse(s) rescue nil
      end
      d
    else
      year = s.scan(/\d{4}/)[0]
      if !year.nil?
        Date.parse("01.01.#{year}") #only year???
      end
    end

  end
end

#url_data(data, name, id, poster_name) ⇒ Object



220
221
222
223
# File 'lib/kp_api/agent.rb', line 220

def url_data(data, name, id, poster_name)
  s = dn(data, name)
  s.nil? ? nil : "#{DOMAINS[:kinopoisk][:poster][poster_name]}_#{id}.jpg"
end

#year_data(data, name, point = :start) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kp_api/agent.rb', line 132

def year_data(data, name, point=:start)
  s = dn(data, name)

  if s.nil?
    nil
  else
    if s.size == 4 && point == :start
      s.to_i
    else s.size == 9
      arr = s.scan(/(\d{4})/).flatten
      if point == :start
        arr[0].to_i
      elsif point == :end
        arr[1].to_i
      end
    end
  end
end