home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / scripts / ckermit / amazon.~1~ < prev    next >
Text File  |  2010-04-06  |  3KB  |  92 lines

  1. #!/p/kd/fdc/solaris9/wermit +
  2. #!/opt/local/bin/kermit +
  3. #
  4. # fdc, Tue Apr  6 15:11:48 2010
  5. #
  6. # Summarizes product orders or product clicks from an Amazon ORDERS
  7. # (not EARNINGS) report, which has been downloaded from the Amazon 
  8. # Associate site as a TSV (Tab-separated value) file.
  9. #
  10. # Usage: tally.ksc tsvfilename [ { orders, clicks } ] > outputfile
  11. #
  12. # The optional second argument is either ORDERS or CLICKS.  If you leave
  13. # off the second argument it defaults to ORDERS.  The program reads the
  14. # file and calculates how many orders (or clicks) were received for each
  15. # product, and then lists the products in descending order of orders
  16. # (or clicks) so you can see which were the most ordered or the most
  17. # looked at.
  18. #
  19. # Requires C-Kermit 9.0 or later.
  20. #
  21. # Amazon Order report fields (separated by Tab) are:
  22. #  1. Product Line
  23. #  2. Item Name
  24. #  3. ASIN
  25. #  4. Date (yyyy/mm/dd)
  26. #  5. Link Type
  27. #  6. Tracking ID
  28. #  7. Product Link Conversion
  29. #  8. Product Link Clicks
  30. #  9. Items Ordered Through Product Links
  31. # 10. All Other Items Ordered
  32. # 11. Total Items Ordered
  33.  
  34. .myname := \fbasename(\%0)              # Name of this script
  35. def usage exit 1 {                      # Usage message
  36.     "Usage: \m(myname) filename.tsv [{orders,clicks}] > outputfile"
  37. }
  38. if not def \%1 usage                    # First argument is required
  39. .what = orders                          # Count orders by default
  40. if def \%2 .what := \%2                 # Or count what arg2 says
  41.  
  42. fopen /read \%c \fcontents(\%1)         # Open TSV file
  43. if fail exit 1                          # Exit upon failure
  44.  
  45. set case off                            # Set up orders or clicks report
  46. switch \m(what) {
  47.   :orders
  48.      .field = 11, .orders = 1, .clicks = 0
  49.      echo ORDERS report...
  50.      break
  51.   :clicks
  52.      .field =  8, .orders = 0, .clicks = 1
  53.      echo CLICKS report...
  54.      break
  55.   :default
  56.      usage
  57. }
  58. .items = 0                              # Initialize items counter
  59.  
  60. while true {                            # Loop through records
  61.     fread /line \%c line                # Read one record
  62.     if fail break                       # Check for EOF
  63.     .\%n := \fsplit(\m(line),&a,\9,ALL,,1) # Split record into fields (\9=Tab)
  64.     if equ "" "\&a[2]" continue         # If there is no item skip this record
  65.     if < \%n field continue             # Skip record if not enough fields 
  66.     if not num \&a[field] continue      # Skip if count field is not numeric
  67.     increment items                     # Count an item
  68.     .\%a[2] := \fsubstitute(\%a[2],\40\41,) # Avoid an obscure syntax problem
  69.     _incr count<\&a[2]> \&a[field]      # Count the quantity
  70.     incr t \&a[field]                   # Accumulate total quantity
  71. }
  72. fclose \%c                              # Close TSV file
  73.  
  74. .m := \faaconvert(count,&x,&y)          # Convert associative array
  75. array sort /num /rev &y &x              # Sort result arrays
  76. for i 1 m 1 {                           # Display results
  77.     echo "\flpad(\&y[i],6)  \&x[i]"
  78. }
  79. if orders {                             # For orders...
  80.   echo Distinct items ordered: \m(items)
  81.   echo Total items ordered: \m(t)
  82. } else {                                # For clicks...
  83.   echo Distinct items clicked: \m(items)
  84.   echo Total items clicked: \m(t)
  85. }
  86. exit 0
  87.  
  88. ; Local Variables:
  89. ; comment-column:40
  90. ; comment-start:"# "
  91. ; End:
  92.