#2 Simplification du code

Closed
theo wants to merge 1 commits from theo/master into vigdis/master
3 changed files with 72 additions and 276 deletions
  1. 0 65
      feed.ksh
  2. 72 0
      fetch_fai_info.pl
  3. 0 211
      json-file.pl

+ 0 - 65
feed.ksh

@@ -1,65 +0,0 @@
-#!/bin/sh
-
-# Copyright (c) 2014 Daniel Jakots <vigdis@chown.me>
-
-# Permission to use, copy, modify, and distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-# http://db.ffdn.org/api/v1/isp/1/ #cafai
-# http://db.ffdn.org/api/v1/isp/2/ # Ilico
-# http://db.ffdn.org/api/v1/isp/3/ # Grifon
-# http://db.ffdn.org/api/v1/isp/4/ # Illyse
-# http://db.ffdn.org/api/v1/isp/5/ # Franciliens.net
-# http://db.ffdn.org/api/v1/isp/6/ # Sames
-# http://db.ffdn.org/api/v1/isp/7/ # Aquilenet
-# http://db.ffdn.org/api/v1/isp/8/ # LDN
-# http://db.ffdn.org/api/v1/isp/9/ # ARN
-# http://db.ffdn.org/api/v1/isp/11/ # Auvernet
-# http://db.ffdn.org/api/v1/isp/15/ # tetaneutral
-# http://db.ffdn.org/api/v1/isp/17/ # Teleragno
-# http://db.ffdn.org/api/v1/isp/18/ # Iloth
-# http://db.ffdn.org/api/v1/isp/24/ # Pclight
-# http://db.ffdn.org/api/v1/isp/30/ # Rézine
-# http://db.ffdn.org/api/v1/isp/31/ # Igwan
-# http://db.ffdn.org/api/v1/isp/33/ # Ail network
-# http://db.ffdn.org/api/v1/isp/50/ # Faimaison
-# http://db.ffdn.org/api/v1/isp/39/ # Rhizome
-# http://db.ffdn.org/api/v1/isp/45/ # neutrinet
-# http://db.ffdn.org/api/v1/isp/48/ # FDN
-# http://db.ffdn.org/api/v1/isp/49/ # apinnet
-# http://db.ffdn.org/api/v1/isp/35/ # TDN
-# http://db.ffdn.org/api/v1/isp/54/ # NDN
-
-wget -O dump/cafai http://db.ffdn.org/api/v1/isp/1/ # cafai
-wget -O dump/ilico http://db.ffdn.org/api/v1/isp/2/ # Ilico
-wget -O dump/grifon http://db.ffdn.org/api/v1/isp/3/ # Grifon
-wget -O dump/illyse http://db.ffdn.org/api/v1/isp/4/ # Illyse
-wget -O dump/franciliens http://db.ffdn.org/api/v1/isp/5/ # Franciliens.net
-wget -O dump/sames http://db.ffdn.org/api/v1/isp/6/ # Sames
-wget -O dump/aquilenet http://db.ffdn.org/api/v1/isp/7/ # Aquilenet
-wget -O dump/ldn http://db.ffdn.org/api/v1/isp/8/ # LDN
-wget -O dump/arn http://db.ffdn.org/api/v1/isp/9/ # ARN
-wget -O dump/auvernet http://db.ffdn.org/api/v1/isp/11/ # Auvernet
-wget -O dump/tetaneutral http://db.ffdn.org/api/v1/isp/15/ # tetaneutral
-wget -O dump/teleragno http://db.ffdn.org/api/v1/isp/17/ # Teleragno
-wget -O dump/iloth http://db.ffdn.org/api/v1/isp/18/ # Iloth
-wget -O dump/pclight http://db.ffdn.org/api/v1/isp/24/ # Pclight
-wget -O dump/rezine http://db.ffdn.org/api/v1/isp/30/ # Rézine
-wget -O dump/igwan http://db.ffdn.org/api/v1/isp/31/ # Igwan
-wget -O dump/ail-network http://db.ffdn.org/api/v1/isp/33/ # Ail network
-wget -O dump/faimaison http://db.ffdn.org/api/v1/isp/50/ # Faimaison
-wget -O dump/rhizome http://db.ffdn.org/api/v1/isp/39/ # Rhizome
-wget -O dump/neutrinet http://db.ffdn.org/api/v1/isp/45/ # neutrinet
-wget -O dump/fdn http://db.ffdn.org/api/v1/isp/48/ # fdn
-wget -O dump/apinnet http://db.ffdn.org/api/v1/isp/49/ # apinnet
-wget -O dump/tdn http://db.ffdn.org/api/v1/isp/35/ # TDN
-wget -O dump/ndn http://db.ffdn.org/api/v1/isp/54/ # NDN

