-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_script.rb
284 lines (230 loc) · 7.39 KB
/
my_script.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*
#
require 'time'
require 'io/console'
require 'tempfile'
require 'mail'
require 'byebug'
require 'fileutils'
require 'google/apis/calendar_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
$logger = Logger.new("./log/invoice.log.#{Time.now.strftime("%Y%m%d")}")
$gmail_pwd = ''
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
"invoice_script.yaml")
CLIENT_SECRETS_PATH = 'client_secrets.json'
APPLICATION_NAME = 'Ruby Invoice Script'
GOOGLE_LOGIN = 'nique.rio'
USAGE = "ruby my_script.rb [fourDigitYear-twoDigitMonth]"
TEACHING_CALENDAR_ID='[email protected]'
def get_gmail_pwd
#Get Gmail Password
puts "Gmail Password for " + GOOGLE_LOGIN
system "stty -echo"
$stdout.flush
$gmail_pwd = $stdin.gets.chomp
system "stty echo"
end
def authorize
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
authorizer = Google::Auth::UserAuthorizer.new(
client_id, SCOPE, token_store)
user_id = 'default'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(
base_url: OOB_URI)
puts "Open the following URL in the browser and enter the " +
"resulting code after authorization"
puts url
code = $stdin.gets.chomp
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI)
end
credentials
end
class Student
def initialize(name,lesson_type,parent,rate,email)
@name, @lesson_type, @parent, @email = name, lesson_type, parent, email
@rate = rate.to_i
@lessons = Array.new
@amount_due = 0.0
@teacher_email = '[email protected]'
@from_address = '[email protected]'
@email_subject = ''
@email_body = ''
end
attr_reader :name, :lesson_type, :parent, :rate, :email, :amount_due, :lessons
def add_lesson(start,finish)
@lessons.push( { start: start, finish: finish} )
end
def run
send_email
end
private
def find_amount_due
sum = 0
@lessons.each do |lesson|
sum += (lesson[:finish] - lesson[:start])/3600
end
@amount_due = (sum * @rate)
end
def create_message
#Creates Email Message based on info from calendar and info from csv file.
#Requires @lessons to be populated.
#Subject Determined by @lesson_type
if @lesson_type == 'Piano'
@email_subject = "Piano Teaching Invoice for #{@lessons[0][:start].strftime("%B")}"
elsif @lesson_type == 'Tutoring'
@email_subject = "Tutoring Invoice for #{@lessons[0][:start].strftime("%B")}"
end
#Intro for Parents Different than for Adults.
if @name != @parent
@email_body += "Hi #{@parent},\n\nHere's what I have on the calendar for #{@name} " \
"for #{@lessons[0][:start].strftime("%B")}:\n\n"
else
@email_body += "Hi #{@parent},\n\nHere's what I have on the calendar for you " \
"for #{@lessons[0][:start].strftime("%B")}:\n\n"
end
#Sort Lessons by Date
sorted_lessons = @lessons.sort_by{ |lesson| lesson[:start] }
sorted_lessons.each do |lesson|
@email_body += lesson[:start].strftime("%A, %B %d from %I:%M - ")
@email_body += lesson[:finish].strftime("%I:%M\n")
end
find_amount_due
@email_body += "\nTotal due for #{@lessons[0][:start].strftime("%B")}: $%.2f.\n\n" \
"See You Soon-\n-Monique\n" % @amount_due
end
def send_email
flag = true
create_message
while flag do
puts @email
puts @email_body
puts "What do you want to do? Send: s; Edit e; Skip: n "
to_do = STDIN.getch
if to_do == 's'
mail = Mail.new
mail[:from] = @teacher_email
mail[:to] = @email
mail[:cc] = @teacher_email
mail[:subject] = @email_subject
mail[:body] = @email_body
tries = 0;
begin
tries += 1
mail.delivery_method :smtp, {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'localhost',
:user_name => GOOGLE_LOGIN,
:password => $gmail_pwd,
:authentication => 'plain',
:enable_starttls_auto => true }
mail.deliver!
rescue Net::SMTPAuthenticationError => e
if tries < 3
puts e.message
puts "Wrong Gmail Password. Try again"
get_gmail_pwd
retry
else
puts e.message
abort("Too many tries")
end
end
$logger.info("SENT message to #{@email} <#{@parent}>\n#{@email_body}\n")
flag = false #Done with this student
elsif to_do == 'e'
temp = Tempfile.new('invoice')
temp.write(@email_body)
temp.flush
system("vim #{temp.path}")
temp.rewind
@email_body = temp.read
temp.close
temp.unlink
flag = true #try again with updated email_body
elsif to_do == 'n'
$logger.info("SKIPPED message for #{@email} <#{@parent}>")
flag = false #Done with this student
else
flag = true #try again since you entered something stupid
end
end
end
end
#-------------------------------------
# MAIN
#-------------------------------------
#Hash that holds instances of Student; Key: studentNameString; Value: Student;
students = Hash.new
#Process CSV file
IO.foreach("students.txt") do |s|
s.strip!
row = s.split(';')
students[row[0]] = Student.new(*row)
end
#Deal with first Argument
first_arg = ARGV[0]
if !first_arg then abort(USAGE) end
#Process Year Month Command Line Arguments
year,month = first_arg.split('-')
year = year.to_i
month = month.to_i
if month > 12 || month < 1 then abort(USAGE) end
if month == 12
next_month = 1
next_year = year+1
else
next_month = month + 1
next_year = year
end
start_date = Time.new(year, month)
end_date = Time.new(next_year, next_month)
##Parse other Command Line Arguments; Not really needed for Ruby version
get_gmail_pwd
#Auth with Google
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
params = {
order_by: 'startTime',
show_deleted: false,
single_events: true,
time_min: start_date.strftime("%FT%T%:z"),
time_max: end_date.strftime("%FT%T%:z"),
}
#Get lesson data from Google Calendar
result = service.list_events(TEACHING_CALENDAR_ID, params)
page_token = nil
while true
events = result.items
events.each do |e|
title = e.summary;
puts title
title = title.gsub(/ Lesson/, '')
title = title.gsub(/ Tutoring/, '')
time_s = e.start.date_time.to_s
time_e = e.end.date_time.to_s
students[title].add_lesson(Time.parse(time_s),Time.parse(time_e))
end
if !(page_token=result.next_page_token)
break
end
result = client.execute(:api_method => calendar.events.list,
:parameters => params.merge({'pageToken' => page_token}),
)
end
#Iterate over students. Sends Email 'n' stuff for each instance of students.
students.each do |key,s|
if !s.lessons.empty?
s.run
end
end
$logger.close