Class: MarketBot::Android::App
- Inherits:
-
Object
- Object
- MarketBot::Android::App
- Defined in:
- lib/market_bot/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, :category_url, :full_screenshot_urls]
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#hydra ⇒ Object
readonly
Returns the value of attribute hydra.
Class Method Summary collapse
Instance Method Summary collapse
- #enqueue_update(&block) ⇒ Object
-
#initialize(app_id, options = {}) ⇒ App
constructor
A new instance of App.
- #market_url ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(app_id, options = {}) ⇒ App
Returns a new instance of App.
142 143 144 145 146 147 148 |
# File 'lib/market_bot/android/app.rb', line 142 def initialize(app_id, ={}) @app_id = app_id @hydra = [:hydra] || MarketBot.hydra @request_opts = [:request_opts] || {} @callback = nil @error = nil end |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
12 13 14 |
# File 'lib/market_bot/android/app.rb', line 12 def app_id @app_id end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
15 16 17 |
# File 'lib/market_bot/android/app.rb', line 15 def callback @callback end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
16 17 18 |
# File 'lib/market_bot/android/app.rb', line 16 def error @error end |
#hydra ⇒ Object (readonly)
Returns the value of attribute hydra.
14 15 16 |
# File 'lib/market_bot/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 |
# File 'lib/market_bot/android/app.rb', line 18 def self.parse(html) result = {} doc = Nokogiri::HTML(html) = doc.css('.meta-info') .each do |info| field_name = info.css('.title').text.strip case field_name when 'Updated' result[:updated] = info.css('.content').text.strip when 'Installs' result[:installs] = info.css('.content').text.strip when 'Size' result[:size] = info.css('.content').text.strip when 'Current Version' result[:current_version] = info.css('.content').text.strip when 'Requires Android' result[:requires_android] = info.css('.content').text.strip when 'Content Rating' result[:content_rating] = info.css('.content').text.strip when 'Contact Developer', 'Developer' info.css('.dev-link').each do |node| node_href = node[:href] if node_href =~ /^mailto:/ result[:email] = node_href.gsub(/^mailto:/,'') else if q_param = URI(node_href).query.split('&').select{ |p| p =~ /q=/ }.first actual_url = q_param.gsub('q=', '') end result[:website_url] = actual_url end end end end node = doc.xpath("//meta[@itemprop='price']").first result[:price] = node[:content].strip rescue 'Free' category_div = doc.css('.category').first result[:category] = category_div.text.strip rescue '' cat_link = category_div["href"] path, cat_name = File.split(cat_link) result[:category_url] = cat_name result[:description] = doc.xpath("//div[@itemprop='description']").first.inner_html.strip result[:title] = doc.xpath("//div[@itemprop='name']").first.text.strip score = doc.css('.score-container').first unless score.nil? node = score.css('.score').first result[:rating] = node.text.strip node = score.xpath("//meta[@itemprop='ratingCount']").first result[:votes] = node[:content].strip end node = doc.xpath("//div[@itemprop='author']") result[:developer] = node.css('.primary').first.text.strip result[:more_from_developer] = [] result[:users_also_installed] = [] result[:related] = [] node = doc.css('.recommendation') node.css('.rec-cluster').each do |recommended| assoc_app_type = recommended.css('.heading').first.text.strip.eql?('Similar' ) ? :related : :more_from_developer recommended.css('.card').each do |card| assoc_app = {} assoc_app[:app_id] = card['data-docid'].strip result[assoc_app_type] << assoc_app end end # Users also installed is no longer on the page, adding this for backwards compatibility, well, sort of.... result[:users_also_installed] = result[:related] node = doc.css('.cover-image').first unless node.nil? result[:banner_icon_url] = node[:src] result[:banner_image_url] = node[:src] end result[:youtube_video_ids] = [] doc.css('.play-action-container').each do |node| url = node['data-video-url'] unless url.nil? result[:youtube_video_ids] << url.split('embed/').last.split('?').first end end result[:screenshot_urls] = [] doc.css('.screenshot').each do |node| result[:screenshot_urls] << node[:src] end result[:full_screenshot_urls] = [] doc.css('.full-screenshot').each do |node| result[:full_screenshot_urls] << node[:src] end node = doc.css('.whatsnew').first result[:whats_new] = node.inner_html.strip unless node.nil? # Stubbing out for now, can't find them in the redesigned page. result[:permissions] = = [] result[:rating_distribution] = { 5 => nil, 4 => nil, 3 => nil, 2 => nil, 1 => nil } histogram = doc.css('div.rating-histogram') cur_index = 5 %w(five four three two one).each do |slot| node = histogram.css(".#{slot.to_s}") result[:rating_distribution][cur_index] = node.css('.bar-number').text.gsub(/,/,'').to_i cur_index -= 1 end result[:html] = html result end |
Instance Method Details
#enqueue_update(&block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/market_bot/android/app.rb', line 162 def enqueue_update(&block) @callback = block @error = nil request = Typhoeus::Request.new(market_url, @request_opts) request.on_complete do |response| result = nil begin result = handle_response(response) rescue Exception => e @error = e end update_callback(result) end hydra.queue(request) self end |
#market_url ⇒ Object
150 151 152 |
# File 'lib/market_bot/android/app.rb', line 150 def market_url "https://play.google.com/store/apps/details?id=#{@app_id}&hl=en" end |
#update ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/market_bot/android/app.rb', line 154 def update resp = Typhoeus::Request.get(market_url, @request_opts) result = handle_response(resp) update_callback(result) self end |