fetch_fai_info.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use LWP::Simple;
  5. use JSON;
  6. binmode STDOUT, ':encoding(UTF-8)';
  7. use utf8;
  8. my $ISP_DIR = './isp/';
  9. my $API_URL = 'https://db.ffdn.org/api/v1/isp/?per_page=9999';
  10. my $isp_data;
  11. my $ffdn_members = 0;
  12. my $ffdn_subscribers = 0;
  13. my @required_json_fields = qw(memberCount subscriberCount website chatrooms progressStatus creationDate ffdnMemberSince email description);
  14. # Fetch data from db.ffdn.org
  15. sub fetch_data {
  16. $isp_data = decode_json get $API_URL;
  17. # How many members/subscribers in the whole federation?
  18. map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{subscriberCount} and $ffdn_subscribers += ${ $_ }{ispformat}{subscriberCount} } @{${ $isp_data }{isps}};
  19. map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{memberCount} and $ffdn_members += ${ $_ }{ispformat}{memberCount} } @{${ $isp_data }{isps}};
  20. # Defaults to N/A if the field doesn't exist.
  21. foreach my $field (@required_json_fields) {
  22. map { ${ $_ }{is_ffdn_member} == 1 and defined ${ $_ }{ispformat}{$field} or ${ $_ }{ispformat}{$field}='N/A' } @{${ $isp_data }{isps}};
  23. }
  24. return 0;
  25. }
  26. # Updates files used by UneFede in $ISP_DIR
  27. sub update_files {
  28. open my $ffdn_adh_fh, '>', "$ISP_DIR/ffdn.adh";
  29. 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;
  30. close $ffdn_adh_fh;
  31. foreach my $fede_isp ( grep { ${ $_ }{is_ffdn_member} == 1 } @{${ $isp_data }{isps}} ) {
  32. my $ispformat = ${ $fede_isp }{ispformat};
  33. # We're using shortname if it exists. Otherwise we'll default to name.
  34. my $isp = defined ${ $ispformat }{shortname} ? lc ${ $ispformat }{shortname} : lc ${ $ispformat }{name};
  35. # isp.adh
  36. my $subscribers = ${ $ispformat }{subscriberCount};
  37. my $members = ${ $ispformat }{memberCount};
  38. my $string ="$members adhérent·e·s (et $subscribers abonné·e·s). ";
  39. if ( $members ne 'N/A' and $subscribers ne 'N/A' ) {
  40. $members > 0 and $string .= sprintf 'C\'est un ratio de %.1f%%. ', $subscribers / $members * 100;
  41. $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;
  42. }
  43. open my $adh_fh, '>', "$ISP_DIR/$isp.adh";
  44. print {$adh_fh} $string;
  45. close $adh_fh;
  46. # isp.info
  47. ${ $ispformat }{description} ne 'N/A' and $string = "${ $ispformat }{description}. ";
  48. ${ $ispformat }{website} ne 'N/A' and $string .= "Site : ${ $ispformat }{website}. ";
  49. ${ $ispformat }{chatrooms} ne 'N/A' and $string .= 'Salon de discussion : ' . @{ $ispformat }{chatrooms} . '. ';
  50. ${ $ispformat }{email} ne 'N/A' and $string .= "Adresse de contact : ${ $ispformat }{email}. ";
  51. ${ $ispformat }{creationDate} ne 'N/A' and $string .= "Créé le ${ $ispformat }{creationDate}. ";
  52. ${ $ispformat }{ffdnMemberSince} ne 'N/A' and $string .= "Il a rejoint la fédé le ${ $ispformat }{ffdnMemberSince}. ";
  53. open my $info_fh, '>', "$ISP_DIR/$isp.info";
  54. print {$info_fh} $string;
  55. close $info_fh;
  56. }
  57. return 0;
  58. }
  59. fetch_data;
  60. update_files;