botz 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f954d590343d6a8b8d94dddb90e405105fd1574f97c7b6b781248401e5694d45
4
+ data.tar.gz: d6757990b3ba7c9a1c57e3ea2537924dbf0df15132f8abda5041a507470be2d0
5
+ SHA512:
6
+ metadata.gz: 21b3758166a2a16069ea8c408a906390f650bc1a9cbd063cd298ea68c5566d6c598af1642a64872e6051bc90025e4cd850b3a8cd4375cbb45513d117121cb78e
7
+ data.tar.gz: 6f12d1277d08d6d2bc3b9f18f69e18f608d669bfab90a77eecf9edb1da3ebd1ce20a5a2d722c106f670378bea05eba3bfc9a461d6f2110f97b3dd63c224d2c1a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # example crawlers
11
+ examples/
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ inherit_from: .rubocop_todo.yml
2
+ AllCops:
3
+ DisplayCopNames: true
4
+ TargetRubyVersion: 2.6
5
+
6
+ Style/ClassAndModuleChildren:
7
+ Enabled: false
8
+
9
+ Style/SignalException:
10
+ EnforcedStyle: semantic
11
+
12
+ Naming/UncommunicativeMethodParamName:
13
+ AllowedNames:
14
+ - as
15
+
16
+ Metrics/LineLength:
17
+ Max: 120
18
+
19
+ Metrics/BlockLength:
20
+ Max: 120
21
+
22
+ SignalException:
23
+ EnforcedStyle: semantic
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,13 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2019-03-29 18:00:03 +0900 using RuboCop version 0.66.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 7
10
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
11
+ # URISchemes: http, https
12
+ Metrics/LineLength:
13
+ Max: 96
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0]
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at aileron.cc@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in crawler.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ botz (0.1.0)
5
+ activemodel (~> 5.2)
6
+ activesupport (~> 5.2)
7
+ mechanize
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (5.2.3)
13
+ activesupport (= 5.2.3)
14
+ activesupport (5.2.3)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ coderay (1.1.2)
20
+ concurrent-ruby (1.1.5)
21
+ connection_pool (2.2.2)
22
+ diff-lcs (1.3)
23
+ domain_name (0.5.20180417)
24
+ unf (>= 0.0.5, < 1.0.0)
25
+ http-cookie (1.0.3)
26
+ domain_name (~> 0.5)
27
+ i18n (1.6.0)
28
+ concurrent-ruby (~> 1.0)
29
+ mechanize (2.7.6)
30
+ domain_name (~> 0.5, >= 0.5.1)
31
+ http-cookie (~> 1.0)
32
+ mime-types (>= 1.17.2)
33
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
34
+ net-http-persistent (>= 2.5.2)
35
+ nokogiri (~> 1.6)
36
+ ntlm-http (~> 0.1, >= 0.1.1)
37
+ webrobots (>= 0.0.9, < 0.2)
38
+ method_source (0.9.2)
39
+ mime-types (3.2.2)
40
+ mime-types-data (~> 3.2015)
41
+ mime-types-data (3.2019.0331)
42
+ mini_portile2 (2.4.0)
43
+ minitest (5.11.3)
44
+ net-http-digest_auth (1.4.1)
45
+ net-http-persistent (3.0.1)
46
+ connection_pool (~> 2.2)
47
+ nokogiri (1.10.3)
48
+ mini_portile2 (~> 2.4.0)
49
+ ntlm-http (0.1.1)
50
+ pry (0.12.2)
51
+ coderay (~> 1.1.0)
52
+ method_source (~> 0.9.0)
53
+ rake (10.5.0)
54
+ rspec (3.8.0)
55
+ rspec-core (~> 3.8.0)
56
+ rspec-expectations (~> 3.8.0)
57
+ rspec-mocks (~> 3.8.0)
58
+ rspec-core (3.8.0)
59
+ rspec-support (~> 3.8.0)
60
+ rspec-expectations (3.8.2)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.8.0)
63
+ rspec-mocks (3.8.0)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.8.0)
66
+ rspec-support (3.8.0)
67
+ thread_safe (0.3.6)
68
+ tzinfo (1.2.5)
69
+ thread_safe (~> 0.1)
70
+ unf (0.1.4)
71
+ unf_ext
72
+ unf_ext (0.0.7.6)
73
+ webrobots (0.1.2)
74
+
75
+ PLATFORMS
76
+ ruby
77
+
78
+ DEPENDENCIES
79
+ botz!
80
+ bundler (~> 2.0)
81
+ pry
82
+ rake (~> 10.0)
83
+ rspec (~> 3.0)
84
+
85
+ BUNDLED WITH
86
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 aileron
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/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Botz
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/crawler`. 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 'botz'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install botz
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]/crawler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Crawler project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/crawler/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'botz'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+ def reload!
13
+ files = $LOADED_FEATURES.select { |feat| feat =~ %r{/gem_name/} }
14
+ files.each { |file| load file }
15
+ load 'lib/botz.rb'
16
+ end
17
+ Pry.start
18
+
19
+ # require 'irb'
20
+ # IRB.start(__FILE__)
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/botz.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'botz/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'botz'
9
+ spec.version = Botz::VERSION
10
+ spec.authors = ['aileron']
11
+ spec.email = ['aileron.cc@gmail.com']
12
+
13
+ spec.summary = 'web bot crawling dsl'
14
+ # spec.description = 'TODO: Write a longer description or delete this line.'
15
+ spec.homepage = 'https://github.com/aileron-inc/botz'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 2.0'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+
32
+ spec.add_runtime_dependency 'activemodel', '~> 5.2'
33
+ spec.add_runtime_dependency 'activesupport', '~> 5.2'
34
+ spec.add_runtime_dependency 'mechanize'
35
+ end
data/exe/botz ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'botz'
5
+ crawler = Botz.open(ARGV[0])
6
+ if ARGV[1]
7
+ crawler.call(ARGV[1], ARGV[2])
8
+ else
9
+ print <<~SHELL
10
+ function spider() {
11
+ botz #{ARGV[0]} spider $1
12
+ }
13
+ function scraper() {
14
+ botz #{ARGV[0]} scraper $1
15
+ }
16
+ SHELL
17
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Class representing a website defined by DSL
5
+ #
6
+ class Botz::Command
7
+ class_attribute :definition
8
+
9
+ class << self
10
+ delegate :domain, :spiders, :scrapers, to: :definition
11
+
12
+ def call(category, name)
13
+ case category.to_sym
14
+ when :spider then spider(name)
15
+ when :scraper then scraper(name)
16
+ end
17
+ end
18
+
19
+ # rubocop:disable Lint/AssignmentInCondition, Style/RescueStandardError
20
+ def scraper(name)
21
+ command = scrapers[name.to_sym]
22
+ while line = STDIN.gets
23
+ url = line.strip
24
+ begin
25
+ command.call(url)
26
+ rescue
27
+ STDERR.puts "ERROR #{command} #{url}"
28
+ end
29
+ end
30
+ end
31
+ # rubocop:enable Lint/AssignmentInCondition, Style/RescueStandardError
32
+
33
+ def spider(name)
34
+ command = spiders[name.to_sym]
35
+ if File.pipe?(STDIN)
36
+ STDIN.each_line do |line|
37
+ start_url = line.strip
38
+ command.call(start_url) { |url| puts url }
39
+ end
40
+ else
41
+ command.call { |url| puts url }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # This class is responsible for actually making a network connection and downloading hypertext
5
+ #
6
+ class Botz::Downloader
7
+ include ActiveModel::Model
8
+ include ActiveModel::Attributes
9
+ USER_AGENT = [
10
+ 'Mozilla/5.0',
11
+ '(Macintosh; Intel Mac OS X 10_12_6)',
12
+ 'AppleWebKit/537.36',
13
+ '(KHTML, like Gecko)',
14
+ 'Chrome/64.0.3282.186',
15
+ 'Safari/537.36'
16
+ ].join(' ')
17
+
18
+ html_loader = lambda do |ctx, url, block|
19
+ block_result = nil
20
+ agent = Mechanize.new
21
+ agent.user_agent = USER_AGENT
22
+ ctx&.call(agent)
23
+ agent.get(url) { |page| block_result = block.call(page) }
24
+ block_result
25
+ end
26
+
27
+ json_loader = lambda do |_ctx, url, block|
28
+ block.call JSON.parse(OpenURI.open_uri(url, 'User-Agent' => USER_AGENT))
29
+ end
30
+
31
+ class_attribute :loaders, default: { html: html_loader, json: json_loader }
32
+
33
+ attribute :context
34
+ attribute :loader
35
+
36
+ def initialize(name, context = nil)
37
+ super(loader: loaders[name], context: context)
38
+ end
39
+
40
+ def call(url, &block)
41
+ loader.call(context, url, block)
42
+ end
43
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # DSL for parsing html into objects
5
+ #
6
+ class Botz::HtmlScraperMacro
7
+ include ActiveModel::Model
8
+ include ActiveModel::Attributes
9
+
10
+ #
11
+ # Scraper error class
12
+ #
13
+ class Error < StandardError
14
+ def initialize(scraper_class, url, errors)
15
+ super("#{scraper_class}(#{url}) # #{errors.full_messages}")
16
+ end
17
+ end
18
+
19
+ attr_reader :scraper_class
20
+ attr_reader :url
21
+ attr_reader :html
22
+ attr_reader :writer
23
+
24
+ def initialize(scraper_class, resource, writer)
25
+ @scraper_class = scraper_class
26
+ @url = resource.uri
27
+ @html = resource
28
+ @writer = writer
29
+ end
30
+
31
+ class << self
32
+ def field_names
33
+ @field_names ||= []
34
+ end
35
+ end
36
+
37
+ def primary_key
38
+ url.to_s
39
+ end
40
+
41
+ def to_h
42
+ fetched_at = Time.current
43
+ fetched_on = fetched_at.beginning_of_day
44
+ timestamps = { fetched_on: fetched_on, fetched_at: fetched_at }
45
+ self.class.field_names.map { |field| [field, send(field)] }.to_h.merge(timestamps)
46
+ end
47
+
48
+ def save
49
+ fail Error.new(scraper_class, url, errors) if invalid?
50
+
51
+ writer.call(to_h)
52
+ end
53
+
54
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
55
+ def self.field(name, path = nil, persist: true, &block)
56
+ if persist
57
+ field_names << name
58
+ case name
59
+ when /.*\?/
60
+ validates name, inclusion: { in: [true, false] }
61
+ else
62
+ validates name, presence: true, allow_blank: true
63
+ end
64
+ end
65
+
66
+ return define_method(name) { instance_exec(html, &block) } if path.nil?
67
+ return define_method(name) { html.search(path).text.strip } if block.nil?
68
+
69
+ define_method(name) { html.search(path).first.try { |e| instance_exec(e, &block) } }
70
+ end
71
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
72
+ end
data/lib/botz/macro.rb ADDED
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Class representing a website defined by DSL
5
+ #
6
+ class Botz::Macro
7
+ Output = ->(result) { STDOUT.puts(result.to_json) }
8
+
9
+ class_attribute :name
10
+ class_attribute :domain
11
+ class_attribute :command
12
+ class_attribute :spiders, default: {}
13
+ class_attribute :scrapers, default: {}
14
+
15
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
16
+ class << self
17
+ def output(&block)
18
+ remove_const(:Output)
19
+ const_set(:Output, block)
20
+ end
21
+
22
+ def scraper(name, as: :html, output: Output, &block)
23
+ class_name = "#{name}_scraper".classify
24
+ downloader = Botz::Downloader.new(as)
25
+ binder_base = Botz.const_get "#{as}_scraper_macro".classify
26
+ binder = Class.new(binder_base, &block)
27
+ binder.define_singleton_method(:name) { class_name }
28
+ crawler_class = self
29
+ scraper_class = Class.new do
30
+ define_singleton_method(:crawler_class) { crawler_class }
31
+ define_singleton_method(:bind) do |url|
32
+ downloader.call(url) do |resource|
33
+ binder.new(scraper_class, resource, output)
34
+ end
35
+ end
36
+ define_singleton_method(:call) { |url| bind(url).save }
37
+ end
38
+ const_set(class_name, scraper_class)
39
+ scrapers[name] = scraper_class
40
+ end
41
+
42
+ def spider(name, start_url = nil, as: :html, &block)
43
+ downloader = Botz::Downloader.new(as)
44
+ spider = Botz::Spider.new(&block)
45
+ spider_class = Class.new do
46
+ define_singleton_method(:call) do |url = start_url, &spider_block|
47
+ downloader.call(url) do |resource|
48
+ spider.call(resource, &spider_block)
49
+ end
50
+ end
51
+ end
52
+ const_set("#{name}_spider".classify, spider_class)
53
+ spiders[name] = spider_class
54
+ end
55
+
56
+ def before_context(url:)
57
+ downloader = Botz::Downloader.new(:html)
58
+ before_context_class = Class.new do
59
+ define_singleton_method(:call) do
60
+ downloader.call(url) do |page|
61
+ yield(page)
62
+ page.mech
63
+ end
64
+ end
65
+ end
66
+ const_set('before_context'.classify, before_context_class)
67
+ end
68
+ end
69
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
70
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Spider
5
+ #
6
+ class Botz::Spider
7
+ def initialize(&block)
8
+ define_singleton_method(:bind, &block)
9
+ end
10
+
11
+ def call(resource)
12
+ yielder = lambda do |url|
13
+ yield url if block_given?
14
+ end
15
+ bind(resource, yielder)
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Botz
4
+ VERSION = '0.1.0'
5
+ end
data/lib/botz.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'botz/version'
4
+ require 'active_support/all'
5
+ require 'active_model'
6
+ require 'mechanize'
7
+
8
+ #
9
+ # web bot dsl engine
10
+ #
11
+ module Botz
12
+ extend ActiveSupport::Autoload
13
+ autoload :Command
14
+ autoload :Macro
15
+ autoload :Spider
16
+ autoload :Downloader
17
+ autoload :HtmlScraperMacro
18
+
19
+ def self.open(filepath)
20
+ remove_const(:Crawler) if const_defined?(:Crawler)
21
+ const_set(:Crawler, Module.new)
22
+ # rubocop:disable Security/Eval
23
+ eval(File.open(filepath).read)
24
+ # rubocop:enable Security/Eval
25
+ end
26
+
27
+ def self.define(name, domain:, &block)
28
+ crawler_definition = Class.new(Botz::Macro, &block)
29
+ crawler_definition.domain = domain
30
+
31
+ command_class = Class.new(Botz::Command)
32
+ command_class.definition = crawler_definition
33
+ crawler_definition.command = command_class
34
+
35
+ Crawler.const_set(name.to_s.camelize, crawler_definition)
36
+ command_class
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: botz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aileron
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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
+ - !ruby/object:Gem::Dependency
70
+ name: activemodel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mechanize
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - aileron.cc@gmail.com
114
+ executables:
115
+ - botz
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".rubocop_todo.yml"
123
+ - ".ruby-version"
124
+ - ".travis.yml"
125
+ - CHANGELOG.md
126
+ - CODE_OF_CONDUCT.md
127
+ - Gemfile
128
+ - Gemfile.lock
129
+ - LICENSE.txt
130
+ - README.md
131
+ - Rakefile
132
+ - bin/console
133
+ - bin/setup
134
+ - botz.gemspec
135
+ - exe/botz
136
+ - lib/botz.rb
137
+ - lib/botz/command.rb
138
+ - lib/botz/downloader.rb
139
+ - lib/botz/html_scraper_macro.rb
140
+ - lib/botz/macro.rb
141
+ - lib/botz/spider.rb
142
+ - lib/botz/version.rb
143
+ homepage: https://github.com/aileron-inc/botz
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 3.0.3
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: web bot crawling dsl
166
+ test_files: []