bored-wikipedia-explorer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf79c1ca3b3092734ef5b9a8cebad458513ee39687d63055ac631bfb9f25be58
4
+ data.tar.gz: 5fc96f3106247e29568087ab1698bd49d72cccef58764e9b74458a90f74f1e37
5
+ SHA512:
6
+ metadata.gz: 9d5f2b16b680aae2139dd4715846c00fddf8d4dca5b0ee1b1a9d7acc192d58dab35e27ce7d675876cf3d23596267e11c33fbec02f8cefbfbd45a05b8a8892f55
7
+ data.tar.gz: 416000b632164d4f5589519a441ebcfe1301517d8dde0afbe0e6f3127859275b74321ba25cb0fcff1f53c25ca976e5211c0b3fff2eb8e64fc2068c1fd891f763
data/bin/run ADDED
@@ -0,0 +1,4 @@
1
+ require_relative "../lib/command_line_interface.rb"
2
+
3
+
4
+ CommandLineInterface.run
@@ -0,0 +1,61 @@
1
+ require_relative "../lib/topic.rb"
2
+ require "colorize"
3
+ require "launchy"
4
+
5
+ class CommandLineInterface
6
+ def self.run
7
+ puts "Welcome to Did-You-Know Wikipedia Edition!"
8
+ puts "Please select a topic to be given a random Wikipedia Portal to read:"
9
+ get_inputs #starts cli flow
10
+ end
11
+
12
+ def self.get_inputs
13
+ display_all_topics #Displays all available main topics
14
+ puts "Select a number to explore that topic"
15
+ get_choice #gets users main topic choice
16
+ get_rand_url #gets random sub-topic and creates Portal objects
17
+ visit_portal #asks user if they want to visit the randomnly select sub-topic
18
+ end
19
+
20
+ #beautifies and lists the command line options
21
+ def self.display_all_topics
22
+ colors = [:red, :green, :yellow, :blue, :magenta, :cyan, :red, :green, :yellow, :blue, :magenta]
23
+ @list = []
24
+ Topic.all_topics_list.each_with_index{|item, indx|
25
+ @list << item
26
+ puts "#{indx + 1}. #{item}".colorize(colors[indx])
27
+ }
28
+ end
29
+
30
+ def self.get_choice
31
+ @choice = @list[gets.strip.to_i - 1]
32
+ @topic = Topic.find_or_create_by_name(@choice)
33
+ end
34
+
35
+ def self.get_rand_url
36
+ @randurl = Scraper.scrape_portals_page(@choice)
37
+ @portal = Portal.find_or_create_by_url(@randurl)
38
+ @portal.name = Scraper.get_portal_name(@randurl)
39
+ @portal.topic = @topic
40
+ @topic.portals << @portal
41
+ end
42
+
43
+ def self.visit_portal
44
+ puts "We've selected " + Scraper.get_portal_name(@randurl)
45
+ + " for you within the " + @choice +" topic you selected."
46
+ puts "Would you like to visit this page? (Y/N)"
47
+ if gets.strip.upcase == "Y"
48
+ puts @randurl
49
+ Launchy.open(@randurl)
50
+ else
51
+ puts "Either type 'reroll' to choose another page within the " + @choice + " topic you selected. Or select a new topic with 'new'."
52
+ choice = gets.strip
53
+ if choice == "reroll"
54
+ get_rand_url
55
+ visit_portal
56
+ elsif choice == "new"
57
+ get_inputs
58
+ end
59
+ end
60
+ end
61
+ end
data/lib/portal.rb ADDED
@@ -0,0 +1,25 @@
1
+ require_relative "../lib/scraper.rb"
2
+
3
+ class Portal
4
+ attr_accessor :url, :topic, :name
5
+ @@all = []
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @@all << self
10
+ end
11
+
12
+ def self.all
13
+ @@all
14
+ end
15
+
16
+ def self.find_or_create_by_url(url)
17
+ if Portal.all.detect{|portal| url == portal.url}
18
+ @portal = Portal.all.detect{|portal| url == portal.url}
19
+ else
20
+ @portal = Portal.new(url)
21
+ # @@all << @portal
22
+ return @portal
23
+ end
24
+ end
25
+ end
data/lib/scraper.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'pry'
4
+
5
+ class Scraper
6
+ @@all_topics = []
7
+
8
+ def self.scrape_portals_page(name)
9
+ choice_index = @@all_topics.index(name) + 1
10
+
11
+ #choice is the chosen topic index
12
+ #there are 11 main topics derrived from Scraper.all_topics
13
+ html = open("https://en.wikipedia.org/wiki/Portal:Contents/Portals")
14
+ doc = Nokogiri::HTML(html) do |config|
15
+ config.noblanks
16
+ end
17
+
18
+ #set portals-container class for all portal links for each topic
19
+ #Thus there are 12 portal links containers but we're skipping the first one
20
+ doc.search("div").each{|anchor|
21
+ if anchor['style'] == "box-sizing: border-box; border: 0px solid #A3BFB1; border-bottom: 0px solid #A3BFB1;; border-top-width: 1px; vertical-align: top;background: #F5FFFA;opacity: 1; color: black; text-align: left; margin: 0 0 10px; padding: 1em;;padding-top: .3em;-moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0;"
22
+ anchor['class'] = "portals-container"
23
+ end
24
+ }
25
+
26
+ #randomnly select a sub-portal from the main topic portal choice
27
+ randval = Random.new
28
+ randnum = randval.rand(doc.search(".portals-container")[choice_index].search("a").count{|i| i.attribute("href").value.include?("/wiki/Portal:")})
29
+ randportal = doc.search(".portals-container")[choice_index].search("a")[randnum].attribute("href").value.prepend("https://en.wikipedia.org")
30
+ return randportal
31
+
32
+ end
33
+
34
+ #Scrapes all main topics from all portals main page
35
+ def self.all_topics
36
+ html = open("https://en.wikipedia.org/wiki/Portal:Contents/Portals")
37
+ doc = Nokogiri::HTML(html) do |config|
38
+ config.noblanks
39
+ end
40
+
41
+ #sets a container for the main topic headlines
42
+ doc.search("#mw-content-text div table table div").each{|anchor|
43
+ if anchor['style'] == "position: relative;border: 0px solid #A3BFB1;background: #CEF2E0;color: black;padding: .1em;text-align: center;font-weight: bold;font-size: 100%;margin-bottom: 0px;border-top: 1px solid #A3BFB1;border-bottom: 1px solid #A3BFB1;"
44
+ anchor['class'] = "title_container" unless anchor.text.include?("General reference")
45
+ end
46
+ }
47
+
48
+ #set .headlines class for all main topics
49
+ doc.search("h2 .mw-headline big").each{|anchor|
50
+ anchor['class'] = "headlines" unless anchor.text == "Wikipedia's contents: Portals" || anchor.text == "Wikipedia's contents: Portals" || anchor.text.include?("General reference")
51
+ }
52
+
53
+ #updating the @@all_topics hash with topic symbols
54
+ doc.search(".headlines").each{|anchor|
55
+ copy = anchor.text.chomp("(see in all page types)").strip
56
+ copy.slice!(-3..-1)
57
+ @@all_topics << copy
58
+ }
59
+ return @@all_topics
60
+ end
61
+
62
+ def self.get_portal_name(url)
63
+ html = open(url)
64
+ doc = Nokogiri::HTML(html) do |config|
65
+ config.noblanks
66
+ end
67
+ return doc.search("title").text
68
+ end
69
+
70
+ end
data/lib/topic.rb ADDED
@@ -0,0 +1,42 @@
1
+ require_relative "../lib/scraper.rb"
2
+ require_relative "../lib/portal.rb"
3
+
4
+ class Topic
5
+ #has many Portals
6
+ @@all = []
7
+ attr_accessor :name, :portals
8
+ def initialize(name)
9
+ @name = name
10
+ @portals = []
11
+ # add_portal
12
+ @@all << self
13
+ end
14
+
15
+ def find_random_portal_page
16
+ find_rand_portal = Scraper.scrape_portals_page(@name).sample
17
+ # create_portal = Portal.find_rand_portal
18
+ # return create_portal
19
+ end
20
+
21
+ def self.all_topics_list
22
+ return Scraper.all_topics
23
+ end
24
+
25
+ def portals
26
+ @portals
27
+ end
28
+
29
+ def self.all
30
+ @@all
31
+ end
32
+
33
+ def self.find_or_create_by_name(name)
34
+ if self.all.detect{|topic| topic.name == name}
35
+ @topic = self.all.detect{|topic| topic.name == name}
36
+ else
37
+ @topic = self.new(name)
38
+ end
39
+ @topic
40
+ end
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bored-wikipedia-explorer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Katrina Brinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple content discovery gem
14
+ email: kbrinso@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - bin/run
20
+ - lib/command_line_interface.rb
21
+ - lib/portal.rb
22
+ - lib/scraper.rb
23
+ - lib/topic.rb
24
+ homepage: http://rubygems.org/gems/bored-wikipedia-explorer
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.7.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: If you're bored this is the CLI app for you to explore new topics on Wikipedia.
48
+ test_files: []