bored-wikipedia-explorer 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/run +1 -1
- data/lib/command_line_interface.rb +94 -33
- data/lib/portal.rb +1 -4
- data/lib/scraper.rb +11 -9
- data/lib/topic.rb +0 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61605819cb8c595b91dae3d3b164cb29ad3fc72fcb9e3eb520a60359dbf2dd54
|
4
|
+
data.tar.gz: 8c37ee1fd0b0760f91d4e1f1ec2f0e030de15615bf0e9ed1c9195a3f173ad675
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ec9fdabccc9ada10e59afe24a522dfc614aec564b16fb461eedea606d51712ee651e6563bee74d423fb63bdc2d38d78352d4316726c63dd826c941b0c5344a4
|
7
|
+
data.tar.gz: 6ee474808b3b1bb683b276da1655969a4de7dc157f9c2aecda96d6967be896a90fb9e3ec2973246e1e285c4133a516dd52edc97c9ce4acf2eca1379abcbaf6f7
|
data/bin/run
CHANGED
@@ -1,19 +1,49 @@
|
|
1
|
-
require_relative "../lib/topic.rb"
|
2
|
-
require "colorize"
|
3
|
-
require "launchy"
|
4
|
-
|
5
1
|
class CommandLineInterface
|
6
2
|
def self.run
|
7
3
|
puts "Welcome to Did-You-Know Wikipedia Edition!"
|
8
|
-
puts "
|
4
|
+
puts "To quit the Wikipedia explorer at anytime type 'exit'."
|
5
|
+
@status = "online"
|
9
6
|
generate_topic_list
|
10
|
-
|
7
|
+
start
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.start
|
11
|
+
puts "Please select a topic to be given a random Wikipedia Portal to read:"
|
12
|
+
while @status == "online"
|
13
|
+
get_inputs #starts cli flow
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.get_current_input
|
18
|
+
@current_input = gets.strip.upcase
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.quit?(option)
|
22
|
+
if option == "EXIT"
|
23
|
+
@status = "offline"
|
24
|
+
puts "Goodbye explorer."
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.generate_topic_list
|
31
|
+
@list = Scraper.all_topics
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.get_rand_url
|
35
|
+
# binding.pry
|
36
|
+
@randurl = Scraper.scrape_portals_page(@topic.name)
|
37
|
+
@portal = Portal.find_or_create_by_url(@randurl)
|
38
|
+
@selected = Scraper.get_portal_name(@randurl)
|
39
|
+
@portal.name = @selected
|
40
|
+
@portal.topic = @topic
|
41
|
+
@topic.portals << @portal
|
11
42
|
end
|
12
43
|
|
13
44
|
def self.get_inputs
|
14
45
|
display_all_topics #Displays all available main topics
|
15
|
-
|
16
|
-
get_choice #gets users main topic choice
|
46
|
+
get_topic_choice #gets users main topic choice
|
17
47
|
get_rand_url #gets random sub-topic and creates Portal objects
|
18
48
|
visit_portal #asks user if they want to visit the randomnly select sub-topic
|
19
49
|
end
|
@@ -26,41 +56,72 @@ class CommandLineInterface
|
|
26
56
|
}
|
27
57
|
end
|
28
58
|
|
29
|
-
def self.generate_topic_list
|
30
|
-
@list = []
|
31
|
-
Topic.all_topics_list.each{|item|
|
32
|
-
@list << item
|
33
|
-
}
|
34
|
-
end
|
35
59
|
|
36
|
-
def self.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
60
|
+
def self.get_topic_choice
|
61
|
+
puts "Select a number (1-12) to explore that topic"
|
62
|
+
get_current_input
|
63
|
+
# binding.pry
|
64
|
+
if !quit?(@current_input)
|
65
|
+
while !@current_input.to_i.between?(1,12)#ask_input != "1"
|
66
|
+
# self.send(__callee__)
|
67
|
+
puts "Invalid input. Please choose a number between 1 and 12."
|
68
|
+
get_topic_choice
|
69
|
+
end
|
70
|
+
@choice = @list[@current_input.to_i - 1]
|
71
|
+
@topic = Topic.find_or_create_by_name(@choice)
|
72
|
+
# binding.pry
|
73
|
+
else
|
74
|
+
quit?(@current_input)
|
75
|
+
end
|
47
76
|
end
|
48
77
|
|
49
78
|
def self.visit_portal
|
50
|
-
puts "We've selected " +
|
79
|
+
puts "We've selected " + @selected
|
51
80
|
+ " for you within the " + @choice +" topic you selected."
|
52
81
|
puts "Would you like to visit this page? (Y/N)"
|
53
|
-
|
54
|
-
|
55
|
-
|
82
|
+
visit_page?
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.visit_page?
|
86
|
+
#checks if user wants to visit the selected portal page
|
87
|
+
get_current_input
|
88
|
+
if !quit?(@current_input)
|
89
|
+
case @current_input
|
90
|
+
when "Y"
|
91
|
+
puts @randurl
|
92
|
+
Launchy.open(@randurl)
|
93
|
+
puts "Do you want to continue exploring? (Y/N)"
|
94
|
+
keep_exploring?
|
95
|
+
when "N"
|
96
|
+
explore_more
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.keep_exploring?
|
102
|
+
get_current_input
|
103
|
+
if @current_input == "Y"
|
104
|
+
start
|
105
|
+
elsif @current_input == "N"
|
106
|
+
quit?("EXIT")
|
56
107
|
else
|
57
|
-
puts "
|
58
|
-
|
59
|
-
|
108
|
+
puts "Please enter a valid command (Y/N) to keep exploring."
|
109
|
+
keep_exploring?
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.explore_more
|
114
|
+
puts "Either type 'reroll' to choose another page within the " + @choice + " topic you selected. Or select a new topic with 'new'."
|
115
|
+
get_current_input
|
116
|
+
if !quit?(@current_input)
|
117
|
+
if @current_input == "REROLL"
|
60
118
|
get_rand_url
|
61
119
|
visit_portal
|
62
|
-
elsif
|
120
|
+
elsif @current_input == "NEW"
|
63
121
|
get_inputs
|
122
|
+
else
|
123
|
+
puts "Invalid input please enter (reroll/new)."
|
124
|
+
explore_more
|
64
125
|
end
|
65
126
|
end
|
66
127
|
end
|
data/lib/portal.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require_relative "../lib/scraper.rb"
|
2
|
-
|
3
1
|
class Portal
|
4
2
|
attr_accessor :url, :topic, :name
|
5
3
|
@@all = []
|
@@ -18,8 +16,7 @@ class Portal
|
|
18
16
|
@portal = Portal.all.detect{|portal| url == portal.url}
|
19
17
|
else
|
20
18
|
@portal = Portal.new(url)
|
21
|
-
|
22
|
-
return @portal
|
19
|
+
@portal
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
data/lib/scraper.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'nokogiri'
|
3
|
-
require 'pry'
|
4
|
-
|
5
1
|
class Scraper
|
6
2
|
@@all_topics = []
|
7
3
|
|
@@ -15,6 +11,7 @@ class Scraper
|
|
15
11
|
config.noblanks
|
16
12
|
end
|
17
13
|
|
14
|
+
puts "***Scraping Portals Page"
|
18
15
|
#set portals-container class for all portal links for each topic
|
19
16
|
#Thus there are 12 portal links containers but we're skipping the first one
|
20
17
|
doc.search("div").each{|anchor|
|
@@ -27,11 +24,11 @@ class Scraper
|
|
27
24
|
randval = Random.new
|
28
25
|
randnum = randval.rand(doc.search(".portals-container")[choice_index].search("a").count{|i| i.attribute("href").value.include?("/wiki/Portal:")})
|
29
26
|
randportal = doc.search(".portals-container")[choice_index].search("a")[randnum].attribute("href").value.prepend("https://en.wikipedia.org")
|
30
|
-
|
31
|
-
|
27
|
+
randportal
|
32
28
|
end
|
33
|
-
|
29
|
+
##################################
|
34
30
|
#Scrapes all main topics from all portals main page
|
31
|
+
#creates all topic instances
|
35
32
|
def self.all_topics
|
36
33
|
html = open("https://en.wikipedia.org/wiki/Portal:Contents/Portals")
|
37
34
|
doc = Nokogiri::HTML(html) do |config|
|
@@ -56,15 +53,20 @@ class Scraper
|
|
56
53
|
copy.slice!(-3..-1)
|
57
54
|
@@all_topics << copy
|
58
55
|
}
|
59
|
-
|
56
|
+
|
57
|
+
@@all_topics.each{|item|
|
58
|
+
Topic.new(item)
|
59
|
+
}
|
60
|
+
@@all_topics
|
60
61
|
end
|
61
62
|
|
62
63
|
def self.get_portal_name(url)
|
63
64
|
html = open(url)
|
65
|
+
puts "***Scraping Portal Name"
|
64
66
|
doc = Nokogiri::HTML(html) do |config|
|
65
67
|
config.noblanks
|
66
68
|
end
|
67
|
-
|
69
|
+
doc.search("title").text
|
68
70
|
end
|
69
71
|
|
70
72
|
end
|
data/lib/topic.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require_relative "../lib/scraper.rb"
|
2
|
-
require_relative "../lib/portal.rb"
|
3
|
-
|
4
1
|
class Topic
|
5
2
|
#has many Portals
|
6
3
|
@@all = []
|
@@ -18,10 +15,6 @@ class Topic
|
|
18
15
|
# return create_portal
|
19
16
|
end
|
20
17
|
|
21
|
-
def self.all_topics_list
|
22
|
-
return Scraper.all_topics
|
23
|
-
end
|
24
|
-
|
25
18
|
def portals
|
26
19
|
@portals
|
27
20
|
end
|