Skip to content

Instantly share code, notes, and snippets.

@eckolancer
Last active June 14, 2017 15:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eckolancer/48625c07d1820c875f75 to your computer and use it in GitHub Desktop.
Save eckolancer/48625c07d1820c875f75 to your computer and use it in GitHub Desktop.
Android App Rating widget for Dashing

##Preview

Description

Display your Android App Rating info. It uses Goolge Play Store website as the source.

##Usage

To use this widget, copy appstore.html, appstore.coffee, and appstore.scss into the /widgets/appstore directory. Put the app_store.rb file in your /jobs folder.

You'll also need the Google logo. Download it, and put it in your /assets/images folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-id="Android e-Rewards Mobile" data-view="Google" data-title="Android e-Rewards Mobile"></div>
</li>

##Settings

You'll need to set the URL path to your application on the iTunes Store website in the app_store.rb.

appPageUrl = 'https://play.google.com/store/apps/details?id=com.researchNow.eRewards' # e-Rewards Mobile

Rating is fetched every 24 Hours, but you can change that by editing the job schedule.

#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
# Average+Average Voting on an Android App
#
# This job will track the average vote score and number of votes on an app
# that is registered in the google play market by scraping the google play
# market website.
#
# There are two variables send to the dashboard:
# `google_play_voters_total` containing the number of people voted
# `google_play_average_rating` float value with the average votes
# Config
# ------
appPageUrl = 'https://play.google.com/store/apps/details?id=com.researchNow.eRewards'
SCHEDULER.every '24h', :first_in => 0 do |job|
puts "fetching App Store Rating for App: " + appPageUrl
# prepare request
http = Net::HTTP.new("play.google.com", Net::HTTP.https_default_port())
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # disable ssl certificate check
# scrape detail page of appPageUrl
response = http.request( Net::HTTP::Get.new(appPageUrl) )
if response.code != "200"
puts "google play store website communication (status-code: #{response.code})\n#{response.body}"
else
data = {
:last_version => {
average_rating: 0.0,
voters_count: 0},
}
# Version: ... aria-label="4 stars, 2180 Ratings"
average_rating = response.body.scan( /class=[\"\']score[\"\']>([\d,.]+)</m)
print "#{average_rating}\n"
# <span class="rating-count">24 Ratings</span>
voters_count = response.body.scan( /class=[\"\']reviews-num[\"\']>([\d,.]+)</m )
print "#{voters_count}\n"
#last versions average rating
if ( average_rating )
if ( average_rating[0] ) # Last Version
raw_string = average_rating[0][0].gsub('star', '')
clean_string = raw_string.match(/[\d,.]+/i)[0]
last_version_average_rating = clean_string.gsub(",", ".").to_f
half = 0.0
if ( raw_string.match(/half/i) )
half = 0.5
end
last_version_average_rating += half
data[:last_version][:average_rating] = '%.1f' % last_version_average_rating
end
if ( average_rating[1] ) # All Versions
raw_string = average_rating[1][0].gsub('star', '')
clean_string = raw_string.match(/[\d,.]+/i)[0]
all_versions_average_rating = clean_string.gsub(",", ".").to_f
half = 0.0
if ( raw_string.match(/half/i) )
half = 0.5
end
all_versions_average_rating += half
data[:all_versions][:average_rating] = '%.1f' % all_versions_average_rating
end
end
# all and last versions voters count
if ( voters_count )
if ( voters_count[0] ) # Last Version
last_version_voters_count = voters_count[0][0].gsub(',', '').to_i
data[:last_version][:voters_count] = last_version_voters_count
else
puts 'ERROR::RegEx for last version voters count didn\'t match anything'
end
if ( voters_count[1] ) # All Versions
all_versions_voters_count = voters_count[1][0].gsub(',', '').to_i
puts all_versions_voters_count
data[:all_versions][:voters_count] = all_versions_voters_count
else
puts 'ERROR::RegEx for all versions voters count didn\'t match anything'
end
end
if defined?(send_event)
send_event('Android e-Rewards Mobile', data)
else
print "#{data}\n"
end
end
end
class Dashing.Google extends Dashing.Widget
ready: ->
@onData(this)
onData: (data) ->
widget = $(@node)
console.log(data)
last_version = @get('last_version')
rating = last_version.average_rating
console.log(rating)
voters_count = last_version.voters_count
console.log(voters_count)
widget.find('.google-rating-value').html( '<div>Last Version Average Rating</div><span id="google-rating-integer-value">' + rating + '</span>')
widget.find('.google-voters-count').html( '<span id="google-voters-count-value">' + voters_count + '</span> Reviews' )
<h1 class="title" data-bind="title"></h1>
<div class="google-rating-container">
<div class="google-rating-value" data-bind='google'></div>
<div class="google-voters-count" data-bind='google'></div>
</div>
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #999;
$title-color: #000;
// ----------------------------------------------------------------------------
// Widget-comment styles
// ----------------------------------------------------------------------------
.widget-google {
background: $background-color url('../assets/google_play.jpg') no-repeat 50% 50%;
.title {
font-weight: bold;
color: $title-color;
opacity: .9
}
.google-rating-container {
div > div {
font-size: 24px
}
}
.google-rating-value {
font-size: 76px;
color: $title-color;
#google-rating-integer-value {
font-weight: bold;
}
}
.google-voters-count {
font-size: 1.5em;
color: $title-color;
#google-voters-count-value {
font-weight: bold
}
}
}
@amitjethani
Copy link

Hey man, thanks for putting together this gist. Just some minor feedback: the link to the Google logo (google_play.jpg) and your preview is missing in the README file.

@fabLouis
Copy link

This widget is deprecated or not?
When i use it, i have the following JS error: Cannot read property 'average_rating' of undefined This is raised on line https://gist.github.com/eckolancer/48625c07d1820c875f75#file-google-coffee-L9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment