amazon_report 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amazon_report.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 kimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # AmazonReport
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'amazon_report'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install amazon_report
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/amazon_report/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["kimoto"]
6
+ gem.email = ["sub+peerler@gmail.com"]
7
+ gem.description = %q{amazon affiliate}
8
+ gem.summary = %q{amazon affiliate}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "amazon_report"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AmazonReport::VERSION
17
+ end
@@ -0,0 +1,19 @@
1
+ #!/bin/env ruby
2
+ #
3
+ require 'amazon_report'
4
+
5
+ p email = ENV["AMAZON_USERNAME"]
6
+ p password = ENV["AMAZON_PASSWORD"]
7
+ from = to = Time.parse("2012/11/25 00:00:00 UTC")
8
+
9
+ AmazonReport.logger = Logger.new(STDERR)
10
+
11
+ f = AmazonReport::Fetcher.new(email, password)
12
+ parser = AmazonReport::Parser.new
13
+
14
+ data = f.fetch_orders_xml("alfalfalafa-22", from, to)
15
+ p parser.parse_orders_xml(data)
16
+
17
+ data = f.fetch_sales_xml("alfalfalafa-22", from, to)
18
+ p parser.parse_sales_xml(data)
19
+
@@ -0,0 +1,10 @@
1
+ require "amazon_report/version"
2
+ require 'amazon_report/fetch'
3
+ require 'amazon_report/parser'
4
+
5
+ module AmazonReport
6
+ def self.logger=(logger)
7
+ Fetcher.logger = logger
8
+ Parser.logger = logger
9
+ end
10
+ end
@@ -0,0 +1,122 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # Author: kimoto
4
+ require 'mechanize'
5
+ require 'nokogiri'
6
+ require 'time'
7
+ require 'logger'
8
+
9
+ module AmazonReport
10
+ class AmazonReportError < StandardError; end
11
+ class LoginError < StandardError; end
12
+
13
+ class Fetcher
14
+ LOGIN_URL = 'https://affiliate.amazon.co.jp/gp/associates/login/login.html'
15
+ TOP_URL = 'https://affiliate.amazon.co.jp/gp/associates/network/reports/main.html'
16
+ BLACK_CURT = "BLACK-CURT"
17
+ class UnknownTrackingId < StandardError; end
18
+
19
+ @@logger = Logger.new(nil)
20
+ def self.logger=(logger)
21
+ @@logger = logger
22
+ end
23
+
24
+ def initialize(email, password)
25
+ @agent = Mechanize.new{ |agent|
26
+ agent.user_agent_alias = 'Windows IE 7'
27
+ }
28
+ login(email, password)
29
+ end
30
+
31
+ def fetch_xml(tracking_code, from, to, options={})
32
+ go_top
33
+ choose_tracking(tracking_code)
34
+ report(from, to, options)
35
+ end
36
+
37
+ def fetch_orders_xml(tracking_code, from, to)
38
+ fetch_xml(tracking_code, from, to, :report_type => 'ordersReport')
39
+ end
40
+
41
+ def fetch_sales_xml(tracking_code, from, to)
42
+ fetch_xml(tracking_code, from, to, :report_type => 'salesReport')
43
+ end
44
+
45
+ private
46
+ def login(email, password, url = LOGIN_URL)
47
+ if email.nil? or password.nil?
48
+ raise ArgumentError.new
49
+ end
50
+ @agent.get(url)
51
+ @agent.page.form_with(:name => 'sign-in') {|f|
52
+ f.field_with(:name => 'email').value = email
53
+ f.field_with(:name => 'password').value = password
54
+ f.click_button
55
+ }
56
+ end
57
+
58
+ def go_top
59
+ @agent.get TOP_URL
60
+ end
61
+
62
+ def choose_tracking(tracking_id)
63
+ @tracking_id = tracking_id
64
+ @agent.page.link_with(:href => /trendsReport/).click()
65
+ @agent.page.form_with(:name => 'idbox_tracking_id_form'){ |f|
66
+ f.field_with(:name => 'idbox_tracking_id').value = tracking_id
67
+ f.submit()
68
+ }
69
+
70
+ registered_tracking_ids = begin
71
+ Nokogiri::HTML(@agent.page.body).search("select[name = 'idbox_tracking_id']").first.search("option").map(&:text)
72
+ rescue
73
+ []
74
+ end
75
+
76
+ unless registered_tracking_ids.include? tracking_id
77
+ raise UnknownTrackingId
78
+ end
79
+ end
80
+
81
+ # :period_type => :exact
82
+ def report(from, to, options = {})
83
+ options = {
84
+ :period_type => :exact,
85
+ :report_type => 'earningsReport',
86
+ :format => :xml
87
+ }.merge(options)
88
+
89
+ @agent.page.form_with(:name => 'htmlReport'){ |f|
90
+ f['program'] = 'all'
91
+ f['tag'] = @tracking_id
92
+
93
+ if options[:period_type] == :exact
94
+ f.radiobutton_with(:value => 'exact').check
95
+ f['startYear'] = from.year
96
+ f['startMonth'] = from.month - 1
97
+ f['startDay'] = from.day
98
+ f['endYear'] = to.year
99
+ f['endMonth'] = to.month - 1
100
+ f['endDay'] = to.day
101
+ else
102
+ f.radiobutton_with(:value => 'preSelected').check
103
+
104
+ # 昨日分抽出するやつ
105
+ f['preSelectedPeriod'] = 'yesterday'
106
+ f['periodType'] = 'preSelected'
107
+ end
108
+
109
+ f['reportType'] = options[:report_type]
110
+
111
+ if options[:format] == :xml
112
+ f.click_button(f.button_with(:name => 'submit.download_XML'))
113
+ elsif options[:format] == :csv
114
+ f.click_button(f.button_with(:name => 'submit.download_CSV'))
115
+ else
116
+ f.click_button(f.button_with(:name => 'submit.display'))
117
+ end
118
+ }
119
+ @agent.page.body
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,67 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # Author: kimoto
4
+
5
+ module AmazonReport
6
+ class Parser
7
+ class BlackCartError < StandardError; end
8
+
9
+ @@logger = Logger.new(nil)
10
+ def self.logger=(logger)
11
+ @@logger = logger
12
+ end
13
+
14
+ def parse_sales_xml(xml_string)
15
+ hash = {}
16
+ doc = Nokogiri::XML(xml_string)
17
+ doc.search("Items > Item").each{ |item|
18
+ asin = item.attributes["ASIN"].value
19
+ revenue = item.attributes["Revenue"].value
20
+ qty = item.attributes["Qty"].value
21
+ earnings = item.attributes["Earnings"].value
22
+ date = Time.parse(item.attributes["Date"].value)
23
+ hash[asin] = {
24
+ :revenue => revenue,
25
+ :qty => qty,
26
+ :earnings => earnings,
27
+ }
28
+ }
29
+ hash
30
+ end
31
+
32
+ def parse_orders_xml(xml_string)
33
+ hash = {}
34
+ doc = Nokogiri::XML(xml_string)
35
+ doc.search("Items > Item").each{ |item|
36
+ asin = item.attributes["ASIN"].value
37
+ nqty = item.attributes["NQty"].value # その他注文された商品
38
+ dqty = item.attributes["DQty"].value # 注文数合計
39
+ qty = item.attributes["Qty"].value # 直接リンクによる注文数
40
+ clicks = item.attributes["Clicks"].value # クリック数
41
+ date = Time.parse(item.attributes["Date"].value) # 日付
42
+ tag = item.attributes["Tag"].value
43
+ hash[asin] = {
44
+ :nqty => nqty,
45
+ :dqty => dqty,
46
+ :qty => qty,
47
+ :clicks => clicks,
48
+ }
49
+ }
50
+ doc.search("ItemsNoOrders > Item").each{ |item|
51
+ asin = item.attributes["ASIN"].value
52
+ clicks = item.attributes["Clicks"].value
53
+ hash[asin] = {
54
+ :nqty => "0",
55
+ :dqty => "0",
56
+ :qty => "0",
57
+ :clicks => clicks
58
+ }
59
+ }
60
+ hash
61
+ end
62
+
63
+ def to_number(string)
64
+ string.gsub(/,/, "").to_i
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module AmazonReport
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazon_report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kimoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: amazon affiliate
15
+ email:
16
+ - sub+peerler@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - amazon_report.gemspec
27
+ - examples/fetch.rb
28
+ - lib/amazon_report.rb
29
+ - lib/amazon_report/fetch.rb
30
+ - lib/amazon_report/parser.rb
31
+ - lib/amazon_report/version.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: amazon affiliate
56
+ test_files: []
57
+ has_rdoc: