home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / amazon < prev    next >
Text File  |  2020-01-01  |  3KB  |  94 lines

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