manga-tools 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b8c2d70d4d5c63f13939ffc01ca319d1c6f711b14ea77a9861ce8c361a914ad1
4
+ data.tar.gz: a6f869a956a9e5c4bb705c4bdcdd73636ee17bbe41c8635a95bd7841ca95338d
5
+ SHA512:
6
+ metadata.gz: 256cfe3263281cb6ac3272405f028ab12d8d963da6dd41993e59f6bbb5ac2fb439384c496fe231aa70f8a8bb044b01d4023b9a45bf5eb03c6901d2e6c3a51b8d
7
+ data.tar.gz: e5dadf87fcf6e8aaea67355802b1b3b932ebf8f51fb270a3a1ab74e6d3cf8d2c454801530107bb50f9ee32d0e4a89caacf1c97ecc472eca889d1194b3d4b7a4c
@@ -0,0 +1,41 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+ strategy:
19
+ fail-fast: false
20
+ matrix:
21
+ os: [ubuntu, macos]
22
+ ruby: [2.6, 2.7]
23
+ runs-on: ${{ matrix.os }}-latest
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby }}
29
+ - name: Setup Code Climate test-reporter
30
+ if: ${{ matrix.os == 'ubuntu' && matrix.ruby == '2.6' }}
31
+ run: |
32
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
33
+ chmod +x ./cc-test-reporter
34
+ ./cc-test-reporter before-build
35
+ - run: bundle install --jobs 4 --retry 3
36
+ - run: bundle exec rake
37
+ - name: Publish code coverage
38
+ if: ${{ matrix.os == 'ubuntu' && matrix.ruby == '2.6' }}
39
+ run: |
40
+ export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
41
+ ./cc-test-reporter after-build -r ${{secrets.CC_TEST_REPORTER_ID}}
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,38 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
11
+
12
+ inherit_from: .rubocop_todo.yml
13
+
14
+ AllCops:
15
+ RubyInterpreters:
16
+ - ruby
17
+ TargetRubyVersion: 2.6
18
+ NewCops: enable
19
+ Exclude:
20
+ - 'bin/*'
21
+
22
+ Metrics/AbcSize:
23
+ Max: 37
24
+
25
+ Metrics/BlockLength:
26
+ Max: 36
27
+
28
+ Metrics/CyclomaticComplexity:
29
+ Max: 10
30
+
31
+ Metrics/MethodLength:
32
+ Max: 44
33
+
34
+ Metrics/PerceivedComplexity:
35
+ Max: 7
36
+
37
+ Style/ClassAndModuleChildren:
38
+ EnforcedStyle: compact
@@ -0,0 +1,8 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-08-09 05:48:45 UTC using RuboCop version 0.89.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
+
@@ -0,0 +1 @@
1
+ 2.7.1
@@ -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 yagihiro@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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in manga-tools.gemspec
6
+ gemspec
7
+
8
+ gem 'faraday'
9
+ gem 'nokogiri'
10
+ gem 'pry'
11
+ gem 'rake', require: false
12
+ gem 'rspec'
13
+ gem 'rubocop', require: false
14
+ gem 'simplecov', '~> 0.17.0', require: false
15
+ gem 'thor'
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ manga-tools (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.4.4)
12
+ docile (1.3.2)
13
+ faraday (1.0.1)
14
+ multipart-post (>= 1.2, < 3)
15
+ json (2.3.1)
16
+ method_source (1.0.0)
17
+ mini_portile2 (2.4.0)
18
+ multipart-post (2.1.1)
19
+ nokogiri (1.10.10)
20
+ mini_portile2 (~> 2.4.0)
21
+ parallel (1.19.2)
22
+ parser (2.7.1.4)
23
+ ast (~> 2.4.1)
24
+ pry (0.13.1)
25
+ coderay (~> 1.1)
26
+ method_source (~> 1.0)
27
+ rainbow (3.0.0)
28
+ rake (13.0.1)
29
+ regexp_parser (1.7.1)
30
+ rexml (3.2.4)
31
+ rspec (3.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.2)
36
+ rspec-support (~> 3.9.3)
37
+ rspec-expectations (3.9.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.3)
44
+ rubocop (0.89.0)
45
+ parallel (~> 1.10)
46
+ parser (>= 2.7.1.1)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ regexp_parser (>= 1.7)
49
+ rexml
50
+ rubocop-ast (>= 0.1.0, < 1.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (>= 1.4.0, < 2.0)
53
+ rubocop-ast (0.3.0)
54
+ parser (>= 2.7.1.4)
55
+ ruby-progressbar (1.10.1)
56
+ simplecov (0.17.1)
57
+ docile (~> 1.1)
58
+ json (>= 1.8, < 3)
59
+ simplecov-html (~> 0.10.0)
60
+ simplecov-html (0.10.2)
61
+ thor (1.0.1)
62
+ unicode-display_width (1.7.0)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ faraday
69
+ manga-tools!
70
+ nokogiri
71
+ pry
72
+ rake
73
+ rspec
74
+ rubocop
75
+ simplecov (~> 0.17.0)
76
+ thor
77
+
78
+ BUNDLED WITH
79
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Hiroki Yagita
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,48 @@
1
+ # Manga::Tools
2
+
3
+ ![Ruby](https://github.com/yagihiro/manga-tools/workflows/Ruby/badge.svg)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/e2797c91a0bd6f521905/maintainability)](https://codeclimate.com/github/yagihiro/manga-tools/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/e2797c91a0bd6f521905/test_coverage)](https://codeclimate.com/github/yagihiro/manga-tools/test_coverage)
6
+
7
+ 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/manga/tools`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ TODO: Delete this and the text above, and describe your gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'manga-tools'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install manga-tools
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ 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.
34
+
35
+ 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).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yagihiro/manga-tools. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yagihiro/manga-tools/blob/master/CODE_OF_CONDUCT.md).
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+
46
+ ## Code of Conduct
47
+
48
+ Everyone interacting in the Manga::Tools project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yagihiro/manga-tools/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "manga/tools"
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(__FILE__)
@@ -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,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'manga/tools'
5
+
6
+ Manga::Tools::CLI.start
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'manga/tools/cache'
4
+ require 'manga/tools/cli'
5
+ require 'manga/tools/http'
6
+ require 'manga/tools/version'
7
+
8
+ module Manga::Tools
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest/md5'
4
+ require 'fileutils'
5
+
6
+ module Manga::Tools
7
+ # Cache class
8
+ class Cache
9
+ class << self
10
+ # Initialize cache
11
+ def init
12
+ FileUtils.mkdir_p(cache_current_year_dir)
13
+ end
14
+
15
+ def root_dir
16
+ @root_dir ||= "#{Dir.home}/.manga-tools"
17
+ end
18
+
19
+ def cache_root_dir
20
+ @cache_root_dir ||= "#{root_dir}/cache"
21
+ end
22
+
23
+ def cache_current_year_dir
24
+ @cache_current_year_dir ||= "#{cache_root_dir}/#{Time.now.year}"
25
+ end
26
+
27
+ # @return [Integer] default expires in seconds (1day)
28
+ def default_expires_in
29
+ @default_expires_in ||= 1 * 24 * 60 * 60
30
+ end
31
+
32
+ # @param key [String] A key to generate a key for the cache
33
+ # @return [String] string of cached data
34
+ def fetch(key:)
35
+ raise ArgumentError, 'mast pass a block' unless block_given?
36
+
37
+ cache = new(key)
38
+ # cache.clear
39
+ return cache.load_data if cache.available?
40
+
41
+ result = yield(cache)
42
+ cache.save_meta_data
43
+ cache.save_data(result)
44
+ result
45
+ end
46
+ end
47
+
48
+ # @return [Integer|nil] A cache deadline in seconds.
49
+ attr_accessor :expires_in
50
+ # @return [String] A key to generate a key for the cache.
51
+ attr_reader :key
52
+ # @return [String] A key for the cache.
53
+ attr_reader :cache_key
54
+ # @return [String] A path to the cache metadata file.
55
+ attr_reader :meta_file_path
56
+ # @return [String] A path to the cached data file.
57
+ attr_reader :data_file_path
58
+
59
+ # @param key [String] A key to generate a key for the cache.
60
+ def initialize(key)
61
+ raise ArgumentError, 'mast pass the key param' unless key
62
+
63
+ @expires_in = self.class.default_expires_in
64
+ @key = key
65
+ @cache_key = Digest::MD5.hexdigest(key)
66
+ @meta_file_path = "#{self.class.cache_current_year_dir}/#{@cache_key}"
67
+ @data_file_path = "#{@meta_file_path}.data"
68
+ end
69
+
70
+ # @return [Boolean] True if a cache is available, otherwise return false.
71
+ def available?
72
+ t = File.mtime(meta_file_path)
73
+ saved_expires_in = File.read(meta_file_path).strip.to_i
74
+
75
+ Time.now <= (t + saved_expires_in)
76
+ rescue StandardError
77
+ false
78
+ end
79
+
80
+ # @return [String] Reads and returns the cache data.
81
+ def load_data
82
+ File.read(data_file_path)
83
+ end
84
+
85
+ # Save the metadata for the cache.
86
+ def save_meta_data
87
+ @expires_in = self.class.default_expires_in unless expires_in
88
+
89
+ File.open(meta_file_path, 'w') do |f|
90
+ f.write(expires_in.to_s)
91
+ end
92
+
93
+ self
94
+ end
95
+
96
+ # @param str [String] Save the specified cache data
97
+ def save_data(str)
98
+ File.open(data_file_path, 'w') do |f|
99
+ f.write(str.force_encoding('utf-8'))
100
+ end
101
+
102
+ self
103
+ end
104
+
105
+ # for debug
106
+ def clear
107
+ File.delete(meta_file_path) if File.exist?(meta_file_path)
108
+ File.delete(data_file_path) if File.exist?(data_file_path)
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'nokogiri'
5
+ require 'stringio'
6
+ require 'thor'
7
+
8
+ module Manga::Tools
9
+ # CLI class
10
+ class CLI < Thor
11
+ def self.exit_on_failure?
12
+ true
13
+ end
14
+
15
+ # desc "pub {name}", "publication date of {name}"
16
+ # def pub(name)
17
+ # doc = Nokogiri::HTML(
18
+ # URI.open('https://tsutaya.tsite.jp/feature/book/release/comic/index', 'User-Agent' => UserAgent)
19
+ # )
20
+ #
21
+ # File.open('.index.txt', 'w') {|f| f.write doc }
22
+ #
23
+ # results = {}
24
+ #
25
+ # current_date = ''
26
+ # state = :title
27
+ # current_data = {}
28
+ # doc.css('h3, div.comic_list div.c_cols-1of3 span').each do |element|
29
+ # case element.name
30
+ # when 'h3'
31
+ # results[element.content] = []
32
+ # current_date = element.content
33
+ # when 'span'
34
+ # current_data[state] = element.content
35
+ # case state
36
+ # when :title
37
+ # state = :author
38
+ # when :author
39
+ # state = :label
40
+ # when :label
41
+ # state = :title
42
+ # results[current_date] << current_data
43
+ # current_data = {}
44
+ # end
45
+ # end
46
+ # end
47
+ #
48
+ # puts JSON.pretty_generate(results)
49
+ # end
50
+
51
+ desc 'search WORD', 'Search for a given WORD'
52
+ def search(word)
53
+ Manga::Tools::Cache.init
54
+
55
+ t = Time.now
56
+ url = "https://calendar.gameiroiro.com/manga.php?year=#{t.year}&month=#{t.month}"
57
+ data = Manga::Tools::Cache.fetch(key: url) do |cache|
58
+ res = Manga::Tools::Http.get(url)
59
+ cache.expires_in = Manga::Tools::Http.seconds_to_cache(res)
60
+ results = generate_internal_data(t, res.body)
61
+ results.to_json
62
+ end
63
+
64
+ hash = JSON.parse(data)
65
+
66
+ puts "Searching '#{word}' ..."
67
+ hash.each do |date, items|
68
+ next if items.empty?
69
+
70
+ items.each do |item|
71
+ puts "Found: #{date}, #{item['title']}" if item['title'].index(word)
72
+ end
73
+ end
74
+ puts 'Finished.'
75
+ end
76
+
77
+ private
78
+
79
+ # @param time [Time] a target time
80
+ # @param data [String] a string of HTTP response body.
81
+ def generate_internal_data(time, data)
82
+ doc = Nokogiri::HTML(StringIO.open(data))
83
+
84
+ results = {}
85
+ current_date = nil
86
+ state = :genre
87
+ data = {}
88
+ doc.css(targets.join(', ')).each do |element|
89
+ value = rm_spaces(element.content)
90
+ case element.name
91
+ when 'td'
92
+ current_date = format('%<ym>s/%<day>02d', ym: time.strftime('%Y/%m'), day: value.to_i)
93
+ results[current_date] = []
94
+ when 'p', 'a'
95
+ case state
96
+ when :genre
97
+ guard_genre(element)
98
+ data[state] = value
99
+ state = :title
100
+ when :title
101
+ guard_title(element)
102
+ data[state] = value
103
+ data[:link] = element['href']
104
+ state = :company
105
+ when :company
106
+ guard_company(element)
107
+ data[state] = value
108
+ state = :authors
109
+ when :authors
110
+ guard_authors(element)
111
+ data[state] = value
112
+ state = :genre
113
+ results[current_date] << data
114
+ data = {}
115
+ end
116
+ end
117
+ rescue StandardError
118
+ # when authors, authors may not be present.
119
+ if state == :authors
120
+ data[state] = ''
121
+ state = :genre
122
+ results[current_date] << data
123
+ data = {}
124
+ end
125
+ retry
126
+ end
127
+
128
+ results
129
+ end
130
+
131
+ def targets
132
+ @targets ||= [
133
+ 'td.day-td',
134
+ 'div.product-description-right p.p-genre',
135
+ 'div.product-description-right a',
136
+ 'div.product-description-right p.p-company'
137
+ ]
138
+ end
139
+
140
+ # Remove HTML spaces (&nbsp;) and white spaces.
141
+ # @param str [String] a string
142
+ def rm_spaces(str)
143
+ str.gsub("\u00A0", '').strip
144
+ end
145
+
146
+ def guard_genre(element)
147
+ raise 'invalid state' unless element.name == 'p' && element['class'] == 'p-genre'
148
+ end
149
+
150
+ def guard_title(element)
151
+ raise 'invalid state' unless element.name == 'a'
152
+ end
153
+
154
+ def guard_company(element)
155
+ raise 'invalid state' unless element.name == 'p' && element['class'] == 'p-company'
156
+ end
157
+
158
+ alias guard_authors guard_company
159
+ end
160
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'uri'
5
+
6
+ module Manga::Tools
7
+ # HTTP client class
8
+ module Http
9
+ # rubocop:disable Layout/LineLength
10
+ UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
11
+ # rubocop:enable Layout/LineLength
12
+
13
+ # @param url [String] an url string, eg: `https://example.com`
14
+ # @return [Faraday::Connection] a connection object
15
+ def self.connection(url)
16
+ @connection ||= Faraday.new(
17
+ url: url,
18
+ headers: { 'User-Agent' => UA }
19
+ ) do |f|
20
+ f.response :logger
21
+ end
22
+ end
23
+
24
+ # @param url [String] an url string, eg: `https://example.com/path/to/object`
25
+ # @return [Faraday::Response] a response object
26
+ def self.get(url)
27
+ u = URI.parse(url)
28
+ connection("#{u.scheme}://#{u.host}").get(u.request_uri)
29
+ end
30
+
31
+ # @param response [Faraday::Response] a response object
32
+ # @return [nil|Integer] number of second to cache
33
+ #
34
+ # supported http header
35
+ # - cache-control: max-age=[sec]
36
+ def self.seconds_to_cache(response)
37
+ result = response['cache-control']&.split(/,/)&.map(&:strip)&.find { |item| item =~ /max-age=(.+)/ }
38
+ return nil unless result
39
+
40
+ Regexp.last_match(1).to_i
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @private
4
+ module Manga
5
+ end
6
+
7
+ module Manga::Tools
8
+ VERSION = '0.1.0'
9
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/manga/tools/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'manga-tools'
7
+ spec.version = Manga::Tools::VERSION
8
+ spec.authors = ['Hiroki Yagita']
9
+ spec.email = ['yagihiro@gmail.com']
10
+
11
+ spec.summary = 'Tools to support the automation of daily manga-related activities.'
12
+ spec.description = 'Tools to support the automation of daily manga-related activities.'
13
+ spec.homepage = 'https://github.com/yagihiro/manga-tools'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ # spec.metadata['source_code_uri'] = 'TODO: Put your gem's public repo URL here.'
21
+ # spec.metadata['changelog_uri'] = 'TODO: Put your gem's CHANGELOG.md URL here.'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manga-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroki Yagita
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tools to support the automation of daily manga-related activities.
14
+ email:
15
+ - yagihiro@gmail.com
16
+ executables:
17
+ - manga-tools
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/ruby.yml"
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".rubocop.yml"
25
+ - ".rubocop_todo.yml"
26
+ - ".ruby-version"
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Gemfile.lock
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - exe/manga-tools
36
+ - lib/manga/tools.rb
37
+ - lib/manga/tools/cache.rb
38
+ - lib/manga/tools/cli.rb
39
+ - lib/manga/tools/http.rb
40
+ - lib/manga/tools/version.rb
41
+ - manga-tools.gemspec
42
+ homepage: https://github.com/yagihiro/manga-tools
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/yagihiro/manga-tools
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Tools to support the automation of daily manga-related activities.
66
+ test_files: []