|
#!/usr/bin/env ruby |
|
require 'csv' |
|
require 'mechanize' |
|
|
|
# OUYA dev stats scraper |
|
# |
|
# This job will download the downloads and purchases csv files from |
|
# the OUYA dev portal and, based on the purchase identifier and |
|
# game title, will send download, sales and conversion ratio to |
|
# the dashboard. |
|
# |
|
# 2 Number types will be needed in the dhashboard to display the |
|
# data. They need to be named: |
|
# ouya_sales |
|
# ouya_downloads |
|
|
|
# Config |
|
# ------ |
|
PURCHASE_IDENTIFIER = 'YOUR_IDENTIFIER_AS_DEFINED_ON_OUYA_PRODUCTS_PAGE' |
|
GAME_TITLE = 'The title of your game as defined on the OUYA Games page' |
|
USER = '[email protected]' |
|
PASS = 'your Password goes here - this is unsecure so be wary where you host' |
|
|
|
# Init history |
|
currentDownloads = 0 |
|
currentPurchases = 0 |
|
|
|
# Tally the # of downloads in the csv data. Return a hash |
|
# with key = package name and value = total download |
|
# CSV index map |
|
# title: 0 |
|
# package name: 1 |
|
# version: 2 |
|
# date: 3 |
|
# count: 4 |
|
def tallyDownloads(data) |
|
downloads = Hash.new |
|
CSV.new(data, :headers => :first_row).each do |line| |
|
if downloads[line[0]] == nil |
|
downloads[line[0]] = Integer(line[4]) |
|
else |
|
downloads[line[0]] = downloads[line[0]] + Integer(line[4]) |
|
end |
|
end |
|
return downloads |
|
end |
|
|
|
# Tally # of purchases |
|
def tallyPurchases(data) |
|
purchases = Hash.new |
|
CSV.new(data, :headers => :first_row).each do |line| |
|
if purchases[line[0]] == nil |
|
purchases[line[0]] = Integer(line[5]) |
|
else |
|
purchases[line[0]] = purchases[line[0]] + Integer(line[5]) |
|
end |
|
end |
|
return purchases |
|
end |
|
|
|
SCHEDULER.every '60m', :first_in => 0 do |job| |
|
|
|
lastDownloads = currentDownloads |
|
lastPurchases = currentPurchases |
|
|
|
m = Mechanize.new |
|
m.get('https://devs.ouya.tv/developers/sign_in') do |page| |
|
|
|
# Enter credentials |
|
main = page.form_with( :action => '/developers/sign_in') do |f| |
|
user_field = f.field_with( :name => 'developer[login]') |
|
user_field.value = USER |
|
|
|
user_password = f.field_with( :name => 'developer[password]') |
|
user_password.value = PASS |
|
end.click_button |
|
|
|
#Now that we're logged in, grab the two CSV files |
|
m.pluggable_parser.default = Mechanize::File |
|
downloadCSV = m.get('https://devs.ouya.tv/developers/analytics/downloads.csv') |
|
purchaseCSV = m.get('https://devs.ouya.tv/developers/analytics/purchases.csv') |
|
|
|
#Parse them into hashes |
|
htDownloads = tallyDownloads(downloadCSV.content) |
|
htPurchases = tallyPurchases(purchaseCSV.content) |
|
|
|
#Send the configured entries to the board |
|
currentPurchases = htPurchases[PURCHASE_IDENTIFIER] |
|
currentDownloads = htDownloads[GAME_TITLE] |
|
conversion = "Conversion Ratio: " + ((currentPurchases / currentDownloads.to_f) * 100).round(3).to_s + "%" |
|
send_event('ouya_sales', {current: currentPurchases, last: lastPurchases, moreinfo: conversion}) |
|
send_event('ouya_downloads', {current: currentDownloads, last: lastDownloads}) |
|
end |
|
end |