ncode-syosetu-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de29b0e2e818040b6e9130ae63096e5486bb1221
4
+ data.tar.gz: 87967d17238840fcfa7e7847df99add4bec9c0b0
5
+ SHA512:
6
+ metadata.gz: 15e12afd38ec333a769d9a144ab6bd0bd52ee0a808e8868e9eda005c441c405187b8ad7e5eff4e9c92ed1a4d8bc46cd81755716a5dc1dcc42f8773a9fb53496d
7
+ data.tar.gz: 5e2a6c096fb8179c3fc064cf3b6d77ec396868532578de5d0dfa79ca1a1d82fb1542d4defa92ff6de92cfa689286d1d598f905ee854fac50fd3046b8ae08ddb1
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ncode-syosetu-core.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 hogelog
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Ncode::Syosetu::Core
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ncode/syosetu/core`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ncode-syosetu-core'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ncode-syosetu-core
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ncode-syosetu-core.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ncode/syosetu/core"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require "ncode_syosetu"
@@ -0,0 +1,12 @@
1
+ require "ncode_syosetu/version"
2
+ require "ncode_syosetu/model"
3
+ require "ncode_syosetu/client"
4
+ require "ncode_syosetu/builder"
5
+
6
+ module NcodeSyosetu
7
+ NCODE_HOST_NAME = "ncode.syosetu.com"
8
+
9
+ def self.client
10
+ @@client ||= Client.new
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require "ncode_syosetu/builder/mobi"
2
+
3
+ module NcodeSyosetu
4
+ module Builder
5
+ end
6
+ end
@@ -0,0 +1,60 @@
1
+ require 'mechanize'
2
+ require "logger"
3
+
4
+ module NcodeSyosetu
5
+ class Client
6
+ class NotFound < StandardError
7
+ attr_reader :url
8
+
9
+ def initialize(url, error_message = nil)
10
+ super(error_message)
11
+ @url = url
12
+ end
13
+ end
14
+
15
+ def initialize(logger: Logger.new(STDOUT), sleep: 0.5)
16
+ @mechanize = Mechanize.new
17
+ @logger = logger
18
+ @sleep = sleep
19
+ end
20
+
21
+ def get(ncode)
22
+ toc = get_toc(ncode)
23
+
24
+ episodes = toc.episodes.map do |episode|
25
+ if episode[:number]
26
+ get_episode(ncode, episode[:text], episode[:number])
27
+ else
28
+ NcodeSyosetu::Model::Heading.new(episode[:text])
29
+ end
30
+ end
31
+
32
+ NcodeSyosetu::Model::Novel.new(toc, episodes)
33
+ end
34
+
35
+ def get_toc(ncode)
36
+ url = toc_url(ncode)
37
+ NcodeSyosetu::Model::Toc.new(get_content(toc_url(ncode)))
38
+ end
39
+
40
+ def get_episode(ncode, title, number)
41
+ sleep(@sleep)
42
+ NcodeSyosetu::Model::Episode.new(title, number, get_content(episode_url(ncode, number)))
43
+ end
44
+
45
+ def toc_url(ncode)
46
+ "http://#{NcodeSyosetu::NCODE_HOST_NAME}/#{ncode}"
47
+ end
48
+
49
+ def episode_url(ncode, number)
50
+ "http://#{NcodeSyosetu::NCODE_HOST_NAME}/#{ncode}/#{number}"
51
+ end
52
+
53
+ private
54
+
55
+ def get_content(url)
56
+ @logger.info("GET #{url}...")
57
+ @mechanize.get(url)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ require "ncode_syosetu/model/novel"
2
+ require "ncode_syosetu/model/toc"
3
+ require "ncode_syosetu/model/heading"
4
+ require "ncode_syosetu/model/episode"
5
+
6
+ module NcodeSyosetu
7
+ module Model
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require "erb"
2
+
3
+ module NcodeSyosetu
4
+ module Model
5
+ class Episode
6
+ attr_accessor :title, :number, :body_html, :url
7
+
8
+ def initialize(title, number, page)
9
+ @url = page.uri.to_s
10
+ @title = title
11
+ @number = number
12
+
13
+ @body_html =
14
+ page.search(".novel_subtitle").to_html <<
15
+ page.search(".novel_view").to_html
16
+ end
17
+
18
+ def html
19
+ <<-HTML
20
+ <?xml version="1.0" encoding="UTF-8"?>
21
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
23
+ <head>
24
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
25
+ <title>#{@title}</title>
26
+ </head>
27
+ <body>
28
+
29
+ #{@body_html}
30
+
31
+ </body>
32
+ </html>
33
+ HTML
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ module NcodeSyosetu
2
+ module Model
3
+ class Heading
4
+ attr_accessor :title
5
+
6
+ def initialize(title)
7
+ @title = title
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module NcodeSyosetu
2
+ module Model
3
+ class Novel
4
+ attr_accessor :toc, :episodes
5
+
6
+ def initialize(toc, episodes)
7
+ @toc = toc
8
+ @episodes = episodes
9
+ end
10
+
11
+ [:title, :author, :abstract, :url].each do |method|
12
+ class_eval <<-EOS
13
+ def #{method}
14
+ @toc.#{method}
15
+ end
16
+ EOS
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ module NcodeSyosetu
2
+ module Model
3
+ class Toc
4
+ attr_accessor :title, :author, :abstract, :url, :episodes
5
+
6
+ def initialize(page)
7
+ @url = page.uri.to_s
8
+ @title = page.title
9
+ @author = page.search(".novel_writername").text.chomp
10
+ @abstract = page.search(".novel_ex").text.chomp
11
+
12
+ @episodes = []
13
+ page.at(".index_box").children.each do |sub_item|
14
+ next unless sub_item.matches?('.chapter_title, .novel_sublist2')
15
+ episode = { text: sub_item.text.gsub(/\s+/, " ").chomp }
16
+ link = sub_item.search("a")
17
+ unless link.empty?
18
+ if link.attr("href").value =~ %r[/(\d+)/?$]
19
+ episode[:number] = $1.to_i
20
+ end
21
+ end
22
+ @episodes << episode
23
+ end
24
+
25
+ @body_html =
26
+ page.search(".novel_writername").to_html <<
27
+ page.search(".novel_ex").to_html <<
28
+ page.search(".index_box").to_html
29
+ end
30
+
31
+ def html
32
+ <<-HTML
33
+ <?xml version="1.0" encoding="UTF-8"?>
34
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
35
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
36
+ <head>
37
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
38
+ <title>#{@title}</title>
39
+ </head>
40
+ <body>
41
+
42
+ #{@body_html}
43
+
44
+ </body>
45
+ </html>
46
+ HTML
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module NcodeSyosetu
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ require_relative 'lib/ncode_syosetu/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "ncode-syosetu-core"
6
+ spec.version = NcodeSyosetu::VERSION
7
+ spec.authors = ["hogelog"]
8
+ spec.email = ["konbu.komuro@gmail.com"]
9
+
10
+ spec.summary = %q{Ncode syosetu scraper}
11
+ spec.homepage = "https://github.com/hogelog/ncode-syosetu"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mechanize", "2.7.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncode-syosetu-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hogelog
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - konbu.komuro@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/ncode-syosetu-core.rb
85
+ - lib/ncode_syosetu.rb
86
+ - lib/ncode_syosetu/builder.rb
87
+ - lib/ncode_syosetu/client.rb
88
+ - lib/ncode_syosetu/model.rb
89
+ - lib/ncode_syosetu/model/episode.rb
90
+ - lib/ncode_syosetu/model/heading.rb
91
+ - lib/ncode_syosetu/model/novel.rb
92
+ - lib/ncode_syosetu/model/toc.rb
93
+ - lib/ncode_syosetu/version.rb
94
+ - ncode-syosetu-core.gemspec
95
+ homepage: https://github.com/hogelog/ncode-syosetu
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Ncode syosetu scraper
119
+ test_files: []