Class: DmgAndroidApi::Android::App

Inherits:
Object
  • Object
show all
Defined in:
lib/dmg_android_api/android/app.rb

Constant Summary collapse

MARKET_ATTRIBUTES =
[:title, :rating, :updated, :current_version, :requires_android,
:category, :installs, :size, :price, :content_rating, :description,
:votes, :developer, :more_from_developer, :users_also_installed,
:related, :banner_icon_url, :banner_image_url, :website_url, :email,
:youtube_video_ids, :screenshot_urls, :whats_new, :permissions,
:rating_distribution, :html]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, options = {}) ⇒ App



169
170
171
172
173
174
175
# File 'lib/dmg_android_api/android/app.rb', line 169

def initialize(app_id, options={})
  @app_id = app_id
  @hydra = options[:hydra] || DmgAndroidApi.hydra
  @request_opts = options[:request_opts] || {}
  @callback = nil
  @error = nil
end

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



12
13
14
# File 'lib/dmg_android_api/android/app.rb', line 12

def app_id
  @app_id
end

#callbackObject (readonly)

Returns the value of attribute callback.



15
16
17
# File 'lib/dmg_android_api/android/app.rb', line 15

def callback
  @callback
end

#errorObject (readonly)

Returns the value of attribute error.



16
17
18
# File 'lib/dmg_android_api/android/app.rb', line 16

def error
  @error
end

#hydraObject (readonly)

Returns the value of attribute hydra.



14
15
16
# File 'lib/dmg_android_api/android/app.rb', line 14

def hydra
  @hydra
end

Class Method Details

.parse(html) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
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
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
95
96
97
98
99
100
101
102
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dmg_android_api/android/app.rb', line 18

def self.parse(html)
  result = {}

  doc = Nokogiri::HTML(html)

  extra_info = doc.css('.meta-info')

  extra_info.each do |info|

    field_name  = info.css('.title').text

    case field_name
    when 'Updated'
      result[:updated] = info.css('.content').text
    when 'Current Version'
      result[:current_version] = info.css('.content').text
    when 'Requires Android'
      result[:requires_android] = info.css('.content').text
    when 'Category'
      result[:category] = info.css('.content').text
    when 'Installs'
      result[:installs] = info.css('.content').text
    when 'Size'
      result[:size] = info.css('.content').text
    when 'Content Rating'
      result[:content_rating] = info.css('.content').text
    end
  end
  result[:description] = doc.css('.text-body div').first.inner_html

  result[:title] = doc.css('.document-title').first.text

  rating_elem = doc.css('.score')
  result[:rating] = rating_elem.text unless rating_elem.empty?

  votes_elem = doc.css('.reviews-num')
  result[:votes] = votes_elem.text unless votes_elem.empty?

  result[:developer] = doc.css('.document-subtitle.primary').text

  result[:more_from_developer] = []
  result[:users_also_installed] = []
  result[:related] = []

  price_info = doc.css('.buy-button-container.apps.medium.play-button')

  price_span = price_info.css('meta[itemprop="price"]').first

  if price_span.attributes['content'].text  == 'Free'
      result[:price] = '0'
  else
    result[:price] = price_span.attributes['content'].text
  end
  if similar_elem = doc.css('.doc-similar').first
    similar_elem.children.each do |similar_elem_child|
      assoc_app_type = similar_elem_child.attributes['data-analyticsid'].text

      next unless %w(more-from-developer users-also-installed related).include?(assoc_app_type)

      assoc_app_type = assoc_app_type.gsub('-', '_').to_sym
      result[assoc_app_type] ||= []

      similar_elem_child.css('.app-left-column-related-snippet-container').each do |app_elem|
        assoc_app = {}

        assoc_app[:app_id] = app_elem.attributes['data-docid'].text

        result[assoc_app_type] << assoc_app
      end
    end
  end

  result[:banner_icon_url] = doc.css('.cover-container img').first.attributes['src'].value

  if image_elem = doc.css('.doc-banner-image-container img').first
    result[:banner_image_url] = image_elem.attributes['src'].value
  else
    result[:banner_image_url] = nil
  end

  if website_elem = doc.css('a').select{ |l| l.text.include?("Visit Developer's Website")}.first
    redirect_url = website_elem.attribute('href').value

    if q_param = URI(redirect_url).query.split('&').select{ |p| p =~ /q=/ }.first
      actual_url = q_param.gsub('q=', '')
    end

    result[:website_url] = actual_url
  end

  if email_elem = doc.css('a').select{ |l| l.text.include?("Email Developer")}.first
    result[:email] = email_elem.attribute('href').value.gsub(/^mailto:/, '')
  end

  unless (video_section_elem = doc.css('.doc-video-section')).empty?
    urls = video_section_elem.children.css('embed').map{ |e| e.attribute('src').value }
    result[:youtube_video_ids] = urls.map{ |u| /youtube\.com\/v\/(.*)\?/.match(u)[1] }
  else
    result[:youtube_video_ids] = []
  end

  
  screenshots = doc.css('.thumbnails img')

  if screenshots && screenshots.length > 0
    result[:screenshot_urls] = screenshots.map { |s| s.attributes['src'].value }
  else
    result[:screenshot_urls] = []
  end

  recent_changes = doc.css('.recent-change')
  result[:whats_new] = ''
  recent_changes.each do |change|

    result[:whats_new] << change.text
    result[:whats_new] << '<br>'

  end
  result[:permissions] = permissions = []
  perm_types = ['dangerous', 'safe']
  perm_types.each do |type|
    doc.css("#doc-permissions-#{type} .doc-permission-group").each do |group_elem|
      title = group_elem.css('.doc-permission-group-title').text
      group_elem.css('.doc-permission-description').each do |desc_elem|
        #permissions << { :security => type, :group => title, :description => desc_elem.text }
      end
      descriptions = group_elem.css('.doc-permission-description').map { |e| e.text }
      descriptions_full = group_elem.css('.doc-permission-description-full').map { |e| e.text }
      (0...descriptions.length).each do |i|
        desc = descriptions[i]
        desc_full = descriptions_full[i]
        permissions << { :security => type, :group => title, :description => desc, :description_full => desc_full }
      end
    end
  end

  result[:rating_distribution] = { 5 => nil, 4 => nil, 3 => nil, 2 => nil, 1 => nil }

  if (histogram = doc.css('div.histogram-table').first)
    cur_index = 5
    histogram.css('tr').each do |e|
      result[:rating_distribution][cur_index] = e.children.last.inner_text.gsub(/[^0-9]/, '').to_i
      cur_index -= 1
    end
  end

  result[:html] = html

  result
end

Instance Method Details

#enqueue_update(&block) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/dmg_android_api/android/app.rb', line 189

def enqueue_update(&block)
  @callback = block
  @error = nil

  request = Typhoeus::Request.new(market_url, @request_opts)

  request.on_complete do |response|
    # HACK: Typhoeus <= 0.4.2 returns a response, 0.5.0pre returns the request.
    response = response.response if response.is_a?(Typhoeus::Request)

    result = nil

    begin
      result = App.parse(response.body)
    rescue Exception => e
      @error = e
    end

    update_callback(result)
  end

  hydra.queue(request)

  self
end

#market_urlObject



177
178
179
# File 'lib/dmg_android_api/android/app.rb', line 177

def market_url
  "https://play.google.com/store/apps/details?id=#{@app_id}&hl=en"
end

#updateObject



181
182
183
184
185
186
187
# File 'lib/dmg_android_api/android/app.rb', line 181

def update
  resp = Typhoeus::Request.get(market_url, @request_opts)
  result = App.parse(resp.body)
  update_callback(result)

  self
end