aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Étienne Deparis 2020-08-07 08:43:32 +0200
committerGravatar Étienne Deparis 2020-08-07 08:43:32 +0200
commit9e99e7d1b39062b01c6a7d08f6ec63be0d1d0b88 (patch)
tree4d9269e6a17823b85d41c7209ddf22b30a392c1e
parente0cf064e6a125f53b55baecae4a22bfb5722c06c (diff)
downloadshell_utils-9e99e7d1b39062b01c6a7d08f6ec63be0d1d0b88.tar.gz
shell_utils-9e99e7d1b39062b01c6a7d08f6ec63be0d1d0b88.tar.xz
shell_utils-9e99e7d1b39062b01c6a7d08f6ec63be0d1d0b88.zip
feat(poubelle.rb): Add a script to fetch data about my garbage collect
-rw-r--r--poubelles.rb140
1 files changed, 140 insertions, 0 deletions
diff --git a/poubelles.rb b/poubelles.rb
new file mode 100644
index 0000000..438e8f1
--- /dev/null
+++ b/poubelles.rb
@@ -0,0 +1,140 @@
+# frozen_string_literal: true
+
+begin
+ require 'curb'
+ require 'nokogiri'
+ require 'optparse'
+rescue LoadError
+ warn 'curb, nokogiri et optparse sont requis.'
+ exit 1
+end
+
+class Poubelle
+ def initialize(login, password)
+ login_uri = 'https://cc-grandlieu.webusager.fr/ccgrandlieu/pages/p_connexion.aspx'
+
+ # Init request with cookies
+ req = Curl.get(login_uri) { |c| c.enable_cookies = true }
+ dom = Nokogiri::HTML(req.body_str)
+
+ args = {
+ 'ctl00$cph_contenu$TB_IDENTIFIANT' => login,
+ 'ctl00$cph_contenu$TB_MOT_DE_PASSE' => password,
+ 'ctl00$cph_contenu$HF_BLOC_ACTIF' => 'CONNEXION',
+ 'ctl00$cph_contenu$HF_REDIRECTION' => '',
+ 'ctl00$cph_contenu$ctl01$TB_REFERENCE' => '',
+ 'ctl00$cph_contenu$ctl01$TB_CLIENT' => '',
+ 'ctl00$cph_contenu$ctl01$incription$TB_IDENTIFIANT_CREATION' => '',
+ 'ctl00$cph_contenu$ctl01$incription$TB_MDP_CREATION' => '',
+ 'ctl00$cph_contenu$ctl01$incription$TB_CMDP_CREATION' => '',
+ 'ctl00$cph_contenu$ctl01$incription$TB_CAPTCHA' => '',
+ '__EVENTTARGET' => 'ctl00$cph_contenu$IB_CONNEXION',
+ '__LASTFOCUS' => '',
+ '__SCROLLPOSITIONX' => '0',
+ '__SCROLLPOSITIONY' => '0'
+ }
+
+ ['__EVENTARGUMENT', '__VIEWSTATE', '__VIEWSTATEGENERATOR',
+ '__EVENTVALIDATION'].each do |name|
+ elem = dom.xpath('//input[@id="' + name + '"]')
+ if elem.empty?
+ args[name] = ''
+ else
+ args[name] = dom.css('#' + name)[0].attr('value')
+ end
+ end
+ header_lines = Curl.post(login_uri, args).header_str.split("\r\n")
+ status = header_lines.shift
+ headers = Hash[header_lines.map { |h| h.split(': ') }]
+ @connected = (status == 'HTTP/1.1 302 Found' \
+ && headers['Location'] == '/ccgrandlieu/pages/p_accueil.aspx')
+ end
+
+ def depots_csv
+ puts 'Date,Heure,Unité de collecte,Carte,Type de dépôt,Quantité,Statut'
+ depots_data.each { |l| puts l.join(',') }
+ end
+
+ def levees_csv
+ puts 'Flux,Type,Date,Nb Levée(s),Identification'
+ levees_data.each { |l| puts l.join(',') }
+ end
+
+ def depots_nice
+ depots_data.each do |l|
+ puts "- Dépôt à #{l[2]} le #{l[0]} à #{l[1]} (carte #{l[3]})"
+ end
+ end
+
+ def levees_nice
+ levees_data.each do |l|
+ type = l[1] == '120DMR' && 'grise' || 'jaune'
+ if ['0', '1'].include?(l[3])
+ type = "#{l[3]} poubelle #{type}"
+ else
+ type = "#{l[3]} poubelles #{type}s"
+ end
+ puts "- #{type} le #{l[2]}"
+ end
+ end
+
+ def connected
+ @connected
+ end
+
+ private
+
+ def depots_data
+ depot_uri = 'https://cc-grandlieu.webusager.fr/ccgrandlieu/pages/p_depot.aspx'
+ depot_content = Curl.get(depot_uri).body_str
+ dom = Nokogiri::HTML(depot_content)
+ dom.css('table.erpstyx_mes_depots_table tr').map do |tr|
+ data = tr.css('td').map(&:content)
+ next if data.length != 6
+ timestamp = data.shift
+ timestamp.split + data
+ end.compact.sort do |a, b|
+ tsa = a[0].split('/').reverse.join + a[1].delete(':')
+ tsb = b[0].split('/').reverse.join + b[1].delete(':')
+ tsa <=> tsb
+ end
+ end
+
+ def levees_data
+ depot_uri = 'https://cc-grandlieu.webusager.fr/ccgrandlieu/pages/p_levee.aspx'
+ depot_content = Curl.get(depot_uri).body_str
+ dom = Nokogiri::HTML(depot_content)
+ dom.css('table.erpstyx_mes_levees_table tr').map do |tr|
+ data = tr.css('td').map(&:content).map(&:strip)
+ next if data.length != 11
+ [data[0], data[6], data[1], data[5], data[9]]
+ end.compact.sort do |a, b|
+ a[2].split('/').reverse.join <=> b[2].split('/').reverse.join
+ end
+ end
+end
+
+params = {}
+OptionParser.new do |opts|
+ opts.on('-uEMAIL', '--user=EMAIL', 'Email à utiliser pour la connexion')
+ opts.on('-pPASS', '--pass=PASS', 'Mot de passe à utiliser pour la connexion')
+ opts.on('--csv', 'Sort les données en CSV')
+end.parse!(into: params)
+
+action = (/d[eé]p[oô]ts?/i =~ ARGV[0] && 'depots') || 'levees'
+
+unless params[:user] && params[:pass]
+ warn 'Il faut spécifier un email et un mot de passe'
+ exit 1
+end
+
+po = Poubelle.new(params[:user], params[:pass])
+if po.connected
+ if params[:csv]
+ po.send("#{action}_csv")
+ else
+ po.send("#{action}_nice")
+ end
+else
+ warn 'Error while connecting'
+end