-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_eventbrite_data.rb
More file actions
218 lines (179 loc) · 6.8 KB
/
get_eventbrite_data.rb
File metadata and controls
218 lines (179 loc) · 6.8 KB
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
require 'net/http'
require 'json'
require 'date'
require 'csv'
# Not used - use get_eventbrite_data_by_series instead
def get_eventbrite_data
if !correct_event_type_input
puts "Please input EL, AA, PEP, or PAW. Example: $ruby get_eventbrite_data.rb EL"
return
end
if teacher_name_input && !correct_teacher_name_input
puts "Please input the teacher's first and last name with an underscore between. Example: $ruby get_eventbrite_data.rb EL Jack_Johnson"
return
end
# Get and filter events to be AA, EL, PEP, or PAW
event_list = get_event_list
# Grab event "id" with event "name.text", and "start.local" (as date, not date time) and put in an array
formatted_events = get_formatted_events(event_list)
puts "Events Retrieved: " + formatted_events.length.to_s
attendees = get_attendees(formatted_events)
open_csv(attendees.flatten)
# make into executable file...
end
def get_event_list
event_list = []
response_body = get_response_body(get_event_list_uri)
raise StandardError.new("There are no events for this search") if !response_body
event_list.push(response_body["events"])
puts "Total Events: " + response_body["pagination"]["object_count"].to_s
output_pages_complete(response_body["pagination"])
while response_body["pagination"]["has_more_items"]
continuation_uri = get_event_list_uri + "&continuation=" + response_body["pagination"]["continuation"]
response_body = get_response_body(continuation_uri)
raise StandardError.new("There was an error retrieving all events for this search") if !response_body
puts response_body["pagination"]["page_number"].to_s + " of " + response_body["pagination"]["page_count"].to_s + " pages retrieved"
event_list.push(response_body["events"])
end
event_list
end
def get_formatted_events(event_list)
ids_list = []
semester_start_date = Date.new(2020, 04, 29)
today = Date.today
event_list.each do |event_group|
event_group.each do |event|
event_date = Date.parse(event["start"]["local"])
if event_date > semester_start_date && event_date < today
ids_list.push({"name": event["name"]["text"], "id": event["id"], "date": event_date.to_s})
end
end
end
puts "Total of " + ids_list.length + " events completed before " + today.to_s
ids_list
end
def get_attendees(formatted_events)
attendees_list = []
formatted_events.each_with_index do |formatted_event, i|
response_body = get_response_body(get_attendees_uri(formatted_event[:id]))
raise StandardError.new("There was an error retrieving attendees for this search") if !response_body
formatted_response = get_formatted_attendees(formatted_event, response_body["attendees"])
attendees_list.push(formatted_response)
puts (i + 1).to_s + " of " + formatted_events.length.to_s + " events complete" if (i + 1) % 5 == 0
end
attendees_list
end
def get_formatted_attendees(event, attendees)
attendees_list = []
attendees.each do |attendee|
next if !attendee["checked_in"]
attendees_list.push(create_attendee(event, attendee))
end
attendees_list
end
def create_attendee(event, attendee)
is_el ? english_lounge_pep_data(event, attendee) : academic_advising_paw_data(event, attendee)
end
def english_lounge_pep_data(event, attendee)
{
"Event Name": event[:name],
"First Name": attendee["profile"]["first_name"],
"Last Name": attendee["profile"]["last_name"],
"Email": attendee["profile"]["email"],
"Ticket Type": attendee["ticket_class_name"],
"Date Attending": event[:date],
"What is your year/position at TIU?": attendee["answers"][0]["answer"],
"What is your major/specialty at TIU?": attendee["answers"][1]["answer"],
"Why are you coming to English Lounge?": attendee["answers"][2]["answer"],
"Who teaches your CB class?": attendee["answers"][4]["answer"],
"Who teaches your EP class?": attendee["answers"][5]["answer"],
"Who teaches this class?": attendee["answers"][6]["answer"]
}
end
def academic_advising_paw_data(event, attendee)
{
"Event Name": event[:name],
"First Name": attendee["profile"]["first_name"],
"Last Name": attendee["profile"]["last_name"],
"Email": attendee["profile"]["email"],
"Ticket Type": attendee["ticket_class_name"],
"Date Attending": event[:date],
"What is your year/position at TIU?": attendee["answers"][0]["answer"],
"What is your major/specialty at TIU?": attendee["answers"][1]["answer"],
"Why are you coming to Academic Advising?": attendee["answers"][2]["answer"],
"Who teaches your AC1 class?": attendee["answers"][5]["answer"],
"Who teaches your AC2 class?": attendee["answers"][6]["answer"],
"Who teaches your EC/BW class?": attendee["answers"][7]["answer"],
"Who teaches your CC class?": attendee["answers"][8]["answer"],
"Who teaches your CB/BS class?": attendee["answers"][9]["answer"],
"Who teaches this class?": attendee["answers"][10]["answer"],
"What assignment, project, or topic are you bringing to Academic Advising?": attendee["answers"][3]["answer"]
}
end
def open_csv(attendees)
file_name = "#{event_type_input}_#{Date.today.strftime("%m_%d_%Y")}.csv"
CSV.open(file_name, "wb") do |csv|
csv << attendees.first.keys # adds the attributes name on the first line
attendees.each do |hash|
csv << hash.values
end
end
puts "Created " + file_name
end
def get_response_body(uri_string)
uri = URI.parse(uri_string)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request["Authorization"] = "Bearer #{bearer_token}"
response = http.request(request)
response.is_a?(Net::HTTPSuccess) ? JSON.parse(response.body) : nil
end
def output_pages_complete(pagination_response)
puts pagination_response["page_number"].to_s + " of " + pagination_response["page_count"].to_s + " pages retrieved"
end
def correct_event_type_input
event_type_input && ["EL", "AA", "PEP", "PAW"].include?(event_type_input)
end
def correct_teacher_name_input
teacher_name_input.include?("_")
end
def event_type_input
ARGV[0]
end
def teacher_name_input
ARGV[1]
end
def organization_id
ENV["EVENTBRITE_ORG_ID"]
end
def bearer_token
ENV["EVENTBRITE_BEARER_TOKEN"]
end
def event_type
case event_type_input
when "EL"
"English Lounge"
when "AA"
"Academic Advising"
else
event_type_input
end
end
def teacher_name
teacher_name_input ? teacher_name_input.gsub("_", " ") : nil
end
def is_el
event_type_input == "EL" || event_type_input == "PEP"
end
def get_event_list_uri
if teacher_name
"https://www.eventbriteapi.com/v3/organizations/#{organization_id}/events/?page_size=200&status=completed&time_filter=current_future&name_filter=#{event_type} with #{teacher_name}"
else
"https://www.eventbriteapi.com/v3/organizations/#{organization_id}/events/?page_size=200&status=completed&time_filter=current_future&name_filter=#{event_type}"
end
end
def get_attendees_uri(event_id)
"https://www.eventbriteapi.com/v3/events/#{event_id}/attendees/"
end
get_eventbrite_data