Add toggl scraping
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
class ScrapeTogglJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(*args)
|
||||
courses = Course.all
|
||||
|
||||
courses.each do |course|
|
||||
self.scrape_course(course)
|
||||
end
|
||||
end
|
||||
|
||||
# @param [Course] course
|
||||
def scrape_course(course)
|
||||
toggl_project_id = course.toggl_project
|
||||
lectures = course.lectures.order(:start_time).where(associated_toggl_entry: nil)
|
||||
return if lectures.empty?
|
||||
|
||||
entries_data = Toggl::entries_for_project(
|
||||
toggl_project_id,
|
||||
|
||||
# TODO: Work out better limits
|
||||
start_time: Time.new('2023-01-01'),
|
||||
end_time: Time.new('2024-01-01')
|
||||
)
|
||||
|
||||
lectures.each do |lecture|
|
||||
entries_data.find do |entry|
|
||||
associated_entry = entry['time_entries'].find do |inner_entry|
|
||||
(Time.new(inner_entry['start']) - lecture.start_time) < 15.seconds
|
||||
end
|
||||
|
||||
if associated_entry.present?
|
||||
lecture.update!(associated_toggl_entry: associated_entry['id'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user