Ruby: Parsing Blackberry Download Reports


/ Published in: Ruby
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. require 'fileutils'
  2. require 'date'
  3. require 'rubygems'
  4. #sudo gem install excelsior
  5. #sudo gem update excelsior
  6. require 'excelsior'
  7.  
  8. class CSVToTxtUtil
  9.  
  10. INPUT_NAME = "DOWNLOADS-for-19-Apr-2011-to-08-Nov-2011.csv"
  11. OUTPUT_NAME = "BB_Playbook_Downloads_" + Date.today.to_s + ".txt"
  12. COLUMN_NUM_FOR_COUNTRY= 8;
  13.  
  14. def initialize
  15. rows = read_csv( INPUT_NAME )
  16. generate_xml( rows );
  17. end
  18.  
  19. def read_csv( file )
  20. rows = Array.new
  21. Excelsior::Reader.rows( File.open( file , 'r') ) do |row|
  22. rows << row
  23. end
  24. return rows
  25. end
  26.  
  27. def generate_xml( array )
  28. output = File.new( OUTPUT_NAME, "w")
  29.  
  30. ##
  31. # Data Pertinent to JSON file
  32. ##
  33. country = ''
  34. count = 0
  35. node = ""
  36. array.length.times do |i|
  37. begin
  38. if country != array[i][COLUMN_NUM_FOR_COUNTRY].strip!.to_s
  39. if i > 1
  40. node = "Country:\"#{country}\", Downloads:#{count.to_s}"
  41. #Verbose Output
  42. puts node
  43. #Write to Text File
  44. output.puts node
  45. end
  46. country = array[i][COLUMN_NUM_FOR_COUNTRY].to_s
  47. count = 0
  48. end
  49. rescue Exception=>e
  50. puts "\n\t\tError: " + e.to_s + "\n\n"
  51. end
  52. count = count + 1
  53. #puts i.to_s + " : " + count.to_s
  54. end
  55. node = "Country:\"#{country}\", Downloads: #{count.to_s} "
  56. puts node
  57. output.puts node
  58. end
  59. end
  60.  
  61. CSVToTxtUtil.new

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.