Return to Snippet

Revision: 53030
at November 10, 2011 12:55 by chrisaiv


Updated Code
require 'fileutils'
require 'date'
require 'rubygems'
#sudo gem install excelsior
#sudo gem update excelsior
require 'excelsior'

class CSVToTxtUtil

  INPUT_NAME = "DOWNLOADS-for-19-Apr-2011-to-08-Nov-2011.csv"
  OUTPUT_NAME = "BB_Playbook_Downloads_" + Date.today.to_s + ".txt"
  COLUMN_NUM_FOR_COUNTRY= 8;

  def initialize
    rows = read_csv( INPUT_NAME )
    generate_xml( rows  );
  end

  def read_csv( file )
    rows = Array.new
    Excelsior::Reader.rows( File.open( file , 'r') ) do |row|
      rows << row
    end
    return rows
  end

  def generate_xml( array )
    output = File.new( OUTPUT_NAME, "w")

    ##
    # Data Pertinent to JSON file
    ##
    country = ''
    count = 0
    node = ""
    array.length.times do |i|
      begin
        if country != array[i][COLUMN_NUM_FOR_COUNTRY].strip!.to_s
          if i > 1
            node = "Country:\"#{country}\", Downloads:#{count.to_s}"
            #Verbose Output
            puts node
            #Write to Text File
            output.puts node
          end
          country = array[i][COLUMN_NUM_FOR_COUNTRY].to_s
          count = 0
        end
      rescue Exception=>e
        puts "\n\t\tError: " + e.to_s + "\n\n"
      end
      count = count + 1
      #puts i.to_s + " : " + count.to_s
    end
    node = "Country:\"#{country}\", Downloads: #{count.to_s} "
    puts node
    output.puts node
  end
end

CSVToTxtUtil.new

Revision: 53029
at November 10, 2011 11:46 by chrisaiv


Initial Code
require 'fileutils'
require 'date'
require 'rubygems'
#sudo gem install excelsior
#sudo gem update excelsior
require 'excelsior'

INPUT_NAME = "DOWNLOADS-for-19-Apr-2011-to-08-Nov-2011.csv"
OUTPUT_NAME = "BB_Playbook_Downloads_" + Date.today.to_s + ".txt"

class CSVToTxtUtil

  def initialize
    rows = read_csv( INPUT_NAME )
    generate_xml( rows  );
  end

  def read_csv( file )
    rows = Array.new
    Excelsior::Reader.rows( File.open( file , 'r') ) do |row|
      rows << row
    end
    return rows
  end

  def generate_xml( array )
    output = File.new( OUTPUT_NAME, "w")

    ##
    # Data Pertinent to JSON file
    ##
    country = ''
    count = 0
    node = ""
    array.length.times do |i|
      begin
        if country != array[i][8].strip!.to_s
          if i > 1
            node = "Country:\"#{country}\", Downloads:#{count.to_s}"
            #Verbose Output
            puts node
            #Write to Text File
            output.puts node
          end
          country = array[i][8].to_s
          count = 0
        end
      rescue Exception=>e
        puts "\n\t\tError: " + e.to_s + "\n\n"
      end
      count = count + 1
      #puts i.to_s + " : " + count.to_s
    end
    node = "Country:\"#{country}\", Downloads: #{count.to_s} "
    puts node
    output.puts node
  end
end

CSVToTxtUtil.new

Initial URL
https://appworld.blackberry.com/isvportal/downloadreports/schedule.seam

Initial Description
The Blackberry AppWorld portal allows you to schedule reports which are delivered as CSV.zip files. Although the CSV file offers a lot of detail about operating system and date of download, most of the time I just want to know which users downloaded the app from which countries.  So usually I choose "Country, then date", unpack the .zip and run this (verbose) ruby script.

Initial Title
Ruby: Parsing Blackberry Download Reports

Initial Tags
ruby

Initial Language
Ruby