+ 72 - 0
fetch_fai_info.pl

@@ -0,0 +1,72 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use LWP::Simple;
+use JSON;
+binmode STDOUT, ':encoding(UTF-8)';
+use utf8;
+
+my $ISP_DIR = './isp/';
+my $API_URL = 'https://db.ffdn.org/api/v1/isp/?per_page=9999';
+my $isp_data;
+my $ffdn_members         = 0;
+my $ffdn_subscribers     = 0;
+my @required_json_fields = qw(memberCount subscriberCount website chatrooms progressStatus creationDate ffdnMemberSince email description);
+
+# Fetch data from db.ffdn.org
+sub fetch_data {
+    $isp_data = decode_json get $API_URL;
+
+    # How many members/subscribers in the whole federation?
+    map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{subscriberCount} and $ffdn_subscribers += ${ $_ }{ispformat}{subscriberCount} } @{${ $isp_data }{isps}};
+    map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{memberCount}     and $ffdn_members     += ${ $_ }{ispformat}{memberCount} }     @{${ $isp_data }{isps}};
+
+    # Defaults to N/A if the field doesn't exist.
+    foreach my $field (@required_json_fields) {
+        map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{$field} or ${ $_ }{ispformat}{$field}='N/A' } @{${ $isp_data }{isps}};
+    }
+    return 0;
+}
+
+# Updates files used by UneFede in $ISP_DIR
+sub update_files {
+
+    open my $ffdn_adh_fh, '>', "$ISP_DIR/ffdn.adh";
+    printf {$ffdn_adh_fh} "La fédé compte $ffdn_members adhérent·e·s (et $ffdn_subscribers abonné·e·s). C'est un ratio de %.1f%%. ", $ffdn_subscribers / $ffdn_members * 100;
+    close $ffdn_adh_fh;
+
+    foreach my $fede_isp ( grep { ${ $_ }{is_ffdn_member} == 1 } @{${ $isp_data }{isps}} ) {
+        my $ispformat = ${ $fede_isp }{ispformat};
+
+        # We're using shortname if it exists. Otherwise we'll default to name.
+        my $isp = defined ${ $ispformat }{shortname} ? lc ${ $ispformat }{shortname} : lc ${ $ispformat }{name};
+
+        # isp.adh
+        my $subscribers = ${ $ispformat }{subscriberCount};
+        my $members     = ${ $ispformat }{memberCount};
+        my $string ="$members adhérent·e·s (et $subscribers abonné·e·s). ";
+        if ( $members ne 'N/A' and $subscribers ne 'N/A' ) {
+            $members > 0 and $string .= sprintf 'C\'est un ratio de %.1f%%. ', $subscribers / $members * 100;
+            $string .= sprintf 'Cela représente %.1f%% et %.1f%% des adhésions et des abonnements dans la fédération.', $members / $ffdn_members *100, $subscribers / $ffdn_subscribers * 100;
+        }
+        open my $adh_fh, '>', "$ISP_DIR/$isp.adh";
+        print {$adh_fh} $string;
+        close $adh_fh;
+
+        # isp.info
+        ${ $ispformat }{description}     ne 'N/A' and $string  = "${ $ispformat }{description}. ";
+        ${ $ispformat }{website}         ne 'N/A' and $string .= "Site : ${ $ispformat }{website}. ";
+        ${ $ispformat }{chatrooms}       ne 'N/A' and $string .= 'Salon de discussion : ' . @{ $ispformat }{chatrooms} . '. ';
+        ${ $ispformat }{email}           ne 'N/A' and $string .= "Adresse de contact : ${ $ispformat }{email}. ";
+        ${ $ispformat }{creationDate}    ne 'N/A' and $string .= "Créé le ${ $ispformat }{creationDate}. ";
+        ${ $ispformat }{ffdnMemberSince} ne 'N/A' and $string .= "Il a rejoint la fédé le ${ $ispformat }{ffdnMemberSince}. ";
+        open my $info_fh, '>', "$ISP_DIR/$isp.info";
+        print {$info_fh} $string;
+        close $info_fh;
+    }
+    return 0;
+}
+
+fetch_data;
+update_files;

