leonardita 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -3
- data/README.md +83 -24
- data/leonardita.gemspec +23 -25
- data/lib/leonardita/dcb/mentoring.rb +6 -3
- data/lib/leonardita/dcb/schedule_scraper.rb +108 -0
- data/lib/leonardita/dcb/workshops.rb +6 -3
- data/lib/leonardita/scraper.rb +57 -0
- data/lib/leonardita/version.rb +1 -1
- data/lib/leonardita.rb +31 -5
- metadata +17 -27
- data/.github/workflows/main.yml +0 -25
- data/.gitignore +0 -8
- data/.rubocop.yml +0 -26
- data/.ruby-version +0 -1
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -67
- data/Rakefile +0 -16
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/lib/leonardita/dcb/schedule_scrapper.rb +0 -55
- data/lib/leonardita/scrapper.rb +0 -29
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 128a00d992a69a603dc579e21d1ffe7e5ea91433d8a0900e77b478304ce13d38
|
|
4
|
+
data.tar.gz: 210e22b2458baba41641e8197eb6576d67f755ae994c6f45ce4b486849d75501
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27c43c503ea1ea450b6a5b43d7f47a4ee40e92315c408064453a7f5fac538cabaccad01656d2518c0d7c0557103d1faeaab65942a9bff05acff1ca46d7771ed9
|
|
7
|
+
data.tar.gz: 8cf2565268d3782fb0dba616575e9930f768dda5388e0d95c01dbeafe3f731e01b50a7b20c04a606f14536abf8cda56a4c7fef1222ce3f0015ed3dc0bdf5dd0f
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.2.0] - 2026-12-02
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Saturday sessions (`"saturday"` in `days`).
|
|
15
|
+
- `Leonardita::HTTPError` (with `#status`) is raised when a page cannot be
|
|
16
|
+
fetched, instead of silently parsing an error page.
|
|
17
|
+
- HTTP redirects are followed (up to 5), with connection timeouts and a
|
|
18
|
+
`leonardita/<version>` User-Agent.
|
|
19
|
+
- `Leonardita.mentoring` and `Leonardita.workshops` accept `reload: true` to
|
|
20
|
+
bypass the memoized result.
|
|
21
|
+
- `Dcb::Mentoring.new` and `Dcb::Workshops.new` accept an optional URL.
|
|
22
|
+
- Test suite (Minitest, WebMock, VCR and HTML fixtures) with 100% line and
|
|
23
|
+
branch coverage.
|
|
24
|
+
- GitHub Actions CI for Ruby 3.3, 3.4 and 4.0, plus Dependabot for Bundler and
|
|
25
|
+
GitHub Actions.
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- **Breaking:** requires Ruby 3.3 or newer.
|
|
30
|
+
- **Breaking:** `Leonardita::Scrapper` and `Leonardita::Dcb::ScheduleScrapper`
|
|
31
|
+
were renamed to `Leonardita::Scraper` and `Leonardita::Dcb::ScheduleScraper`.
|
|
32
|
+
The old names remain as deprecated aliases and will be removed in a future
|
|
33
|
+
release.
|
|
34
|
+
- **Breaking:** `form_url` and `access_data` are `nil` when a session has no
|
|
35
|
+
registration form or access information (previously empty strings or an
|
|
36
|
+
exception).
|
|
37
|
+
- The schedule parser was rewritten for the current DCB page layout. Columns
|
|
38
|
+
are now located by their header labels instead of fixed positions.
|
|
39
|
+
- `access_data` keeps line breaks and full text, including accented characters.
|
|
40
|
+
- Pages are fetched over HTTPS from the new DCB URLs
|
|
41
|
+
(`https://dcb.ingenieria.unam.mx/asesorias/` and `/talleres/`).
|
|
42
|
+
- Pages are always parsed as UTF-8.
|
|
43
|
+
- Development dependencies moved to the `Gemfile`; Nokogiri requirement
|
|
44
|
+
updated to `~> 1.16`.
|
|
45
|
+
- Gem metadata now points to https://github.com/3zcurdia/leonardita.
|
|
46
|
+
|
|
47
|
+
### Fixed
|
|
48
|
+
|
|
49
|
+
- The scrapers returned an empty schedule because the DCB site moved its pages
|
|
50
|
+
(the old URLs redirect to a 404) and changed its markup.
|
|
51
|
+
|
|
1
52
|
## [0.1.0] - 2021-06-25
|
|
2
53
|
|
|
3
|
-
- Initial release
|
|
4
|
-
- Mentoring schedule
|
|
5
|
-
- Workshops schedule
|
|
54
|
+
- Initial release.
|
|
55
|
+
- Mentoring schedule scraper.
|
|
56
|
+
- Workshops schedule scraper.
|
data/README.md
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
# Leonardita
|
|
2
2
|
|
|
3
|
-
[](https://github.com/3zcurdia/leonardita/actions/workflows/ruby.yml)
|
|
4
|
+
[](https://rubygems.org/gems/leonardita)
|
|
4
5
|
|
|
5
|
-
Scraping
|
|
6
|
+
Scraping tools for the websites of the Engineering Faculty (Facultad de
|
|
7
|
+
Ingeniería) of UNAM. It currently supports the schedules published by the
|
|
8
|
+
Division of Basic Sciences (DCB):
|
|
9
|
+
|
|
10
|
+
- [Asesorías](https://dcb.ingenieria.unam.mx/asesorias/) (mentoring sessions)
|
|
11
|
+
- [Talleres](https://dcb.ingenieria.unam.mx/talleres/) (workshops)
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
Ruby 3.3 or newer (tested on 3.3, 3.4 and 4.0).
|
|
6
16
|
|
|
7
17
|
## Installation
|
|
8
18
|
|
|
9
19
|
Add this line to your application's Gemfile:
|
|
10
20
|
|
|
11
21
|
```ruby
|
|
12
|
-
gem
|
|
22
|
+
gem "leonardita"
|
|
13
23
|
```
|
|
14
24
|
|
|
15
25
|
And then execute:
|
|
@@ -22,47 +32,96 @@ Or install it yourself as:
|
|
|
22
32
|
|
|
23
33
|
## Usage
|
|
24
34
|
|
|
25
|
-
|
|
35
|
+
The module methods return the schedule as a `Hash` of subject name to an
|
|
36
|
+
`Array` of sessions:
|
|
26
37
|
|
|
27
|
-
### DCB Asesorias
|
|
28
38
|
```ruby
|
|
29
|
-
|
|
39
|
+
require "leonardita"
|
|
40
|
+
|
|
41
|
+
Leonardita.mentoring # DCB asesorías
|
|
42
|
+
Leonardita.workshops # DCB talleres
|
|
43
|
+
# => {
|
|
44
|
+
# "ÁLGEBRA (1120)" => [
|
|
45
|
+
# {
|
|
46
|
+
# name: "D.I. SOFÍA MAGDALENA ÁVILA BECERRIL",
|
|
47
|
+
# start_at: "16:00",
|
|
48
|
+
# end_at: "18:00",
|
|
49
|
+
# days: ["thursday"],
|
|
50
|
+
# classroom: "En línea",
|
|
51
|
+
# form_url: "https://forms.gle/...",
|
|
52
|
+
# access_data: "https://cuaed-unam-mx.zoom.us/j/...\nID de reunión: ...",
|
|
53
|
+
# url: "https://cuaed-unam-mx.zoom.us/j/..."
|
|
54
|
+
# },
|
|
55
|
+
# ...
|
|
56
|
+
# ],
|
|
57
|
+
# ...
|
|
58
|
+
# }
|
|
30
59
|
```
|
|
31
60
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
61
|
+
| Key | Description |
|
|
62
|
+
| ------------- | ----------- |
|
|
63
|
+
| `name` | Mentor or instructor |
|
|
64
|
+
| `start_at`, `end_at` | Session time (`"HH:MM"`) |
|
|
65
|
+
| `days` | Weekdays in English, from `"monday"` to `"saturday"` |
|
|
66
|
+
| `classroom` | Room, or `"En línea"` for online sessions |
|
|
67
|
+
| `form_url` | Registration form, `nil` when there is none |
|
|
68
|
+
| `access_data` | Access instructions for online sessions (multi-line), `nil` otherwise |
|
|
69
|
+
| `url` | First URL found in `access_data` |
|
|
36
70
|
|
|
37
|
-
|
|
71
|
+
Results are memoized for the life of the process. Pass `reload: true` to fetch
|
|
72
|
+
them again:
|
|
38
73
|
|
|
39
|
-
### DCB Asesorias
|
|
40
74
|
```ruby
|
|
41
|
-
|
|
42
|
-
scrapper.raw_data
|
|
43
|
-
scrapper.data
|
|
75
|
+
Leonardita.mentoring(reload: true)
|
|
44
76
|
```
|
|
45
77
|
|
|
46
|
-
|
|
78
|
+
You can also use the scrapers directly:
|
|
79
|
+
|
|
47
80
|
```ruby
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
81
|
+
scraper = Leonardita::Dcb::Mentoring.new # or Leonardita::Dcb::Workshops.new
|
|
82
|
+
scraper.raw_data # HTML source
|
|
83
|
+
scraper.data # parsed schedule
|
|
84
|
+
|
|
85
|
+
# Parse HTML you already have (for example, a saved copy of the page)
|
|
86
|
+
scraper = Leonardita::Dcb::Workshops.new
|
|
87
|
+
scraper.raw_data = File.read("talleres.html")
|
|
88
|
+
scraper.data
|
|
51
89
|
```
|
|
90
|
+
|
|
91
|
+
Pages that cannot be fetched raise `Leonardita::HTTPError` (a subclass of
|
|
92
|
+
`Leonardita::Error`), which exposes the HTTP `status`.
|
|
93
|
+
|
|
52
94
|
## Development
|
|
53
95
|
|
|
54
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then
|
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then run
|
|
97
|
+
`bundle exec rake` to run the tests and RuboCop. You can also run `bin/console`
|
|
98
|
+
for an interactive prompt.
|
|
99
|
+
|
|
100
|
+
Integration tests replay VCR cassettes stored in `test/fixtures/vcr_cassettes`.
|
|
101
|
+
Delete a cassette to re-record it against the live site. Parser edge cases are
|
|
102
|
+
covered with the HTML fixture in `test/fixtures/html`.
|
|
55
103
|
|
|
56
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
104
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
105
|
+
release a new version, update the version number in
|
|
106
|
+
`lib/leonardita/version.rb` and the `CHANGELOG.md`, then run
|
|
107
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
|
108
|
+
git commits and the created tag, and push the `.gem` file to
|
|
109
|
+
[rubygems.org](https://rubygems.org).
|
|
57
110
|
|
|
58
111
|
## Contributing
|
|
59
112
|
|
|
60
|
-
Bug reports and pull requests are welcome on GitHub at
|
|
113
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
114
|
+
https://github.com/3zcurdia/leonardita. This project is intended to be a safe,
|
|
115
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
|
116
|
+
the [code of conduct](https://github.com/3zcurdia/leonardita/blob/main/CODE_OF_CONDUCT.md).
|
|
61
117
|
|
|
62
118
|
## License
|
|
63
119
|
|
|
64
|
-
The gem is available as open source under the terms of the
|
|
120
|
+
The gem is available as open source under the terms of the
|
|
121
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
|
65
122
|
|
|
66
123
|
## Code of Conduct
|
|
67
124
|
|
|
68
|
-
Everyone interacting in the Leonardita project's codebases, issue trackers,
|
|
125
|
+
Everyone interacting in the Leonardita project's codebases, issue trackers,
|
|
126
|
+
chat rooms and mailing lists is expected to follow the
|
|
127
|
+
[code of conduct](https://github.com/3zcurdia/leonardita/blob/main/CODE_OF_CONDUCT.md).
|
data/leonardita.gemspec
CHANGED
|
@@ -3,35 +3,33 @@
|
|
|
3
3
|
require_relative "lib/leonardita/version"
|
|
4
4
|
|
|
5
5
|
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name
|
|
7
|
-
spec.version
|
|
8
|
-
spec.authors
|
|
9
|
-
spec.email
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
spec.
|
|
13
|
-
spec.
|
|
14
|
-
|
|
15
|
-
spec.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
spec.metadata["
|
|
20
|
-
spec.metadata["
|
|
21
|
-
spec.metadata["
|
|
6
|
+
spec.name = "leonardita"
|
|
7
|
+
spec.version = Leonardita::VERSION
|
|
8
|
+
spec.authors = ["Luis Ezcurdia Razo"]
|
|
9
|
+
spec.email = ["ing.ezcurdia@gmail.com"]
|
|
10
|
+
spec.license = "MIT"
|
|
11
|
+
|
|
12
|
+
spec.summary = "FI-UNAM web scrapers and tools"
|
|
13
|
+
spec.description = "Scrapers for the Engineering Faculty (FI) of UNAM websites, such as the DCB " \
|
|
14
|
+
"mentoring (asesorías) and workshop (talleres) schedules."
|
|
15
|
+
spec.homepage = "https://github.com/3zcurdia/leonardita"
|
|
16
|
+
spec.required_ruby_version = ">= 3.3"
|
|
17
|
+
|
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
20
|
+
spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
|
|
21
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
22
22
|
|
|
23
23
|
# Specify which files should be added to the gem when it is released.
|
|
24
24
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
25
|
-
spec.files = Dir.chdir(
|
|
26
|
-
`git ls-files -z`.split("\x0").reject
|
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
27
|
+
f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|github|rubocop)|Gemfile|Rakefile|mise\.toml|AGENTS\.md)})
|
|
28
|
+
end
|
|
27
29
|
end
|
|
28
|
-
spec.bindir
|
|
29
|
-
spec.executables
|
|
30
|
+
spec.bindir = "exe"
|
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
30
32
|
spec.require_paths = ["lib"]
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
spec.add_dependency "nokogiri", "~> 1.11"
|
|
34
|
-
|
|
35
|
-
# For more information and examples about making a new gem, checkout our
|
|
36
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
|
34
|
+
spec.add_dependency "nokogiri", "~> 1.16"
|
|
37
35
|
end
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Leonardita
|
|
4
4
|
module Dcb
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
# Mentoring (asesorías) schedule of the Division of Basic Sciences (DCB).
|
|
6
|
+
class Mentoring < ScheduleScraper
|
|
7
|
+
URL = "https://dcb.ingenieria.unam.mx/asesorias/"
|
|
8
|
+
|
|
9
|
+
def initialize(url = URL)
|
|
10
|
+
super
|
|
8
11
|
end
|
|
9
12
|
end
|
|
10
13
|
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Leonardita
|
|
4
|
+
module Dcb
|
|
5
|
+
# Parses the DCB schedule pages, where every subject is a red heading
|
|
6
|
+
# paragraph followed by a table with one row per session.
|
|
7
|
+
class ScheduleScraper < Scraper
|
|
8
|
+
WEEKDAYS = {
|
|
9
|
+
"lu" => "monday",
|
|
10
|
+
"ma" => "tuesday",
|
|
11
|
+
"mi" => "wednesday",
|
|
12
|
+
"ju" => "thursday",
|
|
13
|
+
"vi" => "friday",
|
|
14
|
+
"sa" => "saturday"
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
COLUMNS = {
|
|
18
|
+
"asesor" => :name,
|
|
19
|
+
"inicia" => :start_at,
|
|
20
|
+
"termina" => :end_at,
|
|
21
|
+
"lugar" => :classroom,
|
|
22
|
+
"formulario registro" => :form_url,
|
|
23
|
+
"info. acceso" => :access_data
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
ROW_TEMPLATE = {
|
|
27
|
+
name: nil, start_at: nil, end_at: nil, days: [].freeze, classroom: nil,
|
|
28
|
+
form_url: nil, access_data: nil, url: nil
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
URL_REGEX = %r{https?://[^\s<>'"]+}
|
|
32
|
+
ACCESS_DATA_REGEX = /mostrar\('(?<data>.*?)'\)/m
|
|
33
|
+
|
|
34
|
+
# Returns a Hash of subject name => Array of session Hashes.
|
|
35
|
+
def data
|
|
36
|
+
@data ||= build_data
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def build_data
|
|
42
|
+
document.css("table").each_with_object({}) do |table, acc|
|
|
43
|
+
subject = subject_for(table)
|
|
44
|
+
next unless subject
|
|
45
|
+
|
|
46
|
+
(acc[subject] ||= []).concat(scrape_table(table))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def subject_for(table)
|
|
51
|
+
heading = table.previous_element
|
|
52
|
+
return unless heading&.name == "p"
|
|
53
|
+
|
|
54
|
+
text = clean(heading.text)
|
|
55
|
+
text unless text.empty?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def scrape_table(table)
|
|
59
|
+
columns = table.css("thead th").map { |th| clean(th.text).downcase }
|
|
60
|
+
table.css("tbody tr").map { |row| scrape_row(columns, row.css("td")) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def scrape_row(columns, cells)
|
|
64
|
+
row = ROW_TEMPLATE.merge(days: [])
|
|
65
|
+
columns.each_with_index do |column, index|
|
|
66
|
+
assign(row, column, cells[index])
|
|
67
|
+
end
|
|
68
|
+
row[:url] = row[:access_data]&.[](URL_REGEX)
|
|
69
|
+
row
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def assign(row, column, cell)
|
|
73
|
+
return unless cell
|
|
74
|
+
|
|
75
|
+
if WEEKDAYS.key?(column)
|
|
76
|
+
row[:days] << WEEKDAYS[column] unless clean(cell.text).empty?
|
|
77
|
+
elsif (key = COLUMNS[column])
|
|
78
|
+
row[key] = cell_value(key, cell)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def cell_value(key, cell)
|
|
83
|
+
case key
|
|
84
|
+
when :form_url then presence(cell.at_css("a")&.[]("href"))
|
|
85
|
+
when :access_data then access_data(cell)
|
|
86
|
+
else clean(cell.text)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def access_data(cell)
|
|
91
|
+
onclick = cell.at_css("[onclick]")&.[]("onclick")
|
|
92
|
+
match = onclick&.match(ACCESS_DATA_REGEX)
|
|
93
|
+
return presence(clean(cell.text)) unless match
|
|
94
|
+
|
|
95
|
+
match[:data].split(%r{<br\s*/?>}i).map { |line| clean(line) }.reject(&:empty?).join("\n")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def clean(text)
|
|
99
|
+
text.to_s.tr(" ", " ").squeeze(" ").strip
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def presence(value)
|
|
103
|
+
value = value.to_s.strip
|
|
104
|
+
value.empty? ? nil : value
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Leonardita
|
|
4
4
|
module Dcb
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
# Workshops (talleres) schedule of the Division of Basic Sciences (DCB).
|
|
6
|
+
class Workshops < ScheduleScraper
|
|
7
|
+
URL = "https://dcb.ingenieria.unam.mx/talleres/"
|
|
8
|
+
|
|
9
|
+
def initialize(url = URL)
|
|
10
|
+
super
|
|
8
11
|
end
|
|
9
12
|
end
|
|
10
13
|
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "nokogiri"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Leonardita
|
|
8
|
+
# Fetches an HTML page (following redirects) and exposes it as a Nokogiri document.
|
|
9
|
+
class Scraper
|
|
10
|
+
MAX_REDIRECTS = 5
|
|
11
|
+
OPEN_TIMEOUT = 10
|
|
12
|
+
READ_TIMEOUT = 30
|
|
13
|
+
|
|
14
|
+
attr_writer :raw_data
|
|
15
|
+
attr_reader :uri
|
|
16
|
+
|
|
17
|
+
def initialize(url)
|
|
18
|
+
@uri = URI.parse(url)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def raw_data
|
|
22
|
+
@raw_data ||= fetch(uri)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
protected
|
|
26
|
+
|
|
27
|
+
def document
|
|
28
|
+
@document ||= Nokogiri::HTML(raw_data, nil, "UTF-8")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def fetch(location, redirects_left = MAX_REDIRECTS)
|
|
34
|
+
response = get(location)
|
|
35
|
+
case response
|
|
36
|
+
when Net::HTTPSuccess then response.body.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
37
|
+
when Net::HTTPRedirection then follow(location, response, redirects_left)
|
|
38
|
+
else raise HTTPError.new("GET #{location} responded with HTTP #{response.code}", status: response.code)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def follow(location, response, redirects_left)
|
|
43
|
+
raise HTTPError.new("Too many redirects fetching #{uri}", status: response.code) if redirects_left.zero?
|
|
44
|
+
|
|
45
|
+
fetch(location + response["location"], redirects_left - 1)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def get(location)
|
|
49
|
+
Net::HTTP.start(location.host, location.port, use_ssl: location.scheme == "https",
|
|
50
|
+
open_timeout: OPEN_TIMEOUT, read_timeout: READ_TIMEOUT) do |http|
|
|
51
|
+
request = Net::HTTP::Get.new(location)
|
|
52
|
+
request["User-Agent"] = "leonardita/#{VERSION} (+https://github.com/3zcurdia/leonardita)"
|
|
53
|
+
http.request(request)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/leonardita/version.rb
CHANGED
data/lib/leonardita.rb
CHANGED
|
@@ -1,20 +1,46 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "leonardita/version"
|
|
4
|
-
require_relative "leonardita/
|
|
5
|
-
require_relative "leonardita/dcb/
|
|
4
|
+
require_relative "leonardita/scraper"
|
|
5
|
+
require_relative "leonardita/dcb/schedule_scraper"
|
|
6
6
|
require_relative "leonardita/dcb/mentoring"
|
|
7
7
|
require_relative "leonardita/dcb/workshops"
|
|
8
8
|
|
|
9
|
+
# Scrapers for the Engineering Faculty (FI) of UNAM websites.
|
|
9
10
|
module Leonardita
|
|
10
11
|
class Error < StandardError; end
|
|
11
|
-
# Your code goes here...
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
# Raised when a page cannot be fetched (non-2xx status or too many redirects).
|
|
14
|
+
class HTTPError < Error
|
|
15
|
+
attr_reader :status
|
|
16
|
+
|
|
17
|
+
def initialize(message, status: nil)
|
|
18
|
+
@status = status&.to_i
|
|
19
|
+
super(message)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @deprecated Use Leonardita::Scraper
|
|
24
|
+
Scrapper = Scraper
|
|
25
|
+
deprecate_constant :Scrapper
|
|
26
|
+
|
|
27
|
+
module Dcb
|
|
28
|
+
# @deprecated Use Leonardita::Dcb::ScheduleScraper
|
|
29
|
+
ScheduleScrapper = ScheduleScraper
|
|
30
|
+
deprecate_constant :ScheduleScrapper
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# DCB mentoring (asesorías) schedule, grouped by subject. Memoized; pass
|
|
34
|
+
# `reload: true` to fetch it again.
|
|
35
|
+
def self.mentoring(reload: false)
|
|
36
|
+
@mentoring = nil if reload
|
|
14
37
|
@mentoring ||= Dcb::Mentoring.new.data
|
|
15
38
|
end
|
|
16
39
|
|
|
17
|
-
|
|
40
|
+
# DCB workshops (talleres) schedule, grouped by subject. Memoized; pass
|
|
41
|
+
# `reload: true` to fetch it again.
|
|
42
|
+
def self.workshops(reload: false)
|
|
43
|
+
@workshops = nil if reload
|
|
18
44
|
@workshops ||= Dcb::Workshops.new.data
|
|
19
45
|
end
|
|
20
46
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: leonardita
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Luis Ezcurdia
|
|
8
|
-
autorequire:
|
|
7
|
+
- Luis Ezcurdia Razo
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: nokogiri
|
|
@@ -16,49 +15,41 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '1.
|
|
18
|
+
version: '1.16'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '1.
|
|
27
|
-
description: FI
|
|
25
|
+
version: '1.16'
|
|
26
|
+
description: Scrapers for the Engineering Faculty (FI) of UNAM websites, such as the
|
|
27
|
+
DCB mentoring (asesorías) and workshop (talleres) schedules.
|
|
28
28
|
email:
|
|
29
29
|
- ing.ezcurdia@gmail.com
|
|
30
30
|
executables: []
|
|
31
31
|
extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
|
33
33
|
files:
|
|
34
|
-
- ".github/workflows/main.yml"
|
|
35
|
-
- ".gitignore"
|
|
36
|
-
- ".rubocop.yml"
|
|
37
|
-
- ".ruby-version"
|
|
38
34
|
- CHANGELOG.md
|
|
39
35
|
- CODE_OF_CONDUCT.md
|
|
40
|
-
- Gemfile
|
|
41
|
-
- Gemfile.lock
|
|
42
36
|
- LICENSE.txt
|
|
43
37
|
- README.md
|
|
44
|
-
- Rakefile
|
|
45
|
-
- bin/console
|
|
46
|
-
- bin/setup
|
|
47
38
|
- leonardita.gemspec
|
|
48
39
|
- lib/leonardita.rb
|
|
49
40
|
- lib/leonardita/dcb/mentoring.rb
|
|
50
|
-
- lib/leonardita/dcb/
|
|
41
|
+
- lib/leonardita/dcb/schedule_scraper.rb
|
|
51
42
|
- lib/leonardita/dcb/workshops.rb
|
|
52
|
-
- lib/leonardita/
|
|
43
|
+
- lib/leonardita/scraper.rb
|
|
53
44
|
- lib/leonardita/version.rb
|
|
54
|
-
homepage: https://github.com/
|
|
45
|
+
homepage: https://github.com/3zcurdia/leonardita
|
|
55
46
|
licenses:
|
|
56
47
|
- MIT
|
|
57
48
|
metadata:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
source_code_uri: https://github.com/3zcurdia/leonardita
|
|
50
|
+
changelog_uri: https://github.com/3zcurdia/leonardita/blob/main/CHANGELOG.md
|
|
51
|
+
bug_tracker_uri: https://github.com/3zcurdia/leonardita/issues
|
|
52
|
+
rubygems_mfa_required: 'true'
|
|
62
53
|
rdoc_options: []
|
|
63
54
|
require_paths:
|
|
64
55
|
- lib
|
|
@@ -66,15 +57,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
66
57
|
requirements:
|
|
67
58
|
- - ">="
|
|
68
59
|
- !ruby/object:Gem::Version
|
|
69
|
-
version:
|
|
60
|
+
version: '3.3'
|
|
70
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
62
|
requirements:
|
|
72
63
|
- - ">="
|
|
73
64
|
- !ruby/object:Gem::Version
|
|
74
65
|
version: '0'
|
|
75
66
|
requirements: []
|
|
76
|
-
rubygems_version:
|
|
77
|
-
signing_key:
|
|
67
|
+
rubygems_version: 4.0.20
|
|
78
68
|
specification_version: 4
|
|
79
|
-
summary: FI-UNAM
|
|
69
|
+
summary: FI-UNAM web scrapers and tools
|
|
80
70
|
test_files: []
|
data/.github/workflows/main.yml
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
name: Ruby
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches: [ master ]
|
|
5
|
-
pull_request:
|
|
6
|
-
branches: [ master ]
|
|
7
|
-
|
|
8
|
-
jobs:
|
|
9
|
-
test:
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
strategy:
|
|
12
|
-
matrix:
|
|
13
|
-
ruby-version: ['2.7', '3.0']
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v2
|
|
16
|
-
- name: Set up Ruby
|
|
17
|
-
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
|
18
|
-
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
|
19
|
-
# uses: ruby/setup-ruby@v1
|
|
20
|
-
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
|
21
|
-
with:
|
|
22
|
-
ruby-version: ${{ matrix.ruby-version }}
|
|
23
|
-
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
24
|
-
- name: Run tests
|
|
25
|
-
run: bundle exec rake
|
data/.gitignore
DELETED
data/.rubocop.yml
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
AllCops:
|
|
2
|
-
NewCops: enable
|
|
3
|
-
SuggestExtensions: false
|
|
4
|
-
TargetRubyVersion: 2.7
|
|
5
|
-
|
|
6
|
-
Style/StringLiterals:
|
|
7
|
-
Enabled: true
|
|
8
|
-
EnforcedStyle: double_quotes
|
|
9
|
-
|
|
10
|
-
Style/StringLiteralsInInterpolation:
|
|
11
|
-
Enabled: true
|
|
12
|
-
EnforcedStyle: double_quotes
|
|
13
|
-
|
|
14
|
-
Layout/LineLength:
|
|
15
|
-
Max: 120
|
|
16
|
-
|
|
17
|
-
Metrics/AbcSize:
|
|
18
|
-
Max: 22
|
|
19
|
-
|
|
20
|
-
Metrics/MethodLength:
|
|
21
|
-
Max: 15
|
|
22
|
-
Exclude:
|
|
23
|
-
- test/leonardita/dcb/mentoring_test.rb
|
|
24
|
-
|
|
25
|
-
Style/Documentation:
|
|
26
|
-
Enabled: false
|
data/.ruby-version
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
2.7.2
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
leonardita (0.1.0)
|
|
5
|
-
nokogiri (~> 1.11)
|
|
6
|
-
|
|
7
|
-
GEM
|
|
8
|
-
remote: https://rubygems.org/
|
|
9
|
-
specs:
|
|
10
|
-
addressable (2.7.0)
|
|
11
|
-
public_suffix (>= 2.0.2, < 5.0)
|
|
12
|
-
ast (2.4.2)
|
|
13
|
-
crack (0.4.5)
|
|
14
|
-
rexml
|
|
15
|
-
hashdiff (1.0.1)
|
|
16
|
-
mini_portile2 (2.5.3)
|
|
17
|
-
minitest (5.14.4)
|
|
18
|
-
nokogiri (1.11.7)
|
|
19
|
-
mini_portile2 (~> 2.5.0)
|
|
20
|
-
racc (~> 1.4)
|
|
21
|
-
nokogiri (1.11.7-x86_64-darwin)
|
|
22
|
-
racc (~> 1.4)
|
|
23
|
-
nokogiri (1.11.7-x86_64-linux)
|
|
24
|
-
racc (~> 1.4)
|
|
25
|
-
parallel (1.20.1)
|
|
26
|
-
parser (3.0.1.1)
|
|
27
|
-
ast (~> 2.4.1)
|
|
28
|
-
public_suffix (4.0.6)
|
|
29
|
-
racc (1.5.2)
|
|
30
|
-
rainbow (3.0.0)
|
|
31
|
-
rake (13.0.3)
|
|
32
|
-
regexp_parser (2.1.1)
|
|
33
|
-
rexml (3.2.5)
|
|
34
|
-
rubocop (1.17.0)
|
|
35
|
-
parallel (~> 1.10)
|
|
36
|
-
parser (>= 3.0.0.0)
|
|
37
|
-
rainbow (>= 2.2.2, < 4.0)
|
|
38
|
-
regexp_parser (>= 1.8, < 3.0)
|
|
39
|
-
rexml
|
|
40
|
-
rubocop-ast (>= 1.7.0, < 2.0)
|
|
41
|
-
ruby-progressbar (~> 1.7)
|
|
42
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
|
43
|
-
rubocop-ast (1.7.0)
|
|
44
|
-
parser (>= 3.0.1.1)
|
|
45
|
-
ruby-progressbar (1.11.0)
|
|
46
|
-
unicode-display_width (2.0.0)
|
|
47
|
-
vcr (6.0.0)
|
|
48
|
-
webmock (3.13.0)
|
|
49
|
-
addressable (>= 2.3.6)
|
|
50
|
-
crack (>= 0.3.2)
|
|
51
|
-
hashdiff (>= 0.4.0, < 2.0.0)
|
|
52
|
-
|
|
53
|
-
PLATFORMS
|
|
54
|
-
ruby
|
|
55
|
-
x86_64-darwin-20
|
|
56
|
-
x86_64-linux
|
|
57
|
-
|
|
58
|
-
DEPENDENCIES
|
|
59
|
-
leonardita!
|
|
60
|
-
minitest (~> 5.0)
|
|
61
|
-
rake (~> 13.0)
|
|
62
|
-
rubocop (~> 1.7)
|
|
63
|
-
vcr
|
|
64
|
-
webmock
|
|
65
|
-
|
|
66
|
-
BUNDLED WITH
|
|
67
|
-
2.2.21
|
data/Rakefile
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/gem_tasks"
|
|
4
|
-
require "rake/testtask"
|
|
5
|
-
|
|
6
|
-
Rake::TestTask.new(:test) do |t|
|
|
7
|
-
t.libs << "test"
|
|
8
|
-
t.libs << "lib"
|
|
9
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
require "rubocop/rake_task"
|
|
13
|
-
|
|
14
|
-
RuboCop::RakeTask.new
|
|
15
|
-
|
|
16
|
-
task default: %i[test rubocop]
|
data/bin/console
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
require "bundler/setup"
|
|
5
|
-
require "leonardita"
|
|
6
|
-
|
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
-
|
|
10
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
-
# require "pry"
|
|
12
|
-
# Pry.start
|
|
13
|
-
|
|
14
|
-
require "irb"
|
|
15
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Leonardita
|
|
4
|
-
module Dcb
|
|
5
|
-
class ScheduleScrapper < Scrapper
|
|
6
|
-
WEEKDAYS = %w[monday tuesday wednesday thursday friday].freeze
|
|
7
|
-
|
|
8
|
-
def data
|
|
9
|
-
@data ||= build_data
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
private
|
|
13
|
-
|
|
14
|
-
def build_data
|
|
15
|
-
document.css("#program").children.each_with_object({}) do |node, acc|
|
|
16
|
-
node_name = node.name
|
|
17
|
-
node_data = node&.next&.next
|
|
18
|
-
acc[node.text] = scrape_table(node_data) if node_name == "h3" && node_data&.name == "table"
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def scrape_table(node)
|
|
23
|
-
node.css("tbody tr").each_with_object([]) do |row, acc|
|
|
24
|
-
acc << scrape_table_row(row)
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def scrape_table_row(row)
|
|
29
|
-
values = row_values(row)
|
|
30
|
-
{
|
|
31
|
-
name: values[0],
|
|
32
|
-
start_at: values[1],
|
|
33
|
-
end_at: values[2],
|
|
34
|
-
days: values[3..7].map(&:empty?).zip(WEEKDAYS).reject(&:first).map(&:last),
|
|
35
|
-
classroom: values[8],
|
|
36
|
-
form_url: row.css("td a").attribute("href").value,
|
|
37
|
-
access_data: values[10],
|
|
38
|
-
url: values[10].scan(url_regex).first
|
|
39
|
-
}
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def row_values(row)
|
|
43
|
-
row.css("td").map(&:text).map { |word| word.scan(safe_row_regex).join(" ") }
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def safe_row_regex
|
|
47
|
-
@safe_row_regex ||= %r{[[:alnum:]/.\-?:]+}
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def url_regex
|
|
51
|
-
@url_regex ||= %r{https?://[[:alnum:]./?\-]+}
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
data/lib/leonardita/scrapper.rb
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "net/http"
|
|
4
|
-
require "nokogiri"
|
|
5
|
-
|
|
6
|
-
module Leonardita
|
|
7
|
-
class Scrapper
|
|
8
|
-
attr_writer :raw_data
|
|
9
|
-
attr_reader :uri
|
|
10
|
-
|
|
11
|
-
def initialize(url)
|
|
12
|
-
@uri = URI.parse(url)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def raw_data
|
|
16
|
-
@raw_data ||= response.body
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
protected
|
|
20
|
-
|
|
21
|
-
def document
|
|
22
|
-
@document ||= Nokogiri::HTML(raw_data)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def response
|
|
26
|
-
@response ||= Net::HTTP.get_response(uri)
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|