logo

scripts

A bunch of scripts, some to be moved to their own repository git clone https://anongit.hacktivis.me/git/scripts.git/

monecowatt.pm (1309B)


  1. #!/usr/bin/env perl
  2. # J'emmerde les données dites "publiques" avec une API verrouillée.
  3. # Copyright © 2022 Haelwenn (lanodan) Monnier <contact+monecowatt.fr@hacktivis.me>
  4. # SPDX-License-Identifier: AGPL-3-only
  5. use strict;
  6. use utf8;
  7. use HTML::TreeBuilder;
  8. use HTML::TreeBuilder::XPath;
  9. use LWP::UserAgent;
  10. use URI;
  11. my $ua = LWP::UserAgent->new;
  12. $ua->agent('Monécowatt scrapper, HTML reste la seule API ouverte <contact+monecowatt.fr@hacktivis.me>');
  13. sub scrap_html_response {
  14. my ($res) = @_;
  15. my $tree = HTML::TreeBuilder::XPath->new_from_content($res->content) or die "HTML parsing failed";
  16. foreach($tree->findnodes('//*[@id="previsions"]/div[1]/div/div')) {
  17. my $jour = $_->findvalue('div');
  18. $jour =~ s@^ *@@;
  19. my $status = $_->findvalue('div/img/@src');
  20. $status =~ s@/courbes-signaux/courbe-signal-([^\.\-]*).png@\1@;
  21. print $jour, ": ", $status, "\n";
  22. }
  23. }
  24. my $req = HTTP::Request->new(GET => "https://www.monecowatt.fr/");
  25. my $res = $ua->request($req);
  26. if($res->is_success) {
  27. my $content_type = $res->header("Content-Type");
  28. if(($content_type == "text/html") or ($content_type == "application/xhtml+xml")) {
  29. scrap_html_response($res);
  30. } else {
  31. print "La réponse reçue n'est pas de l'HTML\n";
  32. }
  33. } else {
  34. print "Erreur ", $res->status_line, " obtenue au lieu de 2xx\n";
  35. }