Skip to content

Instantly share code, notes, and snippets.

@d-sea
Created August 28, 2020 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-sea/32f7aa761ad6ad6aaeaa216357360018 to your computer and use it in GitHub Desktop.
Save d-sea/32f7aa761ad6ad6aaeaa216357360018 to your computer and use it in GitHub Desktop.
買い忘れ防止のためのメール通知スクリプト (使い切る前に買い時をお知らせ。特定の日にちサイクルで通知する)
require 'date'
require 'mail'
require 'json'
notify_term = 7
buy_now_body = ""
yet_body = ""
File.open("item-list.json", "r+:UTF-8") do |f|
f.each_line { |line|
item = JSON.parse(line)
case item["shop"]
when "Amazon"
shop_url = "https://www.amazon.co.jp/"
when "Rakuten"
shop_url = "https://www.rakuten.co.jp/"
when "Yodobashi"
shop_url = "https://www.yodobashi.com/"
when "カインズ"
shop_url = "http://www.cainz.com/shop/"
when "サミット"
shop_url = "(自分で買いに行ってね)"
else
shop_url = "(自分で調べてね)"
end
if Date.parse(item["updated_date"]) + item["cycle"] - notify_term < Date.today
p "該当しました。 " + item["name"]
buy_now_body << '- ' + item["name"] + ' を ' + item["shop"] + ' ' + shop_url + ' で買ってね。サイクル : ' + item["cycle"].to_s + ', 前
回購入日 : ' + item["updated_date"].to_s + "\n"
# mail.deliver
else
display_item = "購入日まであと " + (item["cycle"] - (Date.today - Date.parse(item["updated_date"])).to_i).to_s + "日 : " + item["name"]
yet_body << "\n" + display_item
p display_item
end
}
end
# Mail Settings
mail = Mail.new do
from '(put your from email address'
to '(put your send email address)'
subject '[オキニ] 購入時期です'
body buy_now_body + "\n" + yet_body
end
options = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => '(put your from email address)',
:password => '(put your password)',
:authentication => :plain,
:enable_starttls_auto => true
}
mail.charset = 'utf-8'
mail.delivery_method(:smtp, options)
unless buy_now_body.empty?
mail.deliver
p "Mail sent!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment