otacrawler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 473a91699e7860f33690062beb20a24c38f2bff8
4
+ data.tar.gz: 1924ca2e5e66a1aabf778b03b9afd1439891049b
5
+ SHA512:
6
+ metadata.gz: 1b6bb304343f9153768a46f293699a186c9449cbec443f8829c6403d84f22f5a012433f0d58118eb5de2637f04fe52d3a3b6cbc6854594e3e909ca53deb45e0d
7
+ data.tar.gz: 5cc7bc50c145d2573d66ac1f96acde576b447d97447afb8b4ea4f9a1f35bf4d0b4a61725b8ecf96a3b81e009e5dab0a7c91126af0d730742525c07ee3bad1cf2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in web_comic_crawler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 yuta-muramoto
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.
data/Procfile ADDED
@@ -0,0 +1 @@
1
+ bot: bundle exec bin/otacrawler
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Otacrawler [![Code Climate](https://codeclimate.com/github/yuta-muramoto/otacrawler/badges/gpa.svg)](https://codeclimate.com/github/yuta-muramoto/otacrawler) [![CircleCI](https://circleci.com/gh/yuta-muramoto/otacrawler/tree/test.svg?style=svg)](https://circleci.com/gh/yuta-muramoto/otacrawler/tree/test) <img src="img/IMG_6837.JPG" align="right">
2
+
3
+
4
+ Otacrawler is web comic crawler such as the Akihabara otaku.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'otacrawler'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install crawler
21
+
22
+ ## Usage
23
+
24
+ Run robot:
25
+
26
+ $ bin/otacrawler
27
+
28
+ ## Development
29
+
30
+ If you want to add crawling sites, you add that site to `Otacrawler::Sites`. At that time, you should inherit the site from `Otacrawler::Sites::Base`.
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yuta-muramoto/otacrawler.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.warning = false
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "otacrawler"
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
data/bin/otacrawler ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "otacrawler"
5
+
6
+ robot = Otacrawler::Robot.new
7
+ robot.run
data/bin/setup ADDED
@@ -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
data/circle.yml ADDED
@@ -0,0 +1,27 @@
1
+ machine:
2
+ timezone:
3
+ Asia/Tokyo
4
+
5
+ ruby:
6
+ version:
7
+ 2.3.1
8
+
9
+ post:
10
+ - sudo service postgresql stop
11
+
12
+ database:
13
+ override:
14
+ - mysql -u root < setup.sql
15
+ - mysql otacrawler -u root < otacrawler.sql
16
+
17
+ dependencies:
18
+ pre:
19
+ - gem install bundler --pre
20
+
21
+ override:
22
+ - bundle install:
23
+ timeout: 180
24
+
25
+ test:
26
+ override:
27
+ - bundle exec rake test
data/database.yml ADDED
@@ -0,0 +1,13 @@
1
+ db:
2
+ production:
3
+ adapter: mysql2
4
+ host: localhost
5
+ username: xxxxxxxx
6
+ password: xxxxxxxx
7
+ database: xxxxxxxx
8
+
9
+ development:
10
+ adapter: mysql2
11
+ host: localhost
12
+ username: root
13
+ database: otacrawler
data/img/IMG_6837.JPG ADDED
Binary file
@@ -0,0 +1,21 @@
1
+ require 'anemone'
2
+
3
+ module Otacrawler
4
+ class Collector
5
+ def initialize(url)
6
+ @url = url
7
+ end
8
+
9
+ def collect(pattern)
10
+ comics = []
11
+ Anemone.crawl(@url, depth_limit: 0) do |anemone|
12
+ anemone.on_every_page do |page|
13
+ page.doc.xpath(pattern).each do |comic|
14
+ comics << comic
15
+ end
16
+ end
17
+ end
18
+ comics
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require "active_record"
2
+
3
+ module Otacrawler
4
+ module Models
5
+ class Author < ActiveRecord::Base
6
+ has_many :writing
7
+ has_many :comics, through: :writing
8
+
9
+ validates :name, presence: true, uniqueness: true
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require "active_record"
2
+
3
+ module Otacrawler
4
+ module Models
5
+ class Comic < ActiveRecord::Base
6
+ has_many :stories
7
+ has_many :writing
8
+ has_many :authors, through: :writing
9
+
10
+ validates :url, presence: true, uniqueness: true
11
+ validates :title, presence: true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require "active_record"
2
+
3
+ module Otacrawler
4
+ module Models
5
+ class Story < ActiveRecord::Base
6
+ belongs_to :comic
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require "active_record"
2
+
3
+ module Otacrawler
4
+ module Models
5
+ class Writing < ActiveRecord::Base
6
+ self.table_name = "writing"
7
+ belongs_to :comic
8
+ belongs_to :author
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Otacrawler
2
+ class Robot
3
+ def run
4
+ sites.each do |site|
5
+ site.analyze
6
+ end
7
+ end
8
+
9
+ def sites
10
+ Otacrawler.sites.map {|site_class| site_class.new }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Otacrawler
2
+ module Sites
3
+ class Base
4
+ def self.inherited(child)
5
+ Otacrawler.sites << child
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,142 @@
1
+ require "mem"
2
+ require 'uri'
3
+
4
+ module Otacrawler
5
+ module Sites
6
+ class TonariNoYJ < Base
7
+ include Mem
8
+
9
+ def initialize
10
+ @url = "http://www.tonarinoyj.jp"
11
+ @collector = Collector.new(@url)
12
+ end
13
+
14
+ def analyze
15
+ url_list.each_with_index do |url, i|
16
+ params = { url: url, title: titles[i] }
17
+ comic = Models::Comic.new(params)
18
+ if comic.save # overlapping check
19
+ create(comic, i)
20
+ else
21
+ update(url, i)
22
+ end
23
+ end
24
+ end
25
+
26
+ def url_list
27
+ item_body.map do |comic|
28
+ path = comic.css('a').first.attributes["href"].to_s
29
+ File.join(@url, path)
30
+ end
31
+ end
32
+ memoize :url_list
33
+
34
+ def titles
35
+ item_body.map do |comic|
36
+ comic.css('h3').first.inner_text
37
+ end
38
+ end
39
+ memoize :titles
40
+
41
+ def authors
42
+ item_body.map do |comic|
43
+ authors = comic.css('p').first.inner_text
44
+ authors.split(/[[:space:]]/)
45
+ end
46
+ end
47
+ memoize :authors
48
+
49
+ def image_url_list
50
+ css = 'figure.home-manga-item-figure'
51
+ comics = items.map {|item| item.css(css) }
52
+ comics.map do |comic|
53
+ path = comic.css('img').first.attributes['src'].to_s
54
+ File.join(@url, path)
55
+ end
56
+ end
57
+ memoize :image_url_list
58
+
59
+ def description(url)
60
+ collector = Collector.new(url)
61
+ pattern = "//div[@class='single-story']"
62
+ comic = collector.collect(pattern)
63
+ comic.first.css('p').first.inner_text
64
+ rescue
65
+ ""
66
+ end
67
+
68
+ def stories(url)
69
+ collector = Collector.new(url)
70
+ pattern = "//div[@class='single-backnumber']/dl"
71
+ stories = collector.collect(pattern)
72
+ result = stories.map do |story|
73
+ unless story.css('dt').empty?
74
+ title = story.css('dt').first.inner_text
75
+ else
76
+ title = ""
77
+ end
78
+ story.css('li').map do |number|
79
+ path = number.css('a').first.attributes["href"].to_s
80
+ {
81
+ url: URI.join(@url, path).to_s,
82
+ title: title,
83
+ number: number.inner_text.to_i
84
+ }
85
+ end
86
+ end
87
+ result.flatten
88
+ end
89
+
90
+ private
91
+
92
+ def create(comic, i)
93
+ comic.image_url = image_url_list[i]
94
+ comic.description = description(comic.url)
95
+ authors[i].each do |author|
96
+ comic.authors.build({ name: author })
97
+ end
98
+ stories(comic.url).each do |story|
99
+ comic.stories.build(story)
100
+ end
101
+ comic.save
102
+ end
103
+
104
+ def update(url, i)
105
+ comic = Models::Comic.find_by(url: url)
106
+ comic.image_url = image_url_list[i]
107
+ comic.description = description(url)
108
+ comic.save
109
+ update_author(url, i)
110
+ update_story(url, i)
111
+ end
112
+
113
+ def update_author(url, i)
114
+ comic = Models::Comic.find_by(url: url)
115
+ authors[i].each do |author|
116
+ comic.authors.build({ name: author })
117
+ end
118
+ comic.save
119
+ end
120
+
121
+ def update_story(url, i)
122
+ comic = Models::Comic.find_by(url: url)
123
+ stories(url).each do |story|
124
+ comic.stories.build(story)
125
+ end
126
+ comic.save
127
+ end
128
+
129
+ def items
130
+ pattern = "//div[@class='home-manga-item-wrapper']"
131
+ @collector.collect(pattern)
132
+ end
133
+ memoize :items
134
+
135
+ def item_body
136
+ css = 'div.home-manga-item-body'
137
+ items.map {|item| item.css(css) }
138
+ end
139
+ memoize :item_body
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module Otacrawler
2
+ VERSION = "0.1.0"
3
+ end
data/lib/otacrawler.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "erb"
2
+ require "yaml"
3
+ require "mem"
4
+ require "active_record"
5
+
6
+ module Otacrawler
7
+ config = YAML.load(ERB.new(IO.read("database.yml")).result)
8
+ ActiveRecord::Base.establish_connection(config["db"]["development"])
9
+
10
+ class << self
11
+ include Mem
12
+ def sites
13
+ []
14
+ end
15
+ memoize :sites
16
+ end
17
+ end
18
+
19
+ require "otacrawler/version"
20
+ require "otacrawler/collector"
21
+ require "otacrawler/robot"
22
+ require "otacrawler/models/comic"
23
+ require "otacrawler/models/author"
24
+ require "otacrawler/models/story"
25
+ require "otacrawler/models/writing"
26
+
27
+ require "otacrawler/sites/base"
28
+ require "otacrawler/sites/tonari_no_yj"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'otacrawler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "otacrawler"
8
+ spec.version = Otacrawler::VERSION
9
+ spec.authors = ["yuta-muramoto"]
10
+ spec.email = ["s1513114@u.tsukuba.ac.jp"]
11
+
12
+ spec.summary = %q{This is web comic crawler.}
13
+ spec.description = %q{This is web comic crawler that Akihabara otaku.}
14
+ spec.homepage = "https://github.com/yuta-muramoto/otacrawler.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'mem'
23
+ spec.add_dependency 'mysql2'
24
+ spec.add_dependency 'anemone'
25
+ spec.add_dependency 'activerecord'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.11"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "minitest-reporters"
31
+ end