seeds.rb 3.27 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
2
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3

Zac Wood's avatar
Zac Wood committed
4
5
6
7
8
9
require_relative 'patriot_web_parser'
require 'thwait'
require 'httparty'
require 'nokogiri'
require 'json'

10
threads = []
Zac Wood's avatar
Zac Wood committed
11
12
13
14
15
16
17
18
19
20
21
total = {}
parser = PatriotWeb::Parser.new

# get the first semester only
semester = parser.parse_semesters.first

puts "DDOSing Patriot Web, buckle up kids"

# parse all subjects and their courses in the semester
parser.parse_subjects(semester).each do |subject|
  puts "Getting courses for #{subject}"
22
23
24
  threads << Thread.new {
    total[subject] = parser.parse_courses_in_subject(subject)
  }
Zac Wood's avatar
Zac Wood committed
25
26
27
end

# For testing, only get first subject
28
# subject = parser.parse_subjects(semester)[0]
Zac Wood's avatar
Zac Wood committed
29
30
31
# total[subject] = parser.parse_courses_in_subject(subject)

# wait for all the threads to finish
32
ThreadsWait.all_waits(*threads)
Zac Wood's avatar
Zac Wood committed
33
34

# delete everything in the current database
Zac Wood's avatar
Zac Wood committed
35
Closure.delete_all
Zac Wood's avatar
Zac Wood committed
36
CourseSection.delete_all
37
38
39
Course.delete_all
Semester.delete_all

Zac Wood's avatar
Zac Wood committed
40
41
42
43
# create a semester for the next semester
semester = Semester.create! season: 'Fall', year: 2018
semester.save!

44
45
46
47
48
# Taking a course and a list of courses, checks if the course is already in that
# list. If it isn't, create an active record and store it in the list for that
# course. If it is, grab the pre-existing database entry.
def get_course(course, all_courses)
  all_courses.each do |c|
Zach Perkins's avatar
Zach Perkins committed
49
    if c[:course_number] == course[:course_number]
50
51
52
53
54
55
56
57
      return c[:db_object]
    end
  end

  course[:db_object] = Course.create!(course)
  all_courses.push(course)
  course[:db_object]
end
58

Zac Wood's avatar
Zac Wood committed
59
60
total.each do |subject, sections|
  puts "Adding courses for #{subject}..."
61
62
63
  all_sections = []
  all_courses = []

Zac Wood's avatar
Zac Wood committed
64
  sections.each do |section|
65
66
67
68
    if section.nil? || !section.key?(:subj) || !section.key?(:course_number)
      puts "#{subject} failed section: #{section.class}"
      next
    end
Zac Wood's avatar
Zac Wood committed
69

70
71
72
    course = get_course({ subject: section[:subj],
                          course_number: section[:course_number],
                          semester: semester }, all_courses)
Zac Wood's avatar
Zac Wood committed
73

74
75
    instructor = Instructor.find_or_create_by!(name: section[:instructor])

Zac Wood's avatar
Zac Wood committed
76
    section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}"
Zac Wood's avatar
Zac Wood committed
77

Zach Perkins's avatar
Zach Perkins committed
78
79
80
81
82
83
84
85
86
87
    all_sections.push(name: section_name,
                      crn: section[:crn],
                      section_type: section[:type],
                      title: section[:title],
                      start_date: section[:start_date],
                      end_date: section[:end_date],
                      days: section[:days],
                      start_time: section[:start_time],
                      end_time: section[:end_time],
                      location: section[:location],
88
89
                      course: course,
                      instructor: instructor)
Zac Wood's avatar
Zac Wood committed
90
  end
Zac Wood's avatar
Zac Wood committed
91

92
  CourseSection.create!(all_sections)
Zac Wood's avatar
Zac Wood committed
93
end
Zac Wood's avatar
Zac Wood committed
94

Zac Wood's avatar
Zac Wood committed
95
96
97
98
99
100
# create closures for the days there will be no classes
# see: https://registrar.gmu.edu/calendars/fall-2018/
Closure.create! date: Date.new(2018, 9, 3), semester: semester
Closure.create! date: Date.new(2018, 10, 8), semester: semester
(21..25).each { |n| Closure.create! date: Date.new(2018, 11, n), semester: semester }
(10..19).each { |n| Closure.create! date: Date.new(2018, 12, n), semester: semester }