forked from tmurakam/android-checkout-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
android_checkout_scraper.rb
196 lines (158 loc) · 4.8 KB
/
android_checkout_scraper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
gem 'mechanize', '1.0.0'
require 'mechanize'
require 'csv'
#
# Google checkout scraper for Android
#
class AndroidCheckoutScraper
# Google account credencial
attr_accessor :email, :password, :dev_acc
# proxy settings
attr_accessor :proxy_host, :proxy_port
def initialize
@agent = nil
end
def setup
#Mechanize.log = Logger.new("mechanize.log")
#Mechanize.log.level = Logger::INFO
@agent = Mechanize.new
if (@proxy_host && @proxy_host.length >= 1)
@agent.set_proxy(@proxy_host, @proxy_port)
end
end
def try_get(url)
unless @agent
setup
end
# try to get
@agent.get(url)
# login needed?
if (@agent.page.uri.host != "accounts.google.com" ||
@agent.page.uri.path != "/ServiceLogin")
# already logined
return
end
# do login
form = @agent.page.forms.find {|f| f.form_node['id'] == "gaia_loginform"}
if (!form)
raise 'No login form'
end
form.field_with(:name => "Email").value = @email
form.field_with(:name => "Passwd").value = @password
form.click_button
if (@agent.page.uri.host == "accounts.google.com")
STDERR.puts "login failed? : uri = " + @agent.page.uri.to_s
raise 'Google login failed'
end
# retry get
@agent.get(url)
end
# Get merchant sales report
def getSalesReport(year, month)
#url = sprintf('https://market.android.com/publish/salesreport/download?report_date=%04d_%02d', year, month)
url = sprintf('https://play.google.com/apps/publish/salesreport/download?report_date=%04d_%02d&report_type=payout_report&dev_acc=%s', year, month, @dev_acc)
try_get(url)
return @agent.page.body
end
# Get merchant etimated sales report
def getEstimatedSalesReport(year, month)
url = sprintf('https://play.google.com/apps/publish/salesreport/download?report_date=%04d_%02d&report_type=sales_report&dev_acc=%s', year, month, @dev_acc)
try_get(url)
return @agent.page.body
end
# Get order list
# startDate: start date (yyyy-mm-ddThh:mm:ss)
# end: end date (yyyy-mm-ddThh:mm:ss)
# state: financial state, one of followings:
# ALL, CANCELLED, CANCELLED_BY_GOOGLE, CHARGEABLE, CHARGED,
# CHARGING, PAYMENT_DECLINED, REVIEWING
def getOrderList(startDate, endDate, state = "CHARGED", expanded = false)
try_get("https://checkout.google.com/sell/orders")
@agent.page.form_with(:name => "dateInput") do |form|
form["start-date"] = startDate
form["end-date"] = endDate
if (state == "ALL")
form.delete_field!("financial-state")
else
form["financial-state"] = state
end
if (expanded)
form["column-style"] = "EXPANDED"
end
#form["date-time-zone"] = "Asia/Tokyo"
#form["_type"] = "order-list-request"
#form["query-type"] = ""
form.click_button
end
return @agent.page.body
end
# get payout report
#
# type: PAYOUT_REPORT or TRANSACTION_DETAIL_REPORT
def getPayouts(startDay, endDay, type = "PAYOUT_REPORT")
try_get("https://checkout.google.com/sell/payouts")
@agent.page.form_with(:name => "btRangeReport") do |form|
form["startDay"] = "d:" + startDay.to_s
form["endDay"] = "d:" + endDay.to_s
#form["reportType"] = type
form.radiobutton_with(:value => type).check
form.click_button
end
return @agent.page.body
end
# get order details page
def getOrderDetail(orderId)
try_get("https://checkout.google.com/sell/multiOrder?order=#{orderId}&ordersTable=1")
return @agent.page.body
end
# push all deliver buttons
def autoDeliver(auto_archive = false)
# access 'orders' page
try_get("https://checkout.google.com/sell/orders")
more_buttons = true
# 押すべきボタンがなくなるまで、ボタンを押し続ける
while(more_buttons)
more_buttons = false
@agent.page.forms.each do |form|
order_id = nil
order_field = form.field_with(:name => "OrderSelection")
if (order_field)
order_id = order_field.value
end
button = form.button_with(:name => "closeOrderButton")
if (button)
puts "Deliver : #{order_id}"
elsif (auto_archive)
button = form.button_with(:name => "archiveButton")
if (button)
puts "Archive : #{order_id}"
end
end
if (button)
form.click_button(button)
more_buttons = true
break
end
end
end
end
# dump CSV (util)
def dumpCsv(csv_string)
headers = nil
CSV.parse(csv_string) do |row|
if (!headers)
headers = row
next
end
i = 0
row.each do |column|
puts "#{headers[i]} : #{column}"
i = i + 1
end
puts
end
end
end