<commit_before># coding: utf-8
require 'mechanize'
require 'openssl'
require 'nokogiri'
require 'io/console'

class MoneyForward
	def initialize
		@agent = Mechanize.new{|a| a.ssl_version, a.verify_mode = "SSLv23", OpenSSL::SSL::VERIFY_NONE}
		@login_url = 'https://moneyforward.com/users/sign_in'
		@base_url = 'https://moneyforward.com/'
	end

	def login
		begin
			id = ENV['MONEYFORWARD_ID']
			pass = ENV['MONEYFORWARD_PASS']

			puts "Login MoneyForward..."
			if id == nil
				print "ID: "
				id = STDIN.gets.chomp
			end

			if pass == nil
				print "Pass: "
				pass = STDIN.noecho(&:gets).chomp
				puts ""
			end

			@agent.get(@login_url) do |page|
				page.form_with(:id => 'new_sign_in_session_service') do  |form|
					form.field_with(:name => "sign_in_session_service[email]").value = id 
					form.field_with(:name => "sign_in_session_service[password]").value = pass 
				end.click_button
			end
			sleep 1

			if not login?
				puts "ログインに失敗しました"
				exit
			end
		end while not login?

		if need_two_step_verifications?
			two_step_verifications
		end
	end

	#require: ログインが終わっている
	#ensure: 2段階認証を完了する
	def two_step_verifications
		begin
			print "2段階認証のコードを入力してください: "
			verification_code = STDIN.gets.chomp

			two_step_verifications_url = "https://moneyforward.com/users/two_step_verifications"
			@agent.get(two_step_verifications_url) do |page|
				page.form_with(:action => '/users/two_step_verifications/verify') do |form|
					form.field_with(:name => "verification_code").value = verification_code
				end.click_button
			end
			sleep 1
		end while need_two_step_verifications?
	end

	def need_two_step_verifications?
		return @agent.page.uri.to_s.include?("two_step_verifications")
	end

	def login?
		return !@agent.page.search("//a[@href=\"/users/sign_out\"]").empty?
	end

	def add(payments)
		account = select_account

		last_payment_date = get_last_payment_date(account)
		payments.delete_if {|payment| payment.day <= last_payment_date}

		puts "追加されていない支払いが#{payments.size.to_s}件あります．"

		payments.each do |payment|
			add_history(payment)
		end

		if payments.size != 0
			record_last_payment_date(payments[0].day)
		else
			record_last_payment_date(last_payment_date)
		end
	end

	def add_history(payment)
		@agent.get("https://moneyforward.com/")
		sleep 1

		puts payment.to_s
		@agent.page.form_with(:id => "js-cf-manual-payment-entry-form") do |form|
			form["user_asset_act[large_category_id]"] = payment.large_category_id
			form["user_asset_act[middle_category_id]"] = payment.middle_category_id
			form.field_with(:name => "user_asset_act[sub_account_id_hash]").option_with(:value => @payment_account[0]).click<commit_msg>必要のない出力を削除<commit_after># coding: utf-8
require 'mechanize'
require 'openssl'
require 'nokogiri'
require 'io/console'

class MoneyForward
	def initialize
		@agent = Mechanize.new{|a| a.ssl_version, a.verify_mode = "SSLv23", OpenSSL::SSL::VERIFY_NONE}
		@login_url = 'https://moneyforward.com/users/sign_in'
		@base_url = 'https://moneyforward.com/'
	end

	def login
		begin
			id = ENV['MONEYFORWARD_ID']
			pass = ENV['MONEYFORWARD_PASS']

			puts "Login MoneyForward..."
			if id == nil
				print "ID: "
				id = STDIN.gets.chomp
			end

			if pass == nil
				print "Pass: "
				pass = STDIN.noecho(&:gets).chomp
				puts ""
			end

			@agent.get(@login_url) do |page|
				page.form_with(:id => 'new_sign_in_session_service') do  |form|
					form.field_with(:name => "sign_in_session_service[email]").value = id 
					form.field_with(:name => "sign_in_session_service[password]").value = pass 
				end.click_button
			end
			sleep 1

			if not login?
				puts "ログインに失敗しました"
				exit
			end
		end while not login?

		if need_two_step_verifications?
			two_step_verifications
		end
	end

	#require: ログインが終わっている
	#ensure: 2段階認証を完了する
	def two_step_verifications
		begin
			print "2段階認証のコードを入力してください: "
			verification_code = STDIN.gets.chomp

			two_step_verifications_url = "https://moneyforward.com/users/two_step_verifications"
			@agent.get(two_step_verifications_url) do |page|
				page.form_with(:action => '/users/two_step_verifications/verify') do |form|
					form.field_with(:name => "verification_code").value = verification_code
				end.click_button
			end
			sleep 1
		end while need_two_step_verifications?
	end

	def need_two_step_verifications?
		return @agent.page.uri.to_s.include?("two_step_verifications")
	end

	def login?
		return !@agent.page.search("//a[@href=\"/users/sign_out\"]").empty?
	end

	def add(payments)
		account = select_account

		last_payment_date = get_last_payment_date(account)
		payments.delete_if {|payment| payment.day <= last_payment_date}

		payments.each do |payment|
			add_history(payment)
		end

		if payments.size != 0
			record_last_payment_date(payments[0].day)
		else
			record_last_payment_date(last_payment_date)
		end
	end

	def add_history(payment)
		@agent.get("https://moneyforward.com/")
		sleep 1

		puts payment.to_s
		@agent.page.form_with(:id => "js-cf-manual-payment-entry-form") do |form|
			form["user_asset_act[large_category_id]"] = payment.large_category_id
			form["user_asset_act[middle_category_id]"] = payment.middle_category_id
			form.field_with(:name => "user_asset_act[sub_account_id_hash]").option_with(:value => @payment_account[0]).click
{'fraction_non_alphanumeric': 0.12105163608851902, 'fraction_numerical': 0.004161149990542841, 'mean_word_length': 4.309236947791165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5250942', 'n_tokens_mistral': 2300, 'n_tokens_neox': 1990, 'n_words': 337}