-
Notifications
You must be signed in to change notification settings - Fork 0
/
coma.cr
110 lines (93 loc) · 2.46 KB
/
coma.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# @author Jinzulen
# @license Apache 2.0
# @copyright Copyright 2022 Khalil G. <https://github.com/Jinzulen>
require "crystagiri"
require "http/client"
require "option_parser"
# Version
VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}
OptionParser.parse do |parser|
# Help dialog.
parser.on "-h", "--help", "Show the help dialog." do
puts parser
exit(1)
end
# Show version.
parser.on "-v", "--version", "Show program version." do
puts "# CoMa v#{VERSION}"
puts "# Check for an updated version over @ https://github.com/Jinzulen/CoMa"
exit(1)
end
# Grab the latest statistics.
parser.on "-g", "--get", "Grab the latest statistics." do
# Header
puts "# CoMa v#{VERSION} by Jinzulen."
puts "# Licensed under Apache 2.0"
puts "# Copyright (C) 2022 Khalil G.\n\n"
begin
# Gateway.
request_gateway : String = "http://www.covidmaroc.ma/Pages/Accueilfr.aspx"
# Headers.
request_headers = HTTP::Headers {
"User-Agent" => "Jinzulen/CoMa"
}
# Send GET request.
HTTP::Client.get(request_gateway, headers: request_headers) do |res|
# Handle an erroneous request.
if !res.success?
STDERR.puts "# [COMA] <#{res.status_code}> #{res.status_message}"
exit(1)
else
# Start scraping.
res_html = res.body_io.gets_to_end
res_docu = Crystagiri::HTML.new res_html
# Data stores.
fin_store = Array(String).new
res_store = Array(String).new
# Labels store.
res_labels = [
"Tests",
"Confirmed",
"Recovered",
"Deceased",
"Dose #1",
"Dose #2",
"Dose #3",
]
# Grab the data.
res_docu.where_tag("tr > td > p > span") do |b|
# Get rid of white space.
["\u0020", "\u200B", "\u00A0"].each do |o|
b.node.content = b.node.content.gsub(o, "")
end
# Push the content of the individual nodes
# into the temp. store.
res_store.push(b.node.content)
end
# Process the data.
res_store.delete_at(0)
res_store.each do |t|
if t.size > 1
# Format.
t = t.to_i.format(".", ",")
# Push.
fin_store.push(t)
end
end
# Publish the data.
fin_store.each_index do |n|
puts "# [COMA: Statistics] #{res_labels[n]}: #{fin_store[n]}"
end
end
end
rescue ex
STDERR.puts ex.message
exit(1)
end
end
# Errors.
parser.invalid_option do |f|
STDERR.puts "# [COMA] <#{f}> is not a recognized flag."
exit(1)
end
end