Last active
August 29, 2015 14:06
-
-
Save GiaoGiaoCat/5669857fdfa5ed7d25ca to your computer and use it in GitHub Desktop.
阅读笔趣阁的小说
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'pry' | |
class Reader | |
BASE_URL = 'http://www.biquge.la/book/546/' | |
LINE_SHOULD_BREAK = 10 | |
@@count = 0 | |
class << self | |
def load_page(url) | |
uri = URI(BASE_URL + url) | |
Nokogiri::HTML(open(uri), nil, 'gbk') | |
end | |
def show_content(doc) | |
content = doc.css('div#content')[0] | |
content.children.each do |c| | |
clean_screen_and_reset_count if @@count == LINE_SHOULD_BREAK | |
print_content_and_increase_count(c) if c.node_name == 'text' | |
end | |
end | |
def next_page(doc) | |
doc.css('div.bottem2 a')[3].attributes["href"].text | |
end | |
def read_url(url) | |
clean_screen_and_reset_count | |
puts "Current URL is #{url}" | |
doc = load_page(url) | |
show_content(doc) | |
puts "Press `n` key to read next page." | |
action = gets.chomp | |
if action == 'n' | |
url = next_page(doc) | |
read_url(url) | |
else | |
puts "88" | |
end | |
end | |
def read | |
puts "Enter your book's url:" | |
url = gets | |
puts "Error: No URL." if url.nil? | |
read_url(url) | |
end | |
def clean_screen_and_reset_count | |
action = gets if @@count == 10 | |
system 'clear' | |
@@count = 0 | |
end | |
def print_content_and_increase_count(c) | |
puts c.text | |
@@count += 1 | |
end | |
end | |
end | |
Reader.read |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
有点意思,我试试