#! /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;