forked from TheBrain0110/worm_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_scrape.rb
74 lines (63 loc) · 1.94 KB
/
serial_scrape.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# encoding: utf-8
require 'optparse'
require 'nokogiri'
require 'open-uri'
require 'uri'
@pact_url="https://pactwebserial.wordpress.com/category/story/arc-1-bonds/1-01/"
@twig_url="https://twigserial.wordpress.com/2014/12/24/taking-root-1-1/"
@worm_url="https://parahumans.wordpress.com/2011/06/11/1-1/"
story = { "pact" => @pact_url, "twig" => @twig_url, "worm" => @worm_url}
options = []
OptionParser.new do |opts|
opts.banner = "Usage: serial_scrape.rb [options]"
opts.on("-s", "--series NAME", "Select web series") do |name|
options << name
end
opts.on("-a", "select all") do
options = ["worm", "pact", "twig"]
end
end.parse!
def write_story(starting_chapter)
@next_chapter = starting_chapter
@toc = "<h1>Table of Contents</h1>"
@book_body = ""
@index = 1
while @next_chapter
#check if url is weird
if @next_chapter.to_s.include?("½")
@next_chapter = URI.escape(@next_chapter)
end
if @next_chapter.to_s.start_with?("//")
@next_chapter = "https:" + @next_chapter
end
doc = Nokogiri::HTML(open(@next_chapter))
#get
@chapter_title = doc.css('h1.entry-title').first #html formatted
#modify chapter to have link
@chapter_title_plain = @chapter_title.content
$stderr.puts @chapter_title_plain
@chapter_content = doc.css('div.entry-content').first #gsub first p
#clean
@chapter_content.search('.//div').remove
@to_remove = doc.css('div.entry-content p').first #gsub first p
@chapter_content = @chapter_content.to_s.gsub(@to_remove.to_s,"")
#write
@book_body << "<h1 id=\"chap#{@index.to_s}\">#{@chapter_title_plain}</h1>"
@book_body << @chapter_content
@toc << "<a href=\"#chap#{@index.to_s}\">#{@chapter_title_plain}</a><br>"
@index += 1
#next
@next_chapter = if doc.css('div.entry-content p a').last.content.to_s.include?("Next")
doc.css('div.entry-content p a').last['href']
else
false
end
end
$stderr.puts "Writing Book..."
puts @toc
puts @book_body
end
story.each{ |key, val| if options.include?(key)
write_story(val)
end
}