sosowa 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +27 -0
- data/Rakefile +2 -0
- data/lib/sosowa/parser.rb +58 -0
- data/lib/sosowa/scheme.rb +102 -0
- data/lib/sosowa/version.rb +3 -0
- data/lib/sosowa.rb +22 -0
- data/sosowa.gemspec +18 -0
- metadata +67 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Oame
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Sosowa
|
2
|
+
|
3
|
+
Sosowa Parser for Ruby 1.9.x
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install sosowa
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
require "sosowa"
|
12
|
+
|
13
|
+
# 最新版から最初のSSを持ってくる
|
14
|
+
novel = Sosowa.get.first
|
15
|
+
puts novel.text
|
16
|
+
|
17
|
+
# 作品集番号156の1320873807を持ってくる
|
18
|
+
novel = Sosowa.get(:log => 156, :key => 1320873807)
|
19
|
+
puts novel.text
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
1. Fork it
|
24
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
25
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
26
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
27
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module Sosowa
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@agent = Mechanize.new
|
6
|
+
@agent.user_agent = "Sosowa Ruby #{Sosowa::VERSION}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def fetch_index(log)
|
10
|
+
page = @agent.get("#{Sosowa::BASE_URL}/?log=#{log}")
|
11
|
+
indexes = []
|
12
|
+
tr = page.search("tr")
|
13
|
+
tr = tr[1, tr.size-1]
|
14
|
+
num = 1
|
15
|
+
tr.each do |tr|
|
16
|
+
if (tr/%{td[@class="tags"]}).size > 0
|
17
|
+
#tags = (tr/%{td[@class="tags"]})[0].inner_html.to_s.toutf8.strip.gsub(/^.+?: /, "").split(/\s/)
|
18
|
+
#indexes[num-1] << tags
|
19
|
+
else
|
20
|
+
title = tr.search(%{td[@class="title cell_title"] > a}).inner_html.to_s.toutf8.strip
|
21
|
+
tags = tr.search(%{td[@class="title cell_title"] > a})[0].attributes["title"].value.split(" / ")
|
22
|
+
key = tr.search(%{td[@class="title cell_title"] > a})[0].attributes["href"].value.gsub(/^.+key=(.+?)&.+$/, '\1').to_i
|
23
|
+
author = tr.search(%{td[@class="cell_author"]}).inner_html.to_s.toutf8.strip
|
24
|
+
created_at = Time.parse(tr.search(%{td[@class="cell_created"]}).inner_html.to_s.toutf8.strip)
|
25
|
+
updated_at = Time.parse(tr.search(%{td[@class="cell_lastup"]}).inner_html.to_s.toutf8.strip)
|
26
|
+
eval = tr.search(%{td[@class="cell_eval"]}).inner_html.to_s.toutf8.strip.split("/")
|
27
|
+
review_count = eval[1].to_i
|
28
|
+
comment_count = eval[0].to_i
|
29
|
+
point = tr.search(%{td[@class="cell_point"]}).inner_html.to_s.toutf8.strip.to_i
|
30
|
+
rate = tr.search(%{td[@class="cell_rate"]}).inner_html.to_s.toutf8.strip.to_f
|
31
|
+
size = tr.search(%{td[@class="cell_size"]}).inner_html.to_s.toutf8.strip
|
32
|
+
index = {
|
33
|
+
:log => log,
|
34
|
+
:key => key,
|
35
|
+
:title => title,
|
36
|
+
:author => author,
|
37
|
+
:created_at => created_at,
|
38
|
+
:updated_at => updated_at,
|
39
|
+
:review_count => review_count,
|
40
|
+
:comment_count => comment_count,
|
41
|
+
:point => point,
|
42
|
+
:tags => tags,
|
43
|
+
:rate => rate,
|
44
|
+
:size => size
|
45
|
+
}
|
46
|
+
indexes << Index.new(index)
|
47
|
+
end
|
48
|
+
num += 1
|
49
|
+
end
|
50
|
+
return indexes
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch_novel(log, key)
|
54
|
+
novel = Novel.new(:log => log, :key => key)
|
55
|
+
return novel
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Sosowa
|
2
|
+
class Scheme
|
3
|
+
protected
|
4
|
+
|
5
|
+
def method_missing(action, *args)
|
6
|
+
return @element[action.to_s.to_sym] rescue nil
|
7
|
+
end
|
8
|
+
|
9
|
+
public
|
10
|
+
|
11
|
+
def params() @element.keys.map{|k|k.to_sym} ; end
|
12
|
+
alias_method :available_methods, :params
|
13
|
+
end
|
14
|
+
|
15
|
+
class Novel < Scheme
|
16
|
+
def initialize(args)
|
17
|
+
@log = args[:log] || 0
|
18
|
+
@key = args[:key]
|
19
|
+
@agent = Mechanize.new
|
20
|
+
@page = nil
|
21
|
+
@element = fetch(@log, @key)
|
22
|
+
end
|
23
|
+
|
24
|
+
def fetch(log, key)
|
25
|
+
@page = @agent.get("#{Sosowa::BASE_URL}/?mode=read&log=#{log}&key=#{key}")
|
26
|
+
tags = (@page/%{dl[@class="info"][1] > dd > a}).map{|t| t.inner_html.to_s.toutf8 }
|
27
|
+
text = (@page/%{div[@class="contents ss"]})[0].inner_html.to_s.toutf8
|
28
|
+
ps = (@page/%{div[@class="aft"]})[0].inner_html.to_s.toutf8
|
29
|
+
author_name = (@page/%{div[@class="author"] b})[0].inner_html.to_s.toutf8
|
30
|
+
author_email, author_website = (@page/%{div[@class="author"] a}).map{|e| e.attributes["href"].value}
|
31
|
+
author = Author.new(:name => author_name, :email => (author_email ? author_email.gsub(/^mailto:/, "") : nil), :website => author_website)
|
32
|
+
header = (@page/%{div[@class="header"] > span})[0].inner_html.to_s.toutf8.split(/\r?\n/).map{|n| n.strip}.reject{|n| n == ""}.map{|n| n.split(": ")}
|
33
|
+
review = header[3][1].split("/")
|
34
|
+
comments = []
|
35
|
+
comment_element = (@page/%{div[@class="comments"] > dl > *})
|
36
|
+
comment_element[1, comment_element.size-1].each_slice(2) do |element|
|
37
|
+
bobj = element[0].search("b").map{|n| n.inner_html.to_s.toutf8.strip}
|
38
|
+
point = element[0].search("span").inner_html.to_s.toutf8.to_i
|
39
|
+
id = element[0].inner_html.to_s.toutf8.split(/\r?\n/).map{|n| n.strip}[1].to_i
|
40
|
+
comment = Comment.new(
|
41
|
+
:id => id,
|
42
|
+
:point => point,
|
43
|
+
:name => bobj[0],
|
44
|
+
:created_at => Time.parse(bobj[1].gsub(/[^\/\d\s:]/, "")),
|
45
|
+
:text => element[1].inner_html.to_s.toutf8.strip
|
46
|
+
)
|
47
|
+
comments << comment
|
48
|
+
end
|
49
|
+
novel = {
|
50
|
+
:text => text,
|
51
|
+
:ps => ps,
|
52
|
+
:author => author,
|
53
|
+
:tags => tags,
|
54
|
+
:log => log,
|
55
|
+
:key => key,
|
56
|
+
:created_at => Time.parse(header[1][1]),
|
57
|
+
:updated_at => Time.parse(header[2][1]),
|
58
|
+
:review_count => review[1].to_i,
|
59
|
+
:comment_count => review[0].to_i,
|
60
|
+
:point => header[4][1].to_i,
|
61
|
+
:rate => header[5][1].to_f,
|
62
|
+
:comments => comments
|
63
|
+
}
|
64
|
+
return novel
|
65
|
+
end
|
66
|
+
|
67
|
+
def simple_rating(point)
|
68
|
+
form = @page.forms[0]
|
69
|
+
form.click_button(form.button_with(:value => point.to_s))
|
70
|
+
end
|
71
|
+
|
72
|
+
def comment(text, params)
|
73
|
+
form = @page.forms[1]
|
74
|
+
form.field_with(:name => "name").value = params[:name] || nil
|
75
|
+
form.field_with(:name => "body").value = text
|
76
|
+
form.field_with(:name => "pass").value = params[:pass] || nil
|
77
|
+
form.field_with(:name => "mail").value = params[:mail] || nil
|
78
|
+
form.field_with(:name => "point").option_with(:value => (params[:point].to_s || "0")).select
|
79
|
+
form.click_button
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Comment < Scheme
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class Author < Scheme
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
class Index < Scheme
|
92
|
+
def initialize(element)
|
93
|
+
super(@element)
|
94
|
+
@element = element
|
95
|
+
end
|
96
|
+
|
97
|
+
def fetch
|
98
|
+
Novel.new(:log => @element[:log], :key => @element[:key])
|
99
|
+
end
|
100
|
+
alias_method :get, :fetch
|
101
|
+
end
|
102
|
+
end
|
data/lib/sosowa.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path("../", __FILE__))
|
2
|
+
require "kconv"
|
3
|
+
require "mechanize"
|
4
|
+
require "time"
|
5
|
+
require "cgi"
|
6
|
+
require "sosowa/version"
|
7
|
+
require "sosowa/scheme"
|
8
|
+
require "sosowa/parser"
|
9
|
+
|
10
|
+
module Sosowa
|
11
|
+
BASE_URL = "http://coolier.sytes.net:8080/sosowa/ssw_l"
|
12
|
+
|
13
|
+
def self.get(args={})
|
14
|
+
args[:log] ||= 0
|
15
|
+
parser = Parser.new
|
16
|
+
if args.has_key?(:key)
|
17
|
+
parser.fetch_novel(args[:log], args[:key])
|
18
|
+
else
|
19
|
+
parser.fetch_index(args[:log])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/sosowa.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sosowa/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Oame"]
|
6
|
+
gem.email = ["oame@oameya.com"]
|
7
|
+
gem.description = %q{Sosowa Parser for Ruby}
|
8
|
+
gem.summary = %q{Sosowa Parser for Ruby.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "sosowa"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Sosowa::VERSION
|
17
|
+
gem.add_dependency "mechanize"
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sosowa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Oame
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mechanize
|
16
|
+
requirement: &70129465130960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70129465130960
|
25
|
+
description: Sosowa Parser for Ruby
|
26
|
+
email:
|
27
|
+
- oame@oameya.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/sosowa.rb
|
38
|
+
- lib/sosowa/parser.rb
|
39
|
+
- lib/sosowa/scheme.rb
|
40
|
+
- lib/sosowa/version.rb
|
41
|
+
- sosowa.gemspec
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Sosowa Parser for Ruby.
|
66
|
+
test_files: []
|
67
|
+
has_rdoc:
|