Module: ThuCourse
- Defined in:
- lib/thu_course.rb,
lib/thu_course/version.rb
Constant Summary collapse
- VERSION =
"0.2.9"
Class Method Summary collapse
- .all(year, semester) ⇒ Object
- .department(year, semester, id) ⇒ Object
- .department_id(year, semester) ⇒ Object
Class Method Details
.all(year, semester) ⇒ Object
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 |
# File 'lib/thu_course.rb', line 7 def self.all(year, semester) uri = URI("http://course.service.thu.edu.tw/view-dept/#{year}/#{semester}/everything").normalize doc = Nokogiri::HTML(open(uri)) hash = [] = doc.css('.b a') # => array of node .each do |a_tag| name = a_tag.text.gsub(/\s+/, ' ').strip dp_uri = uri.merge(a_tag['href'].gsub('view-dept', 'view-ge')) dp_doc = Nokogiri::HTML(open(dp_uri)) tr_tag = dp_doc.css('#no-more-tables tr') tr_tag.each do |tr| td_tag = tr.css('td') next unless td_tag[0] course_id = td_tag[0].css('a').text.strip name = td_tag[1].text.strip credit = td_tag[2].text.strip date = td_tag[3].text.strip teacher = td_tag[4].text.strip num = td_tag[5].text.strip.gsub(/\s+/, '') note = td_tag[6].text.strip hash << { id: course_id, name: name, credit: credit, date: date, teacher: teacher, num: num, note: note } end end hash end |
.department(year, semester, id) ⇒ Object
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 |
# File 'lib/thu_course.rb', line 55 def self.department(year, semester, id) uri = "http://course.thu.edu.tw/view-ge/#{year}/#{semester}/#{id}" dp_doc = Nokogiri::HTML(open(uri)) tr_tag = dp_doc.css('#no-more-tables tr') hash = [] tr_tag.each do |tr| td_tag = tr.css('td') next unless td_tag[0] course_id = td_tag[0].css('a').text.strip name = td_tag[1].text.strip credit = td_tag[2].text.strip date = td_tag[3].text.strip date_text = date date = date_change(date) teacher = [] teacher_data = td_tag[4].css('a') teacher_data.each do |td| teacher_id = '' teacher_id = td['href'].split('/').last if td['href'].split('/').last != 'view-teacher-profile' teacher_name = td.text.strip teacher << { teacher_id: teacher_id, teacher_name: teacher_name } end num = td_tag[5].text.strip.gsub(/\s+/, '') note = td_tag[6].text.strip department = note.split('/').first.strip hash << { id: course_id, name: name, credit: credit, date_text: date_text, date: date, teacher: teacher, department: department, num: num, note: note } end hash end |
.department_id(year, semester) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/thu_course.rb', line 41 def self.department_id(year, semester) uri = URI("http://course.service.thu.edu.tw/view-dept/#{year}/#{semester}/everything").normalize doc = Nokogiri::HTML(open(uri)) = doc.css('.b a') # => array of node hash = [] .each do |a_tag| name = a_tag.text.strip id = a_tag['href'].split('/').last hash << { id: id, name: name } end hash end |