5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/npr_cli_news_reader/scraper.rb', line 5
def self.scrape_articles_for_category(selected_category)
if NprCliNewsReader::Article.all.detect {|article| article.category == selected_category}
return
else
html = ""
if selected_category == "race & culture"
html = open("#{@@base_url}/sections/codeswitch")
else
html = open("#{@@base_url}/sections/#{selected_category}")
end
doc = Nokogiri::HTML(html)
articles = doc.css('article.item')
articles.each do |article|
article_attributes = {
category: selected_category.downcase,
title: article.css('h2.title a').text.strip,
teaser: article.css('p.teaser a').text.strip,
article_url: article.css('h2.title a').attr('href').to_s.strip
}
NprCliNewsReader::Article.new(article_attributes)
end
end
end
|