Skip to content

Instantly share code, notes, and snippets.

@matsubo
Last active May 2, 2022
Embed
What would you like to do?

Toonesの利用料金取得

  • Toones の利用料がAPIで取得できないので、CSVファイルをダウンロードして計算するスクリプトです。
  • 前月の21日から、当月の20日までの利用料金を表示します。

Setup

% docker-compose run bundle install

Retrieve

% docker-compose run -e EMAIL=foo@example.com -e PASSWORD=your_password app bundle exec ruby main.rb
version: '3'
services:
app:
build: .
volumes:
- .:/app
stdin_open: true
tty: true
tmpfs:
- /tmp
working_dir: '/app'
volumes:
- bundle:/usr/local/bundle
- .:/app
environment:
- TZ=Asia/Tokyo
command: 'bundle exec ruby main.rb'
volumes:
bundle:
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "mechanize", "~> 2.8"
gem "activesupport", "~> 7.0"
# frozen_string_literal: true
require 'bundler/setup'
Bundler.require
require 'active_support/all'
email = ENV['EMAIL']
password = ENV['PASSWORD']
login_url = 'https://my.toones.jp/logins/'
agent = Mechanize.new
agent.get(login_url) do |page|
page.form_with(id: 'LoginsCheckForm') do |login_form|
login_form.field_with(id: 'CustomerMail').value = email
login_form.field_with(id: 'CustomerPassword').value = password
login_form.click_button
end
end
point_url = 'https://my.toones.jp/point_tops/index/'
agent.get(point_url) do |page|
page.form_with(id: 'PointTopDownloadPointForm') do |download_form|
download_form.field_with(id: 'PointTopFromDate').value = 1.month.ago.strftime('%Y/%m/21')
download_form.field_with(id: 'PointTopToDate').value = Time.now.strftime('%Y/%m/20')
file = download_form.click_button
# this body contains
# \r\n and \n
body = file.body
utf8 = body.force_encoding(Encoding::SHIFT_JIS).encode(Encoding::UTF_8).gsub(/\R+/, "\n")
total = 0
require 'csv'
CSV.parse(utf8, headers: true).each do |row|
price = row['金額'].to_i
continue if 0 < price
total += price
end
puts total * -1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment