nytimes-top-stories 0.1.2 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/bin/nytimes-top-stories +1 -0
- data/config/environment.rb +4 -2
- data/lib/cli.rb +39 -0
- data/lib/scraper.rb +37 -0
- data/lib/story.rb +43 -0
- data/lib/version.rb +3 -0
- data/nytimes-top-stories-0.1.3.gem +0 -0
- data/nytimes-top-stories-0.1.4.gem +0 -0
- data/nytimes-top-stories-0.1.5.gem +0 -0
- data/nytimes-top-stories.gemspec +4 -5
- metadata +18 -15
- data/lib/nytimes_top_stories.rb +0 -1
- data/lib/nytimes_top_stories/cli.rb +0 -37
- data/lib/nytimes_top_stories/scraper.rb +0 -35
- data/lib/nytimes_top_stories/story.rb +0 -41
- data/lib/nytimes_top_stories/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2fe7e9c69522d5a11f87a117ed82622915d4bc7
|
4
|
+
data.tar.gz: cca166b27322aace76b7605fa3df200234feb01c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fb6c7b3529dc7a3a40e4ec5a01eb3ec4b354c024b18f180b4857425ac0f7afd533597b1bf78a71d9da2f745888e9b851f87a0a990523521a949aa0b6f228e88
|
7
|
+
data.tar.gz: 0c49aa73dedc93bf4d6c5603c0aedf1c0529adbed0d703d9def2af24ed2fe8292fb94d1d7df06ade9891fa03029bc8cfbf952105517899d3979d257b6db15af4
|
data/Gemfile
CHANGED
data/bin/nytimes-top-stories
CHANGED
data/config/environment.rb
CHANGED
data/lib/cli.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class NytimesTopStories
|
2
|
+
class CLI
|
3
|
+
def call
|
4
|
+
puts NytimesTopStories::Scraper.get_date
|
5
|
+
puts "Headlines live from NYT:"
|
6
|
+
self.display_stories
|
7
|
+
self.menu
|
8
|
+
self.goodbye
|
9
|
+
end
|
10
|
+
|
11
|
+
def display_stories
|
12
|
+
NytimesTopStories::Story.new_from_array
|
13
|
+
NytimesTopStories::Story.all.each.with_index(1) {|story, i| puts "#{i}: #{story.headline}"}
|
14
|
+
end
|
15
|
+
|
16
|
+
def menu
|
17
|
+
choice = nil
|
18
|
+
until choice == "exit"
|
19
|
+
puts "Enter story number for more information, or type 'list'/'exit'"
|
20
|
+
choice = gets.strip.downcase
|
21
|
+
if choice.to_i > 0 && !NytimesTopStories::Story.all[choice.to_i-1].nil?
|
22
|
+
story = NytimesTopStories::Story.all[choice.to_i-1]
|
23
|
+
story.puts_story
|
24
|
+
elsif choice == "list"
|
25
|
+
self.call
|
26
|
+
elsif choice == "exit"
|
27
|
+
break
|
28
|
+
else
|
29
|
+
puts "Input invalid. Type 'list' or 'exit'."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def goodbye
|
35
|
+
puts "The truth is more important now than ever."
|
36
|
+
exit(0)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/scraper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'open_uri_redirections'
|
3
|
+
|
4
|
+
class NytimesTopStories
|
5
|
+
class Scraper
|
6
|
+
def self.open_site(site = "https://www.nytimes.com")
|
7
|
+
html = open(site, :allow_redirections => :safe)
|
8
|
+
doc = Nokogiri::HTML(html)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.get_top_stories(site = "https://www.nytimes.com")
|
12
|
+
doc = self.open_site(site)
|
13
|
+
top_stories_array = []
|
14
|
+
stories = doc.css("#top-news .collection .theme-summary")
|
15
|
+
stories.each do |story|
|
16
|
+
story_hash = {}
|
17
|
+
story_hash[:headline] = story.css(".story-heading a").text
|
18
|
+
story_hash[:byline] = story.css(".byline").text.strip
|
19
|
+
if !story.css("ul").empty?
|
20
|
+
story_hash[:summary] = story.css("ul").text.strip
|
21
|
+
else
|
22
|
+
story_hash[:summary] = story.css("p.summary").text.strip
|
23
|
+
end
|
24
|
+
if !story.at_css(".story-heading a").nil?
|
25
|
+
story_hash[:url] = story.css(".story-heading a").attr("href").value
|
26
|
+
end
|
27
|
+
top_stories_array << story_hash
|
28
|
+
end
|
29
|
+
top_stories_array
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.get_date(site = "http://nytimes.com")
|
33
|
+
doc = self.open_site(site)
|
34
|
+
doc.css(".masthead-menu .date").text
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/story.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
class NytimesTopStories
|
2
|
+
class Story
|
3
|
+
attr_accessor :headline, :byline, :summary, :url
|
4
|
+
@@all = []
|
5
|
+
def initialize(story_hash)
|
6
|
+
@headline = story_hash[:headline].gsub(/â/,"'")
|
7
|
+
@byline = story_hash[:byline]
|
8
|
+
@summary = story_hash[:summary].gsub(/â/,"'")
|
9
|
+
@url = story_hash[:url]
|
10
|
+
@@all << self
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.new_from_array(array = NytimesTopStories::Scraper.get_top_stories)
|
14
|
+
self.clear_all
|
15
|
+
array.each do |scraped_story|
|
16
|
+
story = NytimesTopStories::Story.new(scraped_story) unless scraped_story[:headline].empty? || scraped_story[:byline].empty? || scraped_story[:summary].empty? || scraped_story[:url].empty?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def puts_story
|
21
|
+
puts @headline
|
22
|
+
puts @byline
|
23
|
+
puts @summary
|
24
|
+
puts "Press enter to open, enter any other input to escape."
|
25
|
+
choice = gets.strip
|
26
|
+
if choice == ""
|
27
|
+
self.open_story
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def open_story
|
32
|
+
system("open #{@url}")
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.all
|
36
|
+
@@all
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.clear_all
|
40
|
+
@@all.clear
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/version.rb
ADDED
Binary file
|
Binary file
|
Binary file
|
data/nytimes-top-stories.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "./lib/
|
4
|
+
require "./lib/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "nytimes-top-stories"
|
@@ -25,14 +25,13 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
26
|
f.match(%r{^(test|spec|features)/})
|
27
27
|
end
|
28
|
-
spec.
|
29
|
-
spec.
|
30
|
-
spec.require_paths = ["lib"]
|
28
|
+
spec.executables = ["nytimes-top-stories"]
|
29
|
+
spec.require_paths = ["lib" "lib/nytimes_top_stories"]
|
31
30
|
|
32
31
|
spec.add_development_dependency "bundler", "~> 1.15"
|
33
32
|
spec.add_development_dependency "rake", "~> 10.0"
|
34
33
|
spec.add_development_dependency "rspec", "~> 3.0"
|
35
34
|
|
36
35
|
spec.add_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
|
37
|
-
spec.add_dependency'
|
36
|
+
spec.add_dependency'open_uri_redirections'
|
38
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nytimes-top-stories
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luisa Scavo
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,23 +73,24 @@ dependencies:
|
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: 1.6.8
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
76
|
+
name: open_uri_redirections
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- - "
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version: '
|
81
|
+
version: '0'
|
82
82
|
type: :runtime
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: '
|
88
|
+
version: '0'
|
89
89
|
description:
|
90
90
|
email:
|
91
91
|
- luisascavo@gmail.com
|
92
|
-
executables:
|
92
|
+
executables:
|
93
|
+
- nytimes-top-stories
|
93
94
|
extensions: []
|
94
95
|
extra_rdoc_files: []
|
95
96
|
files:
|
@@ -105,11 +106,13 @@ files:
|
|
105
106
|
- bin/setup
|
106
107
|
- config/environment.rb
|
107
108
|
- fixtures/nytindex.htm
|
108
|
-
- lib/
|
109
|
-
- lib/
|
110
|
-
- lib/
|
111
|
-
- lib/
|
112
|
-
-
|
109
|
+
- lib/cli.rb
|
110
|
+
- lib/scraper.rb
|
111
|
+
- lib/story.rb
|
112
|
+
- lib/version.rb
|
113
|
+
- nytimes-top-stories-0.1.3.gem
|
114
|
+
- nytimes-top-stories-0.1.4.gem
|
115
|
+
- nytimes-top-stories-0.1.5.gem
|
113
116
|
- nytimes-top-stories.gemspec
|
114
117
|
- spec.md
|
115
118
|
homepage: https://github.com/weezwo/nytimes-top-stories
|
@@ -119,7 +122,7 @@ metadata: {}
|
|
119
122
|
post_install_message:
|
120
123
|
rdoc_options: []
|
121
124
|
require_paths:
|
122
|
-
-
|
125
|
+
- liblib/nytimes_top_stories
|
123
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
127
|
requirements:
|
125
128
|
- - ">="
|
data/lib/nytimes_top_stories.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require_relative "../config/environment.rb"
|
@@ -1,37 +0,0 @@
|
|
1
|
-
class NytimesTopStories::CLI
|
2
|
-
def call
|
3
|
-
puts NytimesTopStories::Scraper.get_date
|
4
|
-
puts "Headlines live from NYT:"
|
5
|
-
self.display_stories
|
6
|
-
self.menu
|
7
|
-
self.goodbye
|
8
|
-
end
|
9
|
-
|
10
|
-
def display_stories
|
11
|
-
NytimesTopStories::Story.new_from_array
|
12
|
-
NytimesTopStories::Story.all.each.with_index(1) {|story, i| puts "#{i}: #{story.headline}"}
|
13
|
-
end
|
14
|
-
|
15
|
-
def menu
|
16
|
-
choice = nil
|
17
|
-
until choice == "exit"
|
18
|
-
puts "Enter story number for more information, or type 'list'/'exit'"
|
19
|
-
choice = gets.strip.downcase
|
20
|
-
if choice.to_i > 0 && !NytimesTopStories::Story.all[choice.to_i-1].nil?
|
21
|
-
story = NytimesTopStories::Story.all[choice.to_i-1]
|
22
|
-
story.puts_story
|
23
|
-
elsif choice == "list"
|
24
|
-
self.call
|
25
|
-
elsif choice == "exit"
|
26
|
-
break
|
27
|
-
else
|
28
|
-
puts "Input invalid. Type 'list' or 'exit'."
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def goodbye
|
34
|
-
puts "The truth is more important now than ever."
|
35
|
-
exit(0)
|
36
|
-
end
|
37
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
|
3
|
-
class NytimesTopStories::Scraper
|
4
|
-
|
5
|
-
def self.open_site(site = "https://www.nytimes.com")
|
6
|
-
html = open(site)
|
7
|
-
doc = Nokogiri::HTML(html)
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.get_top_stories(site = "https://www.nytimes.com")
|
11
|
-
doc = self.open_site(site)
|
12
|
-
top_stories_array = []
|
13
|
-
stories = doc.css("#top-news .collection .theme-summary")
|
14
|
-
stories.each do |story|
|
15
|
-
story_hash = {}
|
16
|
-
story_hash[:headline] = story.css(".story-heading a").text
|
17
|
-
story_hash[:byline] = story.css(".byline").text.strip
|
18
|
-
if !story.css("ul").empty?
|
19
|
-
story_hash[:summary] = story.css("ul").text.strip
|
20
|
-
else
|
21
|
-
story_hash[:summary] = story.css("p.summary").text.strip
|
22
|
-
end
|
23
|
-
if !story.at_css(".story-heading a").nil?
|
24
|
-
story_hash[:url] = story.css(".story-heading a").attr("href").value
|
25
|
-
end
|
26
|
-
top_stories_array << story_hash
|
27
|
-
end
|
28
|
-
top_stories_array
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.get_date(site = "http://nytimes.com")
|
32
|
-
doc = self.open_site(site)
|
33
|
-
doc.css(".masthead-menu .date").text
|
34
|
-
end
|
35
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
class NytimesTopStories::Story
|
2
|
-
attr_accessor :headline, :byline, :summary, :url
|
3
|
-
@@all = []
|
4
|
-
def initialize(story_hash)
|
5
|
-
@headline = story_hash[:headline].gsub(/â/,"'")
|
6
|
-
@byline = story_hash[:byline]
|
7
|
-
@summary = story_hash[:summary].gsub(/â/,"'")
|
8
|
-
@url = story_hash[:url]
|
9
|
-
@@all << self
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.new_from_array(array = NytimesTopStories::Scraper.get_top_stories)
|
13
|
-
self.clear_all
|
14
|
-
array.each do |scraped_story|
|
15
|
-
story = NytimesTopStories::Story.new(scraped_story) unless scraped_story[:headline].empty? || scraped_story[:byline].empty? || scraped_story[:summary].empty? || scraped_story[:url].empty?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def puts_story
|
20
|
-
puts @headline
|
21
|
-
puts @byline
|
22
|
-
puts @summary
|
23
|
-
puts "Press enter to open, enter any other input to escape."
|
24
|
-
choice = gets.strip
|
25
|
-
if choice == ""
|
26
|
-
self.open_story
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def open_story
|
31
|
-
system("open #{@url}")
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.all
|
35
|
-
@@all
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.clear_all
|
39
|
-
@@all.clear
|
40
|
-
end
|
41
|
-
end
|