covid_pt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 384f4f5029e16f0ec9c55f58666da61ab3f7752b5538951ded51be03f52d6a05
4
+ data.tar.gz: c035a48461b81e0d8727dec3804ee1177f5ddbb26bd84ad3d31b4252402871b9
5
+ SHA512:
6
+ metadata.gz: 75bb705d87b51e5e3dfae743d1b8ed8bbfc736ecdbb42390b9481a1ff7d9efa797c19b8f99f6bc2cbe34a5b39076e0395458dad4f960220fc8a65e4a971192f0
7
+ data.tar.gz: 186d6980168ae8dcc8e3cb5e2777532b4f21cdec1b60bd4425a2a500a81b92d739a723a304faeabd9fdc256a7385cb9cf89fd7efed4b577373940910d1f971af
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.pdf
10
+ *.gem
@@ -0,0 +1,73 @@
1
+ AllCops:
2
+ Include:
3
+ - "**/Rakefile"
4
+ - "**/config.ru"
5
+ Exclude:
6
+ - "db/**/*"
7
+ - "config/**/*"
8
+ - "script/**/*"
9
+ - "bin/**/*"
10
+ - "vendor/**/*"
11
+ - "node_modules/**/*"
12
+ - "spec/factories.rb"
13
+ TargetRubyVersion: 2.5
14
+
15
+ Layout/AlignParameters:
16
+ EnforcedStyle: with_fixed_indentation
17
+
18
+ Layout/DotPosition:
19
+ EnforcedStyle: trailing
20
+
21
+ Layout/MultilineOperationIndentation:
22
+ EnforcedStyle: indented
23
+
24
+ Layout/MultilineMethodCallIndentation:
25
+ EnforcedStyle: indented
26
+
27
+ Metrics/BlockLength:
28
+ Exclude:
29
+ - spec/**/*.rb
30
+ - lib/tasks/**/*.rake
31
+
32
+ Metrics/LineLength:
33
+ Enabled: false
34
+
35
+ Naming/FileName:
36
+ Exclude:
37
+ - "Gemfile"
38
+ - "Brewfile"
39
+
40
+ Naming/PredicateName:
41
+ NamePrefixBlacklist:
42
+ - is_
43
+ - have_
44
+
45
+ Style/ClassAndModuleChildren:
46
+ Enabled: false
47
+
48
+ Style/Documentation:
49
+ Enabled: false
50
+
51
+ Style/FrozenStringLiteralComment:
52
+ Enabled: false
53
+
54
+ Style/IfUnlessModifier:
55
+ Enabled: false
56
+
57
+ Style/NumericLiterals:
58
+ Enabled: false
59
+
60
+ Style/SingleLineBlockParams:
61
+ Enabled: false
62
+
63
+ Style/StringLiterals:
64
+ EnforcedStyle: double_quotes
65
+
66
+ Style/TrailingCommaInArguments:
67
+ EnforcedStyleForMultiline: comma
68
+
69
+ Style/TrailingCommaInArrayLiteral:
70
+ EnforcedStyleForMultiline: comma
71
+
72
+ Style/TrailingCommaInHashLiteral:
73
+ EnforcedStyleForMultiline: comma
@@ -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 fernando@mendes.codes. 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,3 @@
1
+ gemspec
2
+
3
+ gem "rake", "~> 12.0"
@@ -0,0 +1,66 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ Ascii85 (1.0.3)
5
+ afm (0.2.2)
6
+ ast (2.4.1)
7
+ domain_name (0.5.20190701)
8
+ unf (>= 0.0.5, < 1.0.0)
9
+ hashery (2.1.2)
10
+ http-accept (1.7.0)
11
+ http-cookie (1.0.3)
12
+ domain_name (~> 0.5)
13
+ mime-types (3.3.1)
14
+ mime-types-data (~> 3.2015)
15
+ mime-types-data (3.2020.0512)
16
+ netrc (0.11.0)
17
+ parallel (1.19.2)
18
+ parser (2.7.1.3)
19
+ ast (~> 2.4.0)
20
+ pdf-reader (2.4.0)
21
+ Ascii85 (~> 1.0.0)
22
+ afm (~> 0.2.1)
23
+ hashery (~> 2.0)
24
+ ruby-rc4
25
+ ttfunk
26
+ rainbow (3.0.0)
27
+ regexp_parser (1.7.1)
28
+ rest-client (2.1.0)
29
+ http-accept (>= 1.7.0, < 2.0)
30
+ http-cookie (>= 1.0.2, < 2.0)
31
+ mime-types (>= 1.16, < 4.0)
32
+ netrc (~> 0.8)
33
+ rexml (3.2.4)
34
+ rubocop (0.85.1)
35
+ parallel (~> 1.10)
36
+ parser (>= 2.7.0.1)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.7)
39
+ rexml
40
+ rubocop-ast (>= 0.0.3)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 1.4.0, < 2.0)
43
+ rubocop-ast (0.0.3)
44
+ parser (>= 2.7.0.1)
45
+ ruby-progressbar (1.10.1)
46
+ ruby-rc4 (0.1.5)
47
+ sort_alphabetical (1.1.0)
48
+ unicode_utils (>= 1.2.2)
49
+ ttfunk (1.6.2.1)
50
+ unf (0.1.4)
51
+ unf_ext
52
+ unf_ext (0.0.7.7)
53
+ unicode-display_width (1.7.0)
54
+ unicode_utils (1.4.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ pdf-reader
61
+ rest-client
62
+ rubocop
63
+ sort_alphabetical
64
+
65
+ BUNDLED WITH
66
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fernando Mendes
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,65 @@
1
+ # COVID PT
2
+
3
+ A gem to get the COVID-19 data for Portugal.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'covid_pt'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install covid_pt
20
+
21
+ ## Usage
22
+
23
+ There are 3 main functions to generate a report.
24
+
25
+ ```ruby
26
+ report = CovidPT.report("2020-07-01")
27
+ # Generates the report for the given date
28
+
29
+ report = CovidPT.between("2020-07-01", "2020-07-02")
30
+ # Compare the two dates directly
31
+
32
+ report = CovidPT.range("2020-07-01", "2020-07-05")
33
+ # Compare every date between the two given dates
34
+
35
+ # Printing a given report in different formats:
36
+
37
+ # Printing in TXT format
38
+ CovidPT::Printer.pp(report)
39
+
40
+ # Printing in Markdown format
41
+ CovidPT::Printer.md(report)
42
+
43
+ # Printing in JSON
44
+ CovidPT::Printer.json(report)
45
+ ```
46
+
47
+ There is also a command line tool available:
48
+
49
+ ```shell
50
+ $ covid_pt --date="2020-07-01" --output=md
51
+ $ covid_pt --between="2020-07-01,2020-07-02" --output=json
52
+ $ covid_pt --range="2020-07-01,2020-07-10"
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/frm/covid_pt. 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/frm/covid_pt/blob/master/CODE_OF_CONDUCT.md).
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the CovidPT project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/frm/covid_pt/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "covid_pt"
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,33 @@
1
+ require_relative 'lib/covid_pt/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "covid_pt"
5
+ spec.version = CovidPT::VERSION
6
+ spec.authors = ["Fernando Mendes"]
7
+ spec.email = ["fernando@mendes.codes"]
8
+
9
+ spec.summary = %q{COVID19 data fetcher and parser for Portugal}
10
+ spec.homepage = "https://github.com/frm/covid_pt"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/frm/covid_pt"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "sort_alphabetical"
30
+ spec.add_dependency "pdf-reader"
31
+ spec.add_dependency "rest-client"
32
+ spec.add_development_dependency "rubocop"
33
+ end
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "covid_pt"
4
+ require "optparse"
5
+
6
+ options = {output: :txt}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: covid_pt [OPTIONS]"
10
+
11
+ opts.on("-d", "--date=DATE", "Get info for a specific date") do |d|
12
+ options[:type] = :date
13
+ options[:date] = d
14
+ end
15
+
16
+ opts.on("-b", "--between=DATE1,DATE2", "Directly compare cases between two dates") do |d|
17
+ options[:type] = :comparison
18
+ dates = d.split(",")
19
+
20
+ raise OptionParser::InvalidArgument if dates.length != 2
21
+
22
+ options[:dates] = dates
23
+ end
24
+
25
+ opts.on("-r", "--range=DATE1,DATE2", "Cases for each date between the provided ones") do |d|
26
+ options[:type] = :range
27
+ dates = d.split(",")
28
+
29
+ raise OptionParser::InvalidArgument if dates.length != 2
30
+
31
+ options[:dates] = dates
32
+ end
33
+
34
+ opts.on("-o", "--output=FORMAT", "Output format (default: txt)") do |o|
35
+ output = {"txt" => :txt, "md" => :md, "json" => :json}[o]
36
+
37
+ raise OptionParser::InvalidArgument unless output
38
+
39
+ options[:output] = output.to_sym
40
+ end
41
+ end.parse!
42
+
43
+ report =
44
+ case options[:type]
45
+ when :date
46
+ CovidPT.at(options[:date])
47
+ when :comparison
48
+ CovidPT.compare(*options[:dates])
49
+ when :range
50
+ CovidPT.range(*options[:dates])
51
+ end
52
+
53
+ case options[:output]
54
+ when :txt
55
+ puts CovidPT::Printer.pp(report)
56
+ when :md
57
+ puts CovidPT::Printer.md(report)
58
+ when :json
59
+ puts CovidPT::Printer.json(report)
60
+ end
@@ -0,0 +1,62 @@
1
+ require "sort_alphabetical"
2
+ require "covid_pt/printer"
3
+ require "covid_pt/report"
4
+ require "covid_pt/utils"
5
+ require "covid_pt/version"
6
+
7
+ module CovidPT
8
+ class Error < StandardError; end
9
+
10
+ extend self
11
+
12
+ def at(date)
13
+ CovidPT::Report.new(date).generate.merge({type: "report"})
14
+ end
15
+
16
+ def compare(from, to)
17
+ report_1 = CovidPT::Report.new(from).generate
18
+ report_2 = CovidPT::Report.new(to).generate
19
+
20
+ {
21
+ overall: Utils.diff(report_2[:overall], report_1[:overall]),
22
+ regional: Utils.diff(report_2[:regional], report_1[:regional]),
23
+ dates: {
24
+ from: report_1[:date],
25
+ to: report_2[:date]
26
+ },
27
+ type: "comparison"
28
+ }
29
+ end
30
+
31
+ def range(from, to)
32
+ from_date = Date.parse(from)
33
+ to_date = Date.parse(to)
34
+
35
+ dates = (from_date..to_date).to_a
36
+ reports = (from_date..to_date).map { |d| CovidPT::Report.new(d.to_s).generate }
37
+
38
+ i = 0
39
+ acc = {overall: {}, regional: {}, dates: dates, type: "range"}
40
+
41
+ while i < reports.length - 1 do
42
+ report_1 = reports[i]
43
+ report_2 = reports[i + 1]
44
+
45
+ acc[:overall] = Utils.cumulative_diff(
46
+ report_1[:overall],
47
+ report_2[:overall],
48
+ acc[:overall]
49
+ )
50
+
51
+ acc[:regional] = Utils.cumulative_diff(
52
+ report_1[:regional],
53
+ report_2[:regional],
54
+ acc[:regional]
55
+ )
56
+
57
+ i += 1
58
+ end
59
+
60
+ acc
61
+ end
62
+ end
@@ -0,0 +1,364 @@
1
+ require "json"
2
+
3
+ module CovidPT
4
+ module Printer
5
+ extend self
6
+
7
+ def pp(report)
8
+ case report[:type]
9
+ when "report"
10
+ pp_report(report)
11
+ when "comparison"
12
+ pp_comparison(report)
13
+ when "range"
14
+ pp_range(report)
15
+ else
16
+ "Error: unknown report type"
17
+ end
18
+ end
19
+
20
+ def md(report)
21
+ case report[:type]
22
+ when "report"
23
+ md_report(report)
24
+ when "comparison"
25
+ md_comparison(report)
26
+ when "range"
27
+ md_range(report)
28
+ else
29
+ "Error: unknown report type"
30
+ end
31
+ end
32
+
33
+ def json(report)
34
+ report.to_json
35
+ end
36
+
37
+ private
38
+
39
+ def pp_report(report)
40
+ sorted_municipalities =
41
+ report[:regional].
42
+ sort_alphabetical_by(&:first).
43
+ map { |m| "#{m.first}: #{m.last}" }.
44
+ join("\n")
45
+
46
+ overall = report[:overall]
47
+
48
+ <<~TXT
49
+ ###############################################
50
+ # #
51
+ ### Relatório de Situação Nacional COVID-19 ###
52
+ # #
53
+ ###############################################
54
+
55
+ Data: #{report[:date]}
56
+
57
+ ### Situação Nacional
58
+
59
+ Total de casos suspeitos: #{overall[:total_suspect_cases]}
60
+ Total de casos confirmados: #{overall[:total_confirmed_cases]}
61
+ Total de casos não confirmados: #{overall[:total_unconfirmed_cases]}
62
+ Aguardam resultado laboratorial: #{overall[:awaiting_lab_results]}
63
+ Casos recuperados: #{overall[:total_recovered_cases]}
64
+ Óbitos: #{overall[:total_deaths]}
65
+ Total sob vigília: #{overall[:total_under_suspicion]}
66
+
67
+ ### Situação Regional
68
+
69
+ Total de Casos ARS Norte: #{overall[:cases_in_north]}
70
+ Total de Casos ARS Centro: #{overall[:cases_in_centre]}
71
+ Total de Casos ARS LVT: #{overall[:cases_in_lisbon]}
72
+ Total de Casos ARS Alentejo: #{overall[:cases_in_alentejo]}
73
+ Total de Casos ARS Algarve: #{overall[:cases_in_algarve]}
74
+ Total de Casos ARS Açores: #{overall[:cases_in_azores]}
75
+ Total de Casos ARS Madeira: #{overall[:cases_in_madeira]}
76
+
77
+ Total de Óbitos ARS Norte: #{overall[:deaths_in_north]}
78
+ Total de Óbitos ARS Centro: #{overall[:deaths_in_centre]}
79
+ Total de Óbitos ARS LVT: #{overall[:deaths_in_lisbon]}
80
+ Total de Óbitos ARS Alentejo: #{overall[:deaths_in_alentejo]}
81
+ Total de Óbitos ARS Algarve: #{overall[:deaths_in_algarve]}
82
+ Total de Óbitos ARS Açores: #{overall[:deaths_in_azores]}
83
+ Total de Óbitos ARS Madeira: #{overall[:deaths_in_madeira]}
84
+
85
+ ### Situação Municipal
86
+
87
+ #{sorted_municipalities}
88
+ TXT
89
+ end
90
+
91
+ def pp_comparison(report)
92
+ sorted_municipalities =
93
+ report[:regional].
94
+ sort_alphabetical_by(&:first).
95
+ map { |m| "#{m.first}: #{variation_to_string(m.last)}" }.
96
+ join("\n")
97
+
98
+ overall = report[:overall]
99
+
100
+ <<~TXT
101
+ ###############################################
102
+ # #
103
+ ### Relatório de Situação Nacional COVID-19 ###
104
+ # #
105
+ ###############################################
106
+
107
+ Data: #{report[:dates][:from]} - #{report[:dates][:to]}
108
+
109
+ ### Situação Nacional
110
+
111
+ Total de casos suspeitos: #{variation_to_string(overall[:total_suspect_cases])}
112
+ Total de casos confirmados: #{variation_to_string(overall[:total_confirmed_cases])}
113
+ Total de casos não confirmados: #{variation_to_string(overall[:total_unconfirmed_cases])}
114
+ Aguardam resultado laboratorial: #{variation_to_string(overall[:awaiting_lab_results])}
115
+ Casos recuperados: #{variation_to_string(overall[:total_recovered_cases])}
116
+ Óbitos: #{variation_to_string(overall[:total_deaths])}
117
+ Total sob vigília: #{variation_to_string(overall[:total_under_suspicion])}
118
+
119
+ ### Situação Regional
120
+
121
+ Total de Casos ARS Norte: #{variation_to_string(overall[:cases_in_north])}
122
+ Total de Casos ARS Centro: #{variation_to_string(overall[:cases_in_centre])}
123
+ Total de Casos ARS LVT: #{variation_to_string(overall[:cases_in_lisbon])}
124
+ Total de Casos ARS Alentejo: #{variation_to_string(overall[:cases_in_alentejo])}
125
+ Total de Casos ARS Algarve: #{variation_to_string(overall[:cases_in_algarve])}
126
+ Total de Casos ARS Açores: #{variation_to_string(overall[:cases_in_azores])}
127
+ Total de Casos ARS Madeira: #{variation_to_string(overall[:cases_in_madeira])}
128
+
129
+ Total de Óbitos ARS Norte: #{variation_to_string(overall[:deaths_in_north])}
130
+ Total de Óbitos ARS Centro: #{variation_to_string(overall[:deaths_in_centre])}
131
+ Total de Óbitos ARS LVT: #{variation_to_string(overall[:deaths_in_lisbon])}
132
+ Total de Óbitos ARS Alentejo: #{variation_to_string(overall[:deaths_in_alentejo])}
133
+ Total de Óbitos ARS Algarve: #{variation_to_string(overall[:deaths_in_algarve])}
134
+ Total de Óbitos ARS Açores: #{variation_to_string(overall[:deaths_in_azores])}
135
+ Total de Óbitos ARS Madeira: #{variation_to_string(overall[:deaths_in_madeira])}
136
+
137
+ ### Situação Municipal
138
+
139
+ #{sorted_municipalities}
140
+ TXT
141
+ end
142
+
143
+ def pp_range(report)
144
+ sorted_municipalities =
145
+ report[:regional].
146
+ sort_alphabetical_by(&:first).
147
+ map { |m| "#{m.first}: #{multiple_variations_to_string(m.last)}" }.
148
+ join("\n")
149
+
150
+ overall = report[:overall]
151
+
152
+ <<~TXT
153
+ ###############################################
154
+ # #
155
+ ### Relatório de Situação Nacional COVID-19 ###
156
+ # #
157
+ ###############################################
158
+
159
+ Datas: #{report[:dates].first} - #{report[:dates].last}
160
+
161
+ ### Situação Nacional
162
+
163
+ Total de casos suspeitos: #{multiple_variations_to_string(overall[:total_suspect_cases])}
164
+ Total de casos confirmados: #{multiple_variations_to_string(overall[:total_confirmed_cases])}
165
+ Total de casos não confirmados: #{multiple_variations_to_string(overall[:total_unconfirmed_cases])}
166
+ Aguardam resultado laboratorial: #{multiple_variations_to_string(overall[:awaiting_lab_results])}
167
+ Casos recuperados: #{multiple_variations_to_string(overall[:total_recovered_cases])}
168
+ Óbitos: #{multiple_variations_to_string(overall[:total_deaths])}
169
+ Total sob vigília: #{multiple_variations_to_string(overall[:total_under_suspicion])}
170
+
171
+ ### Situação Regional
172
+
173
+ Total de Casos ARS Norte: #{multiple_variations_to_string(overall[:cases_in_north])}
174
+ Total de Casos ARS Centro: #{multiple_variations_to_string(overall[:cases_in_centre])}
175
+ Total de Casos ARS LVT: #{multiple_variations_to_string(overall[:cases_in_lisbon])}
176
+ Total de Casos ARS Alentejo: #{multiple_variations_to_string(overall[:cases_in_alentejo])}
177
+ Total de Casos ARS Algarve: #{multiple_variations_to_string(overall[:cases_in_algarve])}
178
+ Total de Casos ARS Açores: #{multiple_variations_to_string(overall[:cases_in_azores])}
179
+ Total de Casos ARS Madeira: #{multiple_variations_to_string(overall[:cases_in_madeira])}
180
+
181
+ Total de Óbitos ARS Norte: #{multiple_variations_to_string(overall[:deaths_in_north])}
182
+ Total de Óbitos ARS Centro: #{multiple_variations_to_string(overall[:deaths_in_centre])}
183
+ Total de Óbitos ARS LVT: #{multiple_variations_to_string(overall[:deaths_in_lisbon])}
184
+ Total de Óbitos ARS Alentejo: #{multiple_variations_to_string(overall[:deaths_in_alentejo])}
185
+ Total de Óbitos ARS Algarve: #{multiple_variations_to_string(overall[:deaths_in_algarve])}
186
+ Total de Óbitos ARS Açores: #{multiple_variations_to_string(overall[:deaths_in_azores])}
187
+ Total de Óbitos ARS Madeira: #{multiple_variations_to_string(overall[:deaths_in_madeira])}
188
+
189
+ ### Situação Municipal
190
+
191
+ #{sorted_municipalities}
192
+ TXT
193
+ end
194
+
195
+ def md_report(report)
196
+ overall = report[:overall]
197
+
198
+ sorted_municipalities =
199
+ report[:regional].
200
+ sort_alphabetical_by(&:first).
201
+ map { |m| "- **#{m.first}:** #{m.last}" }.
202
+ join("\n")
203
+
204
+ <<~MD
205
+ # Relatório de Situação Nacional COVID-19
206
+
207
+ Data: #{report[:date]}
208
+
209
+ ## Situação Nacional
210
+
211
+ - **Total de casos suspeitos:** #{overall[:total_suspect_cases]}
212
+ - **Total de casos confirmados:** #{overall[:total_confirmed_cases]}
213
+ - **Total de casos não confirmados:** #{overall[:total_unconfirmed_cases]}
214
+ - **Aguardam resultado laboratorial:** #{overall[:awaiting_lab_results]}
215
+ - **Casos recuperados:** #{overall[:total_recovered_cases]}
216
+ - **Óbitos:** #{overall[:total_deaths]}
217
+ - **Total sob vigília:** #{overall[:total_under_suspicion]}
218
+
219
+ ### Situação Regional
220
+
221
+ - **Total de Casos ARS Norte**: #{overall[:cases_in_north]}
222
+ - **Total de Casos ARS Centro**: #{overall[:cases_in_centre]}
223
+ - **Total de Casos ARS LVT**: #{overall[:cases_in_lisbon]}
224
+ - **Total de Casos ARS Alentejo**: #{overall[:cases_in_alentejo]}
225
+ - **Total de Casos ARS Algarve**: #{overall[:cases_in_algarve]}
226
+ - **Total de Casos ARS Açores**: #{overall[:cases_in_azores]}
227
+ - **Total de Casos ARS Madeira**: #{overall[:cases_in_madeira]}
228
+
229
+ - **Total de Óbitos ARS Norte**: #{overall[:deaths_in_north]}
230
+ - **Total de Óbitos ARS Centro**: #{overall[:deaths_in_centre]}
231
+ - **Total de Óbitos ARS LVT**: #{overall[:deaths_in_lisbon]}
232
+ - **Total de Óbitos ARS Alentejo**: #{overall[:deaths_in_alentejo]}
233
+ - **Total de Óbitos ARS Algarve**: #{overall[:deaths_in_algarve]}
234
+ - **Total de Óbitos ARS Açores**: #{overall[:deaths_in_azores]}
235
+ - **Total de Óbitos ARS Madeira**: #{overall[:deaths_in_madeira]}
236
+
237
+ ## Situação Municipal
238
+
239
+ #{sorted_municipalities}
240
+ MD
241
+ end
242
+
243
+ def md_comparison(report)
244
+ overall = report[:overall]
245
+
246
+ sorted_municipalities =
247
+ report[:regional].
248
+ sort_alphabetical_by(&:first).
249
+ map { |m| "- **#{m.first}:** #{variation_to_string(m.last)}" }.
250
+ join("\n")
251
+
252
+ <<~MD
253
+ # Relatório de Situação Nacional COVID-19
254
+
255
+ Data: #{report[:dates][:from]} - #{report[:dates][:to]}
256
+
257
+ ## Situação Nacional
258
+
259
+ - **Total de casos suspeitos:** #{variation_to_string(overall[:total_suspect_cases])}
260
+ - **Total de casos confirmados:** #{variation_to_string(overall[:total_confirmed_cases])}
261
+ - **Total de casos não confirmados:** #{variation_to_string(overall[:total_unconfirmed_cases])}
262
+ - **Aguardam resultado laboratorial:** #{variation_to_string(overall[:awaiting_lab_results])}
263
+ - **Casos recuperados:** #{variation_to_string(overall[:total_recovered_cases])}
264
+ - **Óbitos:** #{variation_to_string(overall[:total_deaths])}
265
+ - **Total sob vigília:** #{variation_to_string(overall[:total_under_suspicion])}
266
+
267
+ ### Situação Regional
268
+
269
+ - **Total de Casos ARS Norte**: #{overall[:cases_in_north]}
270
+ - **Total de Casos ARS Centro**: #{overall[:cases_in_centre]}
271
+ - **Total de Casos ARS LVT**: #{overall[:cases_in_lisbon]}
272
+ - **Total de Casos ARS Alentejo**: #{overall[:cases_in_alentejo]}
273
+ - **Total de Casos ARS Algarve**: #{overall[:cases_in_algarve]}
274
+ - **Total de Casos ARS Açores**: #{overall[:cases_in_azores]}
275
+ - **Total de Casos ARS Madeira**: #{overall[:cases_in_madeira]}
276
+
277
+ - **Total de Óbitos ARS Norte**: #{overall[:deaths_in_north]}
278
+ - **Total de Óbitos ARS Centro**: #{overall[:deaths_in_centre]}
279
+ - **Total de Óbitos ARS LVT**: #{overall[:deaths_in_lisbon]}
280
+ - **Total de Óbitos ARS Alentejo**: #{overall[:deaths_in_alentejo]}
281
+ - **Total de Óbitos ARS Algarve**: #{overall[:deaths_in_algarve]}
282
+ - **Total de Óbitos ARS Açores**: #{overall[:deaths_in_azores]}
283
+ - **Total de Óbitos ARS Madeira**: #{overall[:deaths_in_madeira]}
284
+
285
+ ## Situação Municipal
286
+
287
+ #{sorted_municipalities}
288
+ MD
289
+ end
290
+
291
+ def md_range(report)
292
+ overall = report[:overall]
293
+
294
+ sorted_municipalities =
295
+ report[:regional].
296
+ sort_alphabetical_by(&:first).
297
+ map { |m| "- **#{m.first}:** #{multiple_variations_to_string(m.last)}" }.
298
+ join("\n")
299
+
300
+ <<~MD
301
+ # Relatório de Situação Nacional COVID-19
302
+
303
+ Datas: #{report[:dates].first} - #{report[:dates].last}
304
+
305
+ ## Situação Nacional
306
+
307
+ - **Total de casos suspeitos:** #{multiple_variations_to_string(overall[:total_suspect_cases])}
308
+ - **Total de casos confirmados:** #{multiple_variations_to_string(overall[:total_confirmed_cases])}
309
+ - **Total de casos não confirmados:** #{multiple_variations_to_string(overall[:total_unconfirmed_cases])}
310
+ - **Aguardam resultado laboratorial:** #{multiple_variations_to_string(overall[:awaiting_lab_results])}
311
+ - **Casos recuperados:** #{multiple_variations_to_string(overall[:total_recovered_cases])}
312
+ - **Óbitos:** #{multiple_variations_to_string(overall[:total_deaths])}
313
+ - **Total sob vigília:** #{multiple_variations_to_string(overall[:total_under_suspicion])}
314
+
315
+ ### Situação Regional
316
+
317
+ - **Total de Casos ARS Norte**: #{overall[:cases_in_north]}
318
+ - **Total de Casos ARS Centro**: #{overall[:cases_in_centre]}
319
+ - **Total de Casos ARS LVT**: #{overall[:cases_in_lisbon]}
320
+ - **Total de Casos ARS Alentejo**: #{overall[:cases_in_alentejo]}
321
+ - **Total de Casos ARS Algarve**: #{overall[:cases_in_algarve]}
322
+ - **Total de Casos ARS Açores**: #{overall[:cases_in_azores]}
323
+ - **Total de Casos ARS Madeira**: #{overall[:cases_in_madeira]}
324
+
325
+ - **Total de Óbitos ARS Norte**: #{overall[:deaths_in_north]}
326
+ - **Total de Óbitos ARS Centro**: #{overall[:deaths_in_centre]}
327
+ - **Total de Óbitos ARS LVT**: #{overall[:deaths_in_lisbon]}
328
+ - **Total de Óbitos ARS Alentejo**: #{overall[:deaths_in_alentejo]}
329
+ - **Total de Óbitos ARS Algarve**: #{overall[:deaths_in_algarve]}
330
+ - **Total de Óbitos ARS Açores**: #{overall[:deaths_in_azores]}
331
+ - **Total de Óbitos ARS Madeira**: #{overall[:deaths_in_madeira]}
332
+
333
+ ## Situação Municipal
334
+
335
+ #{sorted_municipalities}
336
+ MD
337
+ end
338
+
339
+ def multiple_variations_to_string(variation)
340
+ variation[:values].each_with_index.inject("") do |acc, (value, idx)|
341
+ if idx.zero?
342
+ value.to_s
343
+ else
344
+ difference = variation[:differences][idx - 1]
345
+ formatted_difference = diff_to_string(difference)
346
+
347
+ acc + ", #{value} (#{formatted_difference})"
348
+ end
349
+ end
350
+ end
351
+
352
+ def variation_to_string(variation)
353
+ "#{variation[:value]} (#{diff_to_string(variation[:difference])})"
354
+ end
355
+
356
+ def diff_to_string(number)
357
+ if number.negative?
358
+ number.to_s
359
+ else
360
+ "+#{number}"
361
+ end
362
+ end
363
+ end
364
+ end
@@ -0,0 +1,151 @@
1
+ require "rest-client"
2
+ require "pdf-reader"
3
+ require "nokogiri"
4
+
5
+ module CovidPT
6
+ class Report
7
+ MUNICIPALITY_EDGE_CASES = {
8
+ "Monsaraz" => "Reguengos de Monsaraz",
9
+ "Graciosa" => "Santa Cruz da Graciosa",
10
+ "Penaguião" => "Santa Marta de Penaguião",
11
+ "António" => "Vila Real de Santo António",
12
+ }.freeze
13
+
14
+ def initialize(date)
15
+ @date = Date.parse(date)
16
+ rescue ArgumentError => e
17
+ puts e.message
18
+ puts "Argument: #{date}"
19
+ end
20
+
21
+ def generate
22
+ return @report if @report
23
+
24
+ @report = {
25
+ overall: overall_report,
26
+ regional: regional_report,
27
+ date: @date
28
+ }
29
+
30
+ cleanup
31
+
32
+ @report
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :date, :pdf
38
+
39
+ def overall_report
40
+ numbers_in_pdf = first_page.text.scan(/\d+/)
41
+
42
+ # numbers_in_pdf[0] is the 19 from COVID-19
43
+ # numbers_in_pdf[9] is the 1 from "1 de janeiro 2020"
44
+ # numbers_in_pdf[11] is the 2020 from "1 de janeiro 2020"
45
+ {
46
+ cases_in_north: numbers_in_pdf[1].to_i,
47
+ deaths_in_north: numbers_in_pdf[2].to_i,
48
+ cases_in_azores: numbers_in_pdf[3].to_i,
49
+ deaths_in_azores: numbers_in_pdf[4].to_i,
50
+ cases_in_centre: numbers_in_pdf[5].to_i,
51
+ deaths_in_centre: numbers_in_pdf[6].to_i,
52
+ cases_in_madeira: numbers_in_pdf[7].to_i,
53
+ deaths_in_madeira: numbers_in_pdf[8].to_i,
54
+ total_suspect_cases: numbers_in_pdf[10].to_i,
55
+ cases_in_lisbon: numbers_in_pdf[12].to_i,
56
+ deaths_in_lisbon: numbers_in_pdf[13].to_i,
57
+ total_confirmed_cases: numbers_in_pdf[14].to_i,
58
+ total_unconfirmed_cases: numbers_in_pdf[15].to_i,
59
+ cases_in_alentejo: numbers_in_pdf[16].to_i,
60
+ deaths_in_alentejo: numbers_in_pdf[17].to_i,
61
+ awaiting_lab_results: numbers_in_pdf[18].to_i,
62
+ cases_in_algarve: numbers_in_pdf[19].to_i,
63
+ deaths_in_algarve: numbers_in_pdf[20].to_i,
64
+ total_recovered_cases: numbers_in_pdf[21].to_i,
65
+ total_deaths: numbers_in_pdf[22].to_i,
66
+ total_under_suspicion: numbers_in_pdf[23].to_i,
67
+ }
68
+ end
69
+
70
+ def regional_report
71
+ municipalities =
72
+ third_page.
73
+ text.
74
+ sub(/\A.+(Abrantes)/m, "\\1").
75
+ sub(/(Vouzela\s+\d+).*\z/m, "\\1").
76
+ gsub(/(\d)\s+/, "\\1\n").
77
+ gsub(/\*/, "").
78
+ split("\n").
79
+ map { |s| s.split(/\s{2,}/).reject(&:empty?) }.
80
+ reject { |m| m.length < 2 }.
81
+ map { |m| m.last(2) }.
82
+ map { |m| [m.first, m.last.to_i] }.
83
+ to_h
84
+
85
+ handle_municipality_edge_cases(municipalities)
86
+ end
87
+
88
+ def handle_municipality_edge_cases(municipalities)
89
+ MUNICIPALITY_EDGE_CASES.map do |k, v|
90
+ municipalities[v] = municipalities.delete(k)
91
+ end
92
+
93
+ municipalities
94
+ end
95
+
96
+ def first_page
97
+ pdf.pages.first
98
+ end
99
+
100
+ def third_page
101
+ pdf.pages[2]
102
+ end
103
+
104
+ def pdf
105
+ @_pdf ||= PDF::Reader.new(download_pdf)
106
+ end
107
+
108
+ def download_pdf
109
+ response = RestClient.get(pdf_url)
110
+
111
+ File.write(filename, response.body)
112
+
113
+ filename
114
+ end
115
+
116
+ def filename
117
+ formatted_date = date.strftime("%Y%m%d")
118
+
119
+ "#{day_identifier}_DGS_boletim_#{formatted_date}.pdf"
120
+ end
121
+
122
+ def pdf_url
123
+ return @_pdf_url if @_pdf_url
124
+
125
+ response = RestClient.get("https://covid19.min-saude.pt/relatorio-de-situacao/")
126
+ doc = Nokogiri::HTML(response.body)
127
+
128
+ li = doc.css("#content_easy > div.single_content > ul", "li").children.select do |li|
129
+ li.content.match(/#{@date.strftime("%d/%m/%Y")}/)
130
+ end.first
131
+
132
+ @_pdf_url = li.css("a").first.attr("href")
133
+ end
134
+
135
+
136
+ # Calculate the numerical identifier of the report
137
+ # based on the 01/06/2020 report, which was #91
138
+ def day_identifier
139
+ day_baseline = Date.parse("2020-06-01")
140
+ number_baseline = 91
141
+
142
+ baseline_shift = date.mjd - day_baseline.mjd
143
+
144
+ number_baseline + baseline_shift
145
+ end
146
+
147
+ def cleanup
148
+ File.delete(filename)
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,29 @@
1
+ module CovidPT
2
+ module Utils
3
+ extend self
4
+
5
+ def diff(a, b)
6
+ a.inject({}) do |acc, (k, v)|
7
+ b_value = b[k] || 0
8
+ a_value = v || 0
9
+
10
+ acc[k] = { value: v, difference: a_value - b_value }
11
+ acc
12
+ end
13
+ end
14
+
15
+ def cumulative_diff(a, b, start)
16
+ a.inject(start) do |acc, (key, value)|
17
+ b_value = b[key] || 0
18
+ a_value = value || 0
19
+
20
+ acc[key] ||= {values: [a_value], differences: []}
21
+
22
+ new_entries = {values: [b_value], differences: [b_value - a_value]}
23
+
24
+ acc[key].merge(new_entries) { |_, old, new| old.concat(new) }
25
+ acc
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module CovidPT
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: covid_pt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Mendes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sort_alphabetical
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pdf-reader
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - fernando@mendes.codes
72
+ executables:
73
+ - covid_pt
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - covid_pt.gemspec
88
+ - exe/covid_pt
89
+ - lib/covid_pt.rb
90
+ - lib/covid_pt/printer.rb
91
+ - lib/covid_pt/report.rb
92
+ - lib/covid_pt/utils.rb
93
+ - lib/covid_pt/version.rb
94
+ homepage: https://github.com/frm/covid_pt
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/frm/covid_pt
99
+ source_code_uri: https://github.com/frm/covid_pt
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.3.0
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.7.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: COVID19 data fetcher and parser for Portugal
120
+ test_files: []