-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #881 from DaanVanVugt/feature/researchsoftwaretrai…
…ning basic scraper for materials of researchsoftwaretraining
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'open-uri' | ||
require 'csv' | ||
require 'nokogiri' | ||
|
||
module Ingestors | ||
class RstIngestor < Ingestor | ||
def self.config | ||
{ | ||
key: 'rst_material', | ||
title: 'RST Events API', | ||
category: :materials | ||
} | ||
end | ||
|
||
def read(url) | ||
begin | ||
process_rst(url) | ||
rescue Exception => e | ||
@messages << "#{self.class.name} failed with: #{e.message}" | ||
end | ||
|
||
# finished | ||
nil | ||
end | ||
|
||
private | ||
|
||
def process_rst(url) | ||
rst_url = 'https://researchsoftwaretraining.nl/resources/' | ||
material_page = Nokogiri::HTML5.parse(open_url(rst_url.to_s, raise: true)).css("div[class='inner_content cf']").first.css('p > a') | ||
material_page.each_with_index do |el, idx| | ||
material = OpenStruct.new | ||
material.title = el&.text | ||
material.url = el&.get_attribute('href') | ||
material.description = material.title | ||
add_material(material) | ||
rescue Exception => e | ||
@messages << "Extract event fields failed with: #{e.message}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'test_helper' | ||
|
||
class RstIngestorTest < ActiveSupport::TestCase | ||
setup do | ||
@user = users(:regular_user) | ||
@content_provider = content_providers(:portal_provider) | ||
mock_ingestions | ||
mock_timezone # System time zone should not affect test result | ||
end | ||
|
||
teardown do | ||
reset_timezone | ||
end | ||
|
||
test 'can ingest materials from rst' do | ||
source = @content_provider.sources.build( | ||
url: 'https://researchsoftwaretraining.nl/resources/', | ||
method: 'rst', | ||
enabled: true | ||
) | ||
|
||
ingestor = Ingestors::RstIngestor.new | ||
|
||
# check materials don't exist | ||
new_title = 'Software Carpentry' | ||
new_url = 'https://software-carpentry.org/lessons/' | ||
refute Material.where(title: new_title, url: new_url).any? | ||
|
||
# run task | ||
assert_difference('Material.count', 24) do | ||
freeze_time(2019) do | ||
VCR.use_cassette("ingestors/rst") do | ||
ingestor.read(source.url) | ||
ingestor.write(@user, @content_provider) | ||
end | ||
end | ||
end | ||
|
||
# check event does exist | ||
material = Material.where(title: new_title, url: new_url).first | ||
assert material | ||
assert_equal new_title, material.title | ||
assert_equal new_url, material.url | ||
|
||
# check other fields | ||
assert_equal 'Software Carpentry', material.description | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.