json-file.pl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/env perl
  2. # Copyright (c) 2014, 2015 Daniel Jakots <vigdis@chown.me>
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. use strict;
  14. use warnings;
  15. binmode STDOUT, ":utf8";
  16. use utf8;
  17. use JSON;
  18. # FAI variables
  19. my $nb_adh = 0; # nombre d'adhérents
  20. my $nb_abo = 0; # nombre d'abonnés
  21. my $nb_adh_ffdn = 0; # nombre d'adhérents
  22. my $nb_abo_ffdn = 0; # nombre d'abonnés
  23. my $site = ""; # site web
  24. my $salon = ""; # salon irc/xmpp
  25. my $statut = 0; # étape de 1 à 8 du FAI
  26. my $date_crea = ""; # date de la création de l'asso
  27. my $date_jffdn = ""; # date de la rentrée dans la fédé
  28. my $contact = ""; # email de contact
  29. my $description = ""; # description de l'asso
  30. # variables des dossiers
  31. my $dump_dir = "dump";
  32. my $wrk_dir = "wrk-dir";
  33. # récupère les infos du json et les mets dans le ficher au nom du FAI
  34. sub info{
  35. my $fai = shift @_;
  36. my $json;
  37. {
  38. local $/; #Enable 'slurp' mode
  39. open my $fh, "<", "/home/daga/bot-irc/". $dump_dir."/". $fai;
  40. $json = <$fh>;
  41. close $fh;
  42. }
  43. my $data = decode_json($json);
  44. # pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
  45. # indique N/A (not available)
  46. if (!length $data->{"ispformat"}->{"memberCount"}) {
  47. $nb_adh = "N/A";
  48. }
  49. else {
  50. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  51. }
  52. if (!length $data->{"ispformat"}->{"subscriberCount"}) {
  53. $nb_abo = "N/A";
  54. }
  55. else {
  56. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  57. }
  58. if (!length $data->{"ispformat"}->{"website"}) {
  59. $site = "N/A";
  60. }
  61. else {
  62. $site = $data->{"ispformat"}->{"website"};
  63. }
  64. if (!length $data->{"ispformat"}->{"chatrooms"}) {
  65. $salon = "N/A";
  66. }
  67. else {
  68. $salon = $data->{"ispformat"}->{"chatrooms"}->[0];
  69. }
  70. if (!length $data->{"ispformat"}->{"progressStatus"}) {
  71. $statut = "N/A";
  72. }
  73. else {
  74. $statut = $data->{"ispformat"}->{"progressStatus"};
  75. }
  76. if (!length $data->{"ispformat"}->{"creationDate"}) {
  77. $date_crea = "N/A";
  78. }
  79. else{
  80. $date_crea = $data->{"ispformat"}->{"creationDate"};
  81. }
  82. if (!length $data->{"ispformat"}->{"ffdnMemberSince"}) {
  83. $date_jffdn = "N/A";
  84. }
  85. else {
  86. $date_jffdn = $data->{"ispformat"}->{"ffdnMemberSince"};
  87. }
  88. if (!length $data->{"ispformat"}->{"email"}) {
  89. $contact = "N/A";
  90. }
  91. else {
  92. $contact = $data->{"ispformat"}->{"email"};
  93. }
  94. if (!length $data->{"ispformat"}->{"description"}) {
  95. $description = "N/A";
  96. }
  97. else {
  98. $description = $data->{"ispformat"}->{"description"};
  99. }
  100. my $filename = "/home/daga/bot-irc/wrk-dir/". $fai.".info";
  101. # On indique dans le fichier la phrase que le bot va répondre
  102. my $fh2;
  103. open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
  104. 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";
  105. close $fh2;
  106. }
  107. # récupère les infos du json et les mets dans le ficher au nom du FAI.adh
  108. sub adh{
  109. my $fai = shift @_;
  110. my $json;
  111. {
  112. local $/; #Enable 'slurp' mode
  113. open my $fh, "<", "/home/daga/bot-irc/dump/". $fai;
  114. $json = <$fh>;
  115. close $fh;
  116. }
  117. my $data = decode_json($json);
  118. # pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
  119. # indique N/A (not available)
  120. if (!length $data->{"ispformat"}->{"memberCount"}) {
  121. $nb_adh = "N/A";
  122. }
  123. else {
  124. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  125. $nb_adh_ffdn += $nb_adh;
  126. }
  127. if (!length $data->{"ispformat"}->{"subscriberCount"}) {
  128. $nb_abo = "N/A";
  129. }
  130. else {
  131. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  132. $nb_abo_ffdn += $nb_abo;
  133. }
  134. my $filename = "/home/daga/bot-irc/wrk-dir/". $fai.".adh";
  135. my $fh2;
  136. open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
  137. print $fh2 $nb_adh ." adhérent·e·s (et ".$nb_abo." abonné·e·s).\n";
  138. close $fh2;
  139. }
  140. # indique le nombre d'adh et d'abo dans l'ensemble des FAI ; fichier : ffdn.adh
  141. sub adh_abo_ffdn {
  142. my $filenameadh = "/home/daga/bot-irc/wrk-dir/ffdn.adh";
  143. my $fh3;
  144. # on met l'info dans le fichier
  145. open ($fh3, '>', $filenameadh) or die "Impossible d'ouvrir le fichier $filenameadh en écriture";
  146. print $fh3 "La fédé compte ".$nb_adh_ffdn ." adhérent·e·s et ". $nb_abo_ffdn ." abonné·e·s";
  147. close $fh3;
  148. }
  149. # pour chaque FAI on génère l'info
  150. my @filesdump = </home/daga/bot-irc/dump/*>;
  151. my $filedump;
  152. foreach $filedump (@filesdump) {
  153. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  154. info ("$filedump");
  155. }
  156. # pour chaque FAI on génère l'info du nombre d'adhérents
  157. foreach $filedump (@filesdump) {
  158. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  159. adh ("$filedump");
  160. }
  161. # on génère l'info du nombre d'adhérents de la fédé
  162. adh_abo_ffdn();