lecture-attendance-manager/app/jobs/scrape_calendar_job.rb
Joshua Coles d9d182bc94 Update lecture properties and various migration changes
Modified code to remove 'status' field from lecture's table and added 'event_uuid' field. Also refined the 'start_time' field in recordings' table to handle datetime instead of strings. Further, made minor changes in scraping jobs and seeding logic. Implemented these changes to allow lectures to be associated with calendar events and streamlined various fields.
2023-10-01 19:44:20 +01:00

72 lines
2.1 KiB
Ruby

class ScrapeCalendarJob < ApplicationJob
queue_as :default
def clean_up_lecture_title(unit_code, short_lecture_title) end
def perform(*args)
ics_file = HTTParty.get("https://mytimetable.bath.ac.uk/ical?6519757b&group=false&timetable=!MjAyMyFzdHVkZW50c2V0ITRDRjQ5MjlGRTg1M0Q4N0MyMDZENTVDNUQ3QTJFNzk0&eu=amMzMDkxQGJhdGguYWMudWs=&h=MiuDbRiudE_Yf7B25v2SfEuFCtmYGkFb5sAUI3yGmtY=")
calendars = Icalendar::Calendar.parse(ics_file)
calendar = calendars.first
unit_codes = Course.all.map(&:unit_code)
events = calendar.events.map do |event|
summary = event.summary
match = summary.split('-')
# Handle odd events we don't care about
next if match.length != 2
unit_code = match[0]
short_lecture_title = match[1]
start_time = event.dtstart.to_time
event_uuid = event.uid.to_s
next if unit_code.nil? ||
short_lecture_title.nil? ||
event_uuid.nil? ||
start_time.nil? ||
!unit_code.in?(unit_codes)
{
unit_code:,
short_lecture_title:,
start_time:,
event_uuid:,
}
end
events
.compact
.filter { |attrs| attrs[:unit_code].in? unit_codes }
.group_by { |attrs| attrs[:unit_code] }
.map do |unit_code, course_events|
course = Course.find_by(unit_code: unit_code)
lecture_counter = 0
course_events.each do |event|
# Naive check to see if we've already created this lecture
if course.lectures.find_by(event_uuid: event[:event_uuid]).present? || course.lectures.find_by(
start_time: (event[:start_time].beginning_of_hour + 5.minutes)..(event[:start_time].beginning_of_hour + 1.hour + 5.minutes)
).present?
next
end
if event[:short_lecture_title].starts_with? "Lec"
lecture_counter += 1
title = "Lecture #{lecture_counter}"
else
title = event[:short_lecture_title]
end
course.lectures.create!(
title: title,
start_time: event[:start_time],
event_uuid: event[:event_uuid],
)
end
end
end
end