Class: MarketBot::Play::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/market_bot/play/chart.rb,
lib/market_bot/play/chart/constants.rb

Direct Known Subclasses

Developer

Constant Summary collapse

MAX_PAGES =
6
COLLECTIONS =
%w{
  topselling_free
  topselling_paid
  topgrossing
  topselling_free
  movers_shakers
  topgrossing
  topselling_new_free
  topselling_new_paid
}
CATEGORIES =
%w{
				ANDROID_WEAR
				BOOKS_AND_REFERENCE
				BUSINESS
				COMICS
				COMMUNICATION
				EDUCATION
				ENTERTAINMENT
				FINANCE
				HEALTH_AND_FITNESS
				LIBRARIES_AND_DEMO
				LIFESTYLE
				APP_WALLPAPER
				MEDIA_AND_VIDEO
				MEDICAL
				MUSIC_AND_AUDIO
				NEWS_AND_MAGAZINES
				PERSONALIZATION
				PHOTOGRAPHY
				PRODUCTIVITY
				SHOPPING
				SOCIAL
				SPORTS
				TOOLS
				TRANSPORTATION
				TRAVEL_AND_LOCAL
				WEATHER
				APP_WIDGETS
				GAME_ACTION
				GAME_ADVENTURE
				GAME_ARCADE
				GAME_BOARD
				GAME_CARD
				GAME_CASINO
				GAME_CASUAL
				GAME_EDUCATIONAL
				GAME_MUSIC
				GAME_PUZZLE
				GAME_RACING
				GAME_ROLE_PLAYING
				GAME_SIMULATION
				GAME_SPORTS
				GAME_STRATEGY
				GAME_TRIVIA
				GAME_WORD
				FAMILY?age=AGE_RANGE1
				FAMILY?age=AGE_RANGE2
				FAMILY?age=AGE_RANGE3
				FAMILY_ACTION
				FAMILY_BRAINGAMES
				FAMILY_CREATE
				FAMILY_EDUCATION
				FAMILY_MUSICVIDEO
				FAMILY_PRETEND
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, category = nil, opts = {}) ⇒ Chart

Returns a new instance of Chart.



53
54
55
56
57
58
59
60
# File 'lib/market_bot/play/chart.rb', line 53

def initialize(collection, category=nil, opts={})
	@collection = collection
	@category = category
	@request_opts = MarketBot::Util.build_request_opts(opts[:request_opts])
	@lang = opts[:lang] || MarketBot::Play::DEFAULT_LANG
	@country = opts[:country] || MarketBot::Play::DEFAULT_COUNTRY
     @max_pages = opts[:max_pages] || MarketBot::Play::Chart::MAX_PAGES
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



5
6
7
# File 'lib/market_bot/play/chart.rb', line 5

def category
  @category
end

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/market_bot/play/chart.rb', line 4

def collection
  @collection
end

#countryObject (readonly)

Returns the value of attribute country.



6
7
8
# File 'lib/market_bot/play/chart.rb', line 6

def country
  @country
end

#langObject (readonly)

Returns the value of attribute lang.



7
8
9
# File 'lib/market_bot/play/chart.rb', line 7

def lang
  @lang
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/market_bot/play/chart.rb', line 8

def result
  @result
end

Class Method Details

.parse(html, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
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
# File 'lib/market_bot/play/chart.rb', line 10

def self.parse(html, opts={})
	opts[:lang] ||= MarketBot::Play::DEFAULT_LANG

	results = []
	doc = Nokogiri::HTML(html)

	doc.css('.card').each do |snippet_node|
		result = {}

		details_node = snippet_node.css('.details')

		unless snippet_node.css('img').empty?
			result[:icon_url] = MarketBot::Util.fix_content_url(snippet_node.css('img').first.attributes['src'].value)
		end

		unless snippet_node.css('.current-rating').empty?
			stars_style = snippet_node.css('.current-rating').first.attributes['style'].value
			stars_width_percent = stars_style[/width:\s+([0-9.]+)%/, 1].to_f
			result[:stars] = (5 * stars_width_percent / 100).round(1).to_s
		else
			result[:stars] = nil
		end

		title_node = details_node.css('.title').first
		result[:title] = title_node.attributes['title'].to_s
		result[:rank] = title_node.text.gsub(/\..*/, '').to_i

		if (price_elem = details_node.css('.buy span').first)
			result[:price] = price_elem.text
		end

		result[:developer] = details_node.css('.subtitle').first.attributes['title'].to_s
		result[:package] = details_node.css('.title').first.attributes['href'].to_s.gsub('/store/apps/details?id=', '').gsub(/&feature=.*$/, '')
		result[:store_url] = "https://play.google.com/store/apps/details?id=#{result[:package]}&hl=#{opts[:lang]}"

		result[:price] = '0' if result[:price] == 'Free'

		results << result
	end

	results
end

Instance Method Details

#store_urlsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/market_bot/play/chart.rb', line 62

def store_urls
	urls = []
	start = 0
	num = 100

	@max_pages.times do |i|
		url = 'https://play.google.com/store/apps'
		url << "/category/#{@category}" if @category
		url << "/collection/#{@collection}?"
		url << "start=#{start}&"
		url << "gl=#{@country}&"
		url << "num=#{num}&"
		url << "hl=#{@lang}"

		urls << url
		start += num
	end

	urls
end

#update(opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/market_bot/play/chart.rb', line 83

def update(opts={})
	@result = []

	store_urls.each do |url|
		req = Typhoeus::Request.new(url, @request_opts)
		req.run

		break unless response_handler(req.response)
	end

	@result.flatten!

	self
end