Sha256: 97184e9ec1e694d15085c08fbb9b5a61f999204b981da14c71c1953b71aa3c05

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

class ScraperGooglePlay::Reviews < ScraperGooglePlay::Base
	attr_accessor :package_name

	def initialize(package_name, options = {})
		@package_name = package_name
  	@agent = Mechanize.new
  	@agent.user_agent_alias = 'Linux Firefox'
		@agent.log = Logger.new(STDOUT)

		@data = {
			:pageNum => options[:page] || 0,
      :id => package_name || options[:id],
      :reviewSortOrder => options[:sort] || 0,
      :hl => options[:lang] || 'en',
      :reviewType => 0,
      :xhr => 1
		}
	end

	def fetch_reviews
		@reviews ||= result
	end

	def result
		@page = @agent.post(
		  "#{ScraperGooglePlay::REVIEWSURL}", 
		  @data, 
		  {
		  	'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
			}
		)
		data =  JSON.parse @page.body[6..-1]
		@page = Nokogiri::HTML(data[0][2])
		
		parse_fields
		while (@page.search('.single-review').count >= 40) do
			@data[:pageNum] += 1
			result
		end
		@reviews
	end

	def parse_fields
		@reviews ||= []
		@page.search('.single-review').each do |elm_review|
	    review = {
	    	id: elm_review.at('.review-header').attr('data-reviewid').strip(),
	    	info: elm_review.at('.review-info').text,
	    	userName: elm_review.at('.author-name').text(),
	    	userImage: getUserImage(elm_review),
	    	date: elm_review.at('span.review-date').text().strip(),
	    	score: getScore(elm_review),
	    	reviewContent: elm_review.at('.review-body').text.gsub(' Full Review ', ''),
	    	title: elm_review.at('span.review-title').text,
	    	text: elm_review.at('.review-body').text.gsub(' Full Review ', ''),
	    }
	    @reviews << review
		end
	end

	def getUserImage elm_review
		nil
	end

	def getScore elm_review
		start_rating = elm_review.at('.review-info .current-rating').attributes["style"].text
		case start_rating
		when "width: 100%;"
			5
		when "width: 80%;"
			4
		when "width: 60%;"
			3
		when "width: 40%;"
			2
		when "width: 20%;"
			1
		else 
			0
		end
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scraper_google_play-0.1.9 lib/scraper_google_play/reviews.rb