seeds.rb 2.89 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

4 5 6 7 8 9 10
require_relative 'patriot_web_parser'
require 'thwait'
require 'httparty'
require 'nokogiri'
require 'json'

threads = []
Zac Wood's avatar
Zac Wood committed
11
total = {}
12 13
parser = PatriotWeb::Parser.new

Zac Wood's avatar
Zac Wood committed
14
# get the first semester only
15
semester = parser.parse_semesters.first
16

17 18
puts "DDOSing Patriot Web, buckle up kids"

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

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

31
# wait for all the threads to finish
32 33
ThreadsWait.all_waits(*threads)

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

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

Zac Wood's avatar
Zac Wood committed
44 45 46 47 48 49 50 51 52
total.each do |subject, sections|
  puts "Adding courses for #{subject}..."
  sections.each do |section|
    next if section.nil? || !section.key?(:subj) || !section.key?(:course_number)
    
    # Find or create a course and set its semester
    # TODO: this breaks when you try to do more than one semester,
    # since just the subject + course_number do not uniquely identify a course
    # Check the semester as well
53
    course = Course.find_or_create_by(subject: section[:subj],
Zac Wood's avatar
Zac Wood committed
54
                                      course_number: section[:course_number])
55 56
    course.semester = semester
    course.save!
57

Zac Wood's avatar
Zac Wood committed
58 59
    section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}"
    
60 61
    puts "Adding #{section_name}..."

Zac Wood's avatar
Zac Wood committed
62
    CourseSection.create!(name: section_name,
63
                    crn: section[:crn],
Zac Wood's avatar
Zac Wood committed
64 65 66 67 68 69 70 71 72
                    section_type: section[:type],
                    title: section[:title],
                    instructor: section[:instructor],
                    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],
73
                    course: course)
Zac Wood's avatar
Zac Wood committed
74
                   
75 76
  end
end
Zac Wood's avatar
Zac Wood committed
77

78 79
# create closures for the days there will be no classes
# see: https://registrar.gmu.edu/calendars/fall-2018/
Zac Wood's avatar
Zac Wood committed
80 81 82 83
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 }