json-file.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #/usr/lib/perl
  2. # Copyright (c) 2014 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 $site = ""; # site web
  22. my $salon = ""; # salon irc/xmpp
  23. my $statut = 0; # étape de 1 à 8 du FAI
  24. my $date_crea = ""; # date de la création de l'asso
  25. my $date_jffdn = ""; # date de la rentrée dans la fédé
  26. my $contact = ""; # email de contact
  27. my $description = ""; # description de l'asso
  28. # variables des dossiers
  29. my $dump_dir = "dump";
  30. my $wrk_dir = "wrk-dir";
  31. # récupère les infos du json et les mets dans le ficher au nom du FAI
  32. sub info{
  33. my $fai = shift @_;
  34. my $json;
  35. {
  36. local $/; #Enable 'slurp' mode
  37. open my $fh, "<", "/home/daga/bot-irc/". $dump_dir."/". $fai;
  38. $json = <$fh>;
  39. close $fh;
  40. }
  41. my $data = decode_json($json);
  42. # pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
  43. # indique N/A (not available)
  44. if (!length $data->{"ispformat"}->{"memberCount"}) {
  45. $nb_adh = "N/A";
  46. }
  47. else {
  48. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  49. }
  50. if (!length $data->{"ispformat"}->{"subscriberCount"}) {
  51. $nb_abo = "N/A";
  52. }
  53. else {
  54. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  55. }
  56. if (!length $data->{"ispformat"}->{"website"}) {
  57. $site = "N/A";
  58. }
  59. else {
  60. $site = $data->{"ispformat"}->{"website"};
  61. }
  62. if (!length $data->{"ispformat"}->{"chatrooms"}) {
  63. $salon = "N/A";
  64. }
  65. else {
  66. $salon = $data->{"ispformat"}->{"chatrooms"}->[0];
  67. }
  68. if (!length $data->{"ispformat"}->{"progressStatus"}) {
  69. $statut = "N/A";
  70. }
  71. else {
  72. $statut = $data->{"ispformat"}->{"progressStatus"};
  73. }
  74. if (!length $data->{"ispformat"}->{"creationDate"}) {
  75. $date_crea = "N/A";
  76. }
  77. else{
  78. $date_crea = $data->{"ispformat"}->{"creationDate"};
  79. }
  80. if (!length $data->{"ispformat"}->{"ffdnMemberSince"}) {
  81. $date_jffdn = "N/A";
  82. }
  83. else {
  84. $date_jffdn = $data->{"ispformat"}->{"ffdnMemberSince"};
  85. }
  86. if (!length $data->{"ispformat"}->{"email"}) {
  87. $contact = "N/A";
  88. }
  89. else {
  90. $contact = $data->{"ispformat"}->{"email"};
  91. }
  92. if (!length $data->{"ispformat"}->{"description"}) {
  93. $description = "N/A";
  94. }
  95. else {
  96. $description = $data->{"ispformat"}->{"description"};
  97. }
  98. my $filename = "/home/daga/bot-irc/wrk-dir/". $fai.".info";
  99. # On indique dans le fichier la phrase que le bot va répondre
  100. my $fh2;
  101. open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
  102. <<<<<<< HEAD
  103. print $fh2 $description . ". Site : ". $site ." - Salon de discussion : ". $salon ." - Adresse de contact : ". $contact . " - Crée le ". $date_crea .", il a rejoint la fédé le ". $date_jffdn ."\n";
  104. =======
  105. 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";
  106. >>>>>>> e1e4ba39a66a7e535beea14fba6ec9c7ec75ef60
  107. close $fh2;
  108. }
  109. # récupère les infos du json et les mets dans le ficher au nom du FAI.adh
  110. sub adh{
  111. my $fai = shift @_;
  112. my $json;
  113. {
  114. local $/; #Enable 'slurp' mode
  115. open my $fh, "<", "/home/daga/bot-irc/dump/". $fai;
  116. $json = <$fh>;
  117. close $fh;
  118. }
  119. my $data = decode_json($json);
  120. # pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
  121. # indique N/A (not available)
  122. if (!length $data->{"ispformat"}->{"memberCount"}) {
  123. $nb_adh = "N/A";
  124. }
  125. else {
  126. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  127. }
  128. if (!length $data->{"ispformat"}->{"subscriberCount"}) {
  129. $nb_abo = "N/A";
  130. }
  131. else {
  132. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  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 @filesdump = </home/daga/bot-irc/dump/*>;
  143. my $filedump;
  144. my $adh_ffdn = 0;
  145. my $abo_ffdn = 0;
  146. foreach $filedump (@filesdump) {
  147. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  148. my $json;
  149. {
  150. local $/; #Enable 'slurp' mode
  151. open my $fh, "<", "/home/daga/bot-irc/dump/". $filedump;
  152. $json = <$fh>;
  153. close $fh;
  154. }
  155. my $data = decode_json($json);
  156. # si pas d'info, tant pis
  157. unless (!length $data->{"ispformat"}->{"memberCount"}) {
  158. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  159. }
  160. unless (!length $data->{"ispformat"}->{"subscriberCount"}) {
  161. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  162. }
  163. my $fh2;
  164. open ($fh2, '>', $filedump) or die "Impossible d'ouvrir le fichier $filedump en écriture";
  165. # on incrémente pour chaque FAI
  166. $adh_ffdn += $nb_adh;
  167. $abo_ffdn += $nb_abo;
  168. close $fh2;
  169. }
  170. my $filenameadh = "/home/daga/bot-irc/wrk-dir/ffdn.adh";
  171. my $fh3;
  172. # on met l'info dans le fichier
  173. open ($fh3, '>', $filenameadh) or die "Impossible d'ouvrir le fichier $filenameadh en écriture";
  174. print $fh3 "La fédé compte ".$adh_ffdn ." adhérent·e·s et ". $abo_ffdn ." abonné·e·s";
  175. close $fh3;
  176. }
  177. # pour chaque FAI on génère l'info
  178. my @filesdump = </home/daga/bot-irc/dump/*>;
  179. my $filedump;
  180. foreach $filedump (@filesdump) {
  181. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  182. info ("$filedump");
  183. }
  184. # pour chaque FAI on génère l'info du nombre d'adhérents
  185. foreach $filedump (@filesdump) {
  186. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  187. adh ("$filedump");
  188. }
  189. # on génère l'info du nombre d'adhérents de la fédé
  190. adh_abo_ffdn();