+ 0 - 211
json-file.pl

@@ -1,211 +0,0 @@
-#!/bin/env perl
-
-# Copyright (c) 2014, 2015 Daniel Jakots <vigdis@chown.me>
-
-# Permission to use, copy, modify, and distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-use strict;
-use warnings;
-
-binmode STDOUT, ":utf8";
-use utf8;
-use JSON;
-
-
-# FAI variables
-my $nb_adh = 0; # nombre d'adhérents
-my $nb_abo = 0; # nombre d'abonnés
-my $nb_adh_ffdn_old = 0; # avant le passage du script
-my $nb_abo_ffdn_old = 0; # avant le passage du script
-my $nb_adh_ffdn = 0; # nombre d'adhérents de la fédé
-my $nb_abo_ffdn = 0; # nombre d'abonnés de la fédé
-my $site = ""; # site web
-my $salon = ""; # salon irc/xmpp
-my $statut = 0; # étape de 1 à 8 du FAI
-my $date_crea = ""; # date de la création de l'asso
-my $date_jffdn = ""; # date de la rentrée dans la fédé
-my $contact = ""; # email de contact
-my $description = ""; # description de l'asso
-
-# variables des dossiers
-my $dump_dir = "dump";
-my $wrk_dir = "wrk-dir";
-
-# récupère les infos du json et les mets dans le ficher au nom du FAI
-sub info{
-    my $fai = shift @_;
-    my $json;
-    {
-	local $/; #Enable 'slurp' mode
-	open my $fh, "<", "/home/bot/bot-irc/". $dump_dir."/". $fai;
-	$json = <$fh>;
-	close $fh;
-    }
-
-    my $data = decode_json($json);
-# pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
-# indique N/A (not available)
-    if (!length $data->{"ispformat"}->{"memberCount"}) {
-	$nb_adh = "N/A";
-    }
-    else {
-	$nb_adh = $data->{"ispformat"}->{"memberCount"};
-    }
-
-    if (!length $data->{"ispformat"}->{"subscriberCount"}) {
-     	$nb_abo = "N/A";
-    }
-    else {
-	$nb_abo = $data->{"ispformat"}->{"subscriberCount"};
-    }
-
-    if (!length $data->{"ispformat"}->{"website"}) {
-	$site = "N/A";
-    }
-    else {
-	$site = $data->{"ispformat"}->{"website"};
-    }
-    if (!length $data->{"ispformat"}->{"chatrooms"}) {
-	$salon = "N/A";
-    }
-    else {
-	$salon = $data->{"ispformat"}->{"chatrooms"}->[0];
-    }
-
-    if (!length $data->{"ispformat"}->{"progressStatus"}) {
-	$statut = "N/A";
-    }
-    else {
-	$statut = $data->{"ispformat"}->{"progressStatus"};
-    }
-
-    if (!length $data->{"ispformat"}->{"creationDate"}) {
-	$date_crea = "N/A";
-    }
-    else{
-	$date_crea = $data->{"ispformat"}->{"creationDate"};
-    }
-
-    if (!length $data->{"ispformat"}->{"ffdnMemberSince"}) {
-	$date_jffdn = "N/A";
-    }
-    else {
-	$date_jffdn = $data->{"ispformat"}->{"ffdnMemberSince"};
-    }
-    if (!length $data->{"ispformat"}->{"email"}) {
-	$contact = "N/A";
-    }
-    else {
-	$contact = $data->{"ispformat"}->{"email"};
-    }
-    if (!length $data->{"ispformat"}->{"description"}) {
-	$description = "N/A";
-    }
-    else {
-	$description = $data->{"ispformat"}->{"description"};
-    }
-
-    my $filename = "/home/bot/bot-irc/wrk-dir/". $fai.".info";
-
-# On indique dans le fichier la phrase que le bot va répondre
-
-    my $fh2;
-    open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
-    print $fh2 $description . ". Site : ". $site ." - Salon de discussion : ". $salon ." - Adresse de contact : ". $contact . " - Créé le ". $date_crea .", il a rejoint la fédé le ". $date_jffdn ."\n";
-    close $fh2;
-}
-
-# récupère les infos du json et les mets dans le ficher au nom du FAI.adh
-sub adh{
-    my $fai = shift @_;
-    my $json;
-    {
-	local $/; #Enable 'slurp' mode
-	open my $fh, "<", "/home/bot/bot-irc/dump/". $fai;
-	$json = <$fh>;
-	close $fh;
-    }
-
-    my $data = decode_json($json);
-# pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
-# indique N/A (not available)
-    if (!length $data->{"ispformat"}->{"memberCount"}) {
-	$nb_adh = "N/A";
-    }
-    else {
-	$nb_adh = $data->{"ispformat"}->{"memberCount"};
-	$nb_adh_ffdn += $nb_adh;
-    }
-
-    if (!length $data->{"ispformat"}->{"subscriberCount"}) {
-     	$nb_abo = "N/A";
-    }
-    else {
-	$nb_abo = $data->{"ispformat"}->{"subscriberCount"};
-	$nb_abo_ffdn += $nb_abo;
-    }
-
-    my $filename = "/home/bot/bot-irc/wrk-dir/". $fai.".adh";
-    my $fh2;
-    open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
-    print $fh2 $nb_adh ." adhérent·e·s (et ".$nb_abo." abonné·e·s). C'est un ratio de ".sprintf("%.1f", $nb_abo / $nb_adh * 100)."%. Cela représente ".
-	sprintf("%.1f", $nb_adh / $nb_adh_ffdn_old * 100) ."% et ". sprintf("%.1f", $nb_abo / $nb_abo_ffdn_old * 100) .
-	"% des adhésions et des abonnements dans la fédération.\n";
-    close $fh2;
-}
-# indique le nombre d'adh et d'abo dans l'ensemble des FAI ; fichier : ffdn.adh
-sub adh_abo_ffdn {
-
-    my $filenameadh = "/home/bot/bot-irc/wrk-dir/ffdn.adh";
-
-    my $fh3;
-# on met l'info dans le fichier
-    open ($fh3, '>', $filenameadh) or die "Impossible d'ouvrir le fichier $filenameadh en écriture";
-    print $fh3 "La fédé compte ".$nb_adh_ffdn ." adhérent·e·s et ". $nb_abo_ffdn ." abonné·e·s. C'est un ratio de ".sprintf("%.1f", $nb_abo_ffdn / $nb_adh_ffdn * 100)."%.";
-    close $fh3;
-
-}
-sub vieuxchiffres {
-    my $filename = "/home/bot/bot-irc/wrk-dir/ffdn.adh";
-    if (-e $filename)
-    {
-	my $fh;
-	open ($fh, '<', $filename) or die "Impossible d'ouvrir le fichier $filename en lecture";
-	# une seule ligne
-	my $line = <$fh>;
-	close $fh;
-	$line =~ s/[^0-9]//g;
-	# gros hack qui ne marchera plus pour les chiffres > 10k ; dealwithit.jpg
-	my @nums = $line =~/\d{4}/g;
-	$nb_adh_ffdn_old = $nums[0];
-	$nb_abo_ffdn_old = $nums[1];
-    }
-
-}
-
-
-vieuxchiffres();
-# pour chaque FAI on génère l'info
-my @filesdump = </home/bot/bot-irc/dump/*>;
-my $filedump;
-foreach $filedump (@filesdump) {
-    $filedump =~ s{/home/bot/bot-irc/dump/}{};
-    info ("$filedump");
-}
-# pour chaque FAI on génère l'info du nombre d'adhérents
-foreach $filedump (@filesdump) {
-    $filedump =~ s{/home/bot/bot-irc/dump/}{};
-    adh ("$filedump");
-}
-# on génère l'info du nombre d'adhérents de la fédé
-adh_abo_ffdn();