-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiquote_to_fortune.rb
53 lines (37 loc) · 969 Bytes
/
wikiquote_to_fortune.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'rubygems'
require 'nokogiri'
require 'open-uri'
# Get name and make url
if ARGV[0].nil?
puts "I need at least one parameter."
exit 1
end
page = ARGV[0]
url = "https://en.wikiquote.org/w/index.php?action=edit&title=#{ARGV[0].gsub("\s","_")}"
# parse and extract quotes
document = Nokogiri::HTML.parse(open(url))
text = document.xpath("//textarea[@name='wpTextbox1']").text
extract = text.scan(/^\*.*$/)
# remove sources (start with **)
extract.reject! do |line|
line =~ /^\*\*/
end
# remove mediawiki tags
extract.collect! do |line|
line
.gsub("''","")
.gsub(/{{.*?}}/,"")
.gsub(/<\/?[^>]+>/, "")
.gsub(/^\*/,"")
.gsub(/^\s/,"")
.gsub(/\[\[w:/,"")
.gsub(/\|.*?\]\]/, "")
.gsub("[[","")
.gsub("]]","")
end
# Leave out empty lines and list items that only contain urls
extract.reject!{ |line| line =~ /^\[http.*/ }
extract.reject!{ |line| line.length < 3}
# Join and print
joined = extract.join "\n%\n"
puts joined