tsukuba-gc 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
+ SHA1:
3
+ metadata.gz: 4b8af5f871451a89f124b405108e154ae139f3af
4
+ data.tar.gz: 5abe1260211da023a99f7e27f6395bf0e5714afa
5
+ SHA512:
6
+ metadata.gz: ea3c820859715d93149e236e1d2555ebbb5584bae542f72fdcb0842ea2f4d297f4f7c09d1d392d63bcf77e3409dcdcbce20fe860bed6cea75964caa4c79e5df9
7
+ data.tar.gz: b71022cca9d4a4debf8651ea96229fadfb56072774fa2f07105fc3e52b76407300efafa92044531c33107cb5fb86fc687c81fcf8c09a1355de7df62640d64d75
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.pdf
11
+ *.json
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tsukuba-gc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 SAKATA Sinji
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # tsukuba-gc
2
+
3
+ つくば市がPDFで配布しているごみ収集カレンダーを解析して、JSON形式で出力する
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tsukuba-gc'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tsukuba-gc
20
+
21
+ ## Usage
22
+
23
+ ### Example CLI
24
+
25
+ $ tsukuba-gc 28north.pdf 2016 -o 28north.json
26
+
27
+ Type "tsukuba-gc –-help" for all available options.
28
+
29
+ ### Example Library
30
+
31
+ ```ruby
32
+ require 'tsukuba-gc'
33
+
34
+ Tsukuba::GC.parse(2016, "28north.pdf") #=> [schedule, ...]
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/NKMR6194/tsukuba-gc.
46
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tsukuba/gc"
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,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "tsukuba-gc"
4
+
5
+ Tsukuba::GC::CLI.run
@@ -0,0 +1 @@
1
+ require "tsukuba/gc"
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rmagick"
4
+ require "tsukuba/gc/version"
5
+ require "tsukuba/gc/calendar"
6
+ require "tsukuba/gc/cli"
7
+
8
+ module Tsukuba
9
+ module GC
10
+ module_function
11
+
12
+ def parse(year, file_path)
13
+ pages = Magick::Image.read(file_path)
14
+ time = Time.new(year, 4)
15
+ schedules = []
16
+
17
+ 12.times do |i|
18
+ calendar = Calendar.new(time.year, time.month, pages[(i/4) + 1])
19
+ schedules.concat(calendar.schedules)
20
+ time = time.next_month
21
+ end
22
+
23
+ return schedules
24
+ end
25
+
26
+ def generate_json_object(schedules)
27
+ schedules.map { |schedule| {date: schedule.time.to_s, type: schedule.type_ja} }
28
+ end
29
+
30
+ def dump_json(schedules, io = nil)
31
+ obj = generate_json_object(schedules)
32
+ json_str = JSON.pretty_generate(obj) + "\n"
33
+
34
+ if io.nil?
35
+ json_str
36
+ else
37
+ io.write(json_str)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext"
5
+ require "tsukuba/gc/rgb"
6
+ require "tsukuba/gc/schedule"
7
+
8
+ module Tsukuba
9
+ module GC
10
+ class Calendar
11
+ def initialize(year, month, img)
12
+ @img = img
13
+ @time = Time.new(year, month, 1)
14
+
15
+ # base pixel
16
+ @x = 143
17
+ @y = 122
18
+
19
+ # size of a cell
20
+ @dx = 84
21
+ @dy = 50
22
+
23
+ # focus to a calendar
24
+ if (month % 4) % 2 == 1
25
+ # shift down
26
+ @y += 314
27
+ end
28
+ if (month % 4) >= 2
29
+ # shift right
30
+ @x += 560
31
+ end
32
+
33
+ # when 5 weeks in a month
34
+ if @time.end_of_month.day == 31 && (@time.saturday? || @time.friday?)
35
+ @dy = 44
36
+ end
37
+
38
+ # focus to a cell of day 1
39
+ if @time.saturday?
40
+ @y += 25
41
+ elsif @time.sunday?
42
+ # skip
43
+ else
44
+ @x += @dx * (@time.wday - 1)
45
+ end
46
+ end
47
+
48
+ def schedules
49
+ return @schedules if @schedules.present?
50
+
51
+ @schedules = []
52
+
53
+ @time.end_of_month.day.times do
54
+ if @time.sunday? || @time.saturday?
55
+ @schedules << Schedule.new(@time, :none)
56
+ @time = @time.next_day
57
+ next
58
+ end
59
+
60
+ pixel = @img.get_pixels(@x, @y, 1, 1)[0]
61
+ rgb = RGB.new(pixel.red / 257, pixel.green / 257, pixel.blue / 257)
62
+
63
+ @schedules << Schedule.new(@time, rgb.type)
64
+
65
+ if @time.friday?
66
+ @y += @dy
67
+ @x -= @dx * 4
68
+ else
69
+ @x += @dx
70
+ end
71
+ @time = @time.next_day
72
+ end
73
+
74
+ return @schedules
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "tsukuba/gc"
5
+
6
+ module Tsukuba
7
+ module GC
8
+ module CLI
9
+ BANNER = "Usage: tsukuba-gc [options] file year"
10
+
11
+ module_function
12
+
13
+ def run(argv = ARGV)
14
+ output = nil
15
+ opt = OptionParser.new(BANNER)
16
+ opt.version = Tsukuba::GC::VERSION
17
+ opt.separator("Options:")
18
+ opt.on("-o <file>", "結果を<file>に出力する") { |v| output = v }
19
+ opt.on_tail("-h", "--help", "ヘルプを出力する") do
20
+ puts opt
21
+ exit
22
+ end
23
+
24
+ opt.on_tail("--version", "バージョンを表示する") do
25
+ puts opt.ver
26
+ exit
27
+ end
28
+
29
+ argv = opt.parse(argv)
30
+
31
+ if argv.length < 2
32
+ puts "Error: 引数が不足しています"
33
+ puts
34
+ puts opt.help
35
+ exit(1)
36
+ end
37
+
38
+ file_path = argv[0]
39
+ year = argv[1].to_i
40
+
41
+ schedules = Tsukuba::GC.parse(year, file_path)
42
+ if output.present?
43
+ open(output, "w") do |f|
44
+ Tsukuba::GC.dump_json(schedules, f)
45
+ end
46
+ else
47
+ Tsukuba::GC.dump_json(schedules, STDOUT)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tsukuba
4
+ module GC
5
+ class RGB
6
+ attr_reader :r, :g, :b
7
+
8
+ def initialize(red, green, blue)
9
+ @r = red
10
+ @g = green
11
+ @b = blue
12
+ end
13
+
14
+ COLORS = {
15
+ none: RGB.new(255, 255, 255),
16
+ burnable: RGB.new(255, 137, 171),
17
+ used_paper: RGB.new(0, 130, 70),
18
+ glass: RGB.new(234, 241, 86),
19
+ spray: RGB.new(255, 168, 42),
20
+ pet: RGB.new(146, 218, 229),
21
+ oversized: RGB.new(193, 180, 135),
22
+ cans: RGB.new(212, 213, 214),
23
+ non_burnale: RGB.new(37, 83, 159),
24
+ }.freeze
25
+
26
+ def type
27
+ kind = nil
28
+ min_dist = nil
29
+
30
+ COLORS.each do |key, rgb|
31
+ d = dist(rgb)
32
+ if min_dist.nil? || min_dist > d
33
+ min_dist = d
34
+ kind = key
35
+ end
36
+ end
37
+
38
+ return kind
39
+ end
40
+
41
+ private
42
+
43
+ def dist(rgb)
44
+ dr = @r - rgb.r
45
+ dg = @g - rgb.g
46
+ db = @b - rgb.b
47
+ return dr * dr + dg * dg + db * db
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tsukuba
4
+ module GC
5
+ class Schedule
6
+ TYPES = {
7
+ none: "収集なし",
8
+ burnable: "燃やせるごみ",
9
+ used_paper: "古紙・古衣",
10
+ glass: "びん・スプレー容器",
11
+ spray: "びん・スプレー容器",
12
+ pet: "ペットボトル",
13
+ oversized: "粗大ごみ",
14
+ cans: "かん",
15
+ non_burnale: "燃やせないごみ",
16
+ }.freeze
17
+
18
+ attr_reader :time, :type
19
+
20
+ def initialize(time, type)
21
+ @time = time.clone.freeze
22
+ @type = type
23
+ end
24
+
25
+ def type_ja
26
+ TYPES[@type]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Tsukuba
2
+ module GC
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -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 'tsukuba/gc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tsukuba-gc"
8
+ spec.version = Tsukuba::GC::VERSION
9
+ spec.authors = ["SAKATA Sinji"]
10
+ spec.email = ["sinji@sakasin.net"]
11
+
12
+ spec.summary = "Scan the schedule of garbage collection in Tsukuba from PDF."
13
+ spec.description = "Scan the schedule of garbage collection in Tsukuba from PDF."
14
+ spec.homepage = "https://github.com/NKMR6194/tsukuba-gc"
15
+ spec.licenses = ["MIT"]
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.2.0")
25
+
26
+ spec.add_dependency "rmagick", "~>2.16.0"
27
+ spec.add_dependency "activesupport", "~>5.0.1"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.13"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsukuba-gc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SAKATA Sinji
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rmagick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.16.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.16.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Scan the schedule of garbage collection in Tsukuba from PDF.
70
+ email:
71
+ - sinji@sakasin.net
72
+ executables:
73
+ - tsukuba-gc
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - exe/tsukuba-gc
85
+ - lib/tsukuba-gc.rb
86
+ - lib/tsukuba/gc.rb
87
+ - lib/tsukuba/gc/calendar.rb
88
+ - lib/tsukuba/gc/cli.rb
89
+ - lib/tsukuba/gc/rgb.rb
90
+ - lib/tsukuba/gc/schedule.rb
91
+ - lib/tsukuba/gc/version.rb
92
+ - tsukuba-gc.gemspec
93
+ homepage: https://github.com/NKMR6194/tsukuba-gc
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 2.2.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.5.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Scan the schedule of garbage collection in Tsukuba from PDF.
117
+ test_files: []