json-file.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. 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";
  103. close $fh2;
  104. }
  105. # récupère les infos du json et les mets dans le ficher au nom du FAI.adh
  106. sub adh{
  107. my $fai = shift @_;
  108. my $json;
  109. {
  110. local $/; #Enable 'slurp' mode
  111. open my $fh, "<", "/home/daga/bot-irc/dump/". $fai;
  112. $json = <$fh>;
  113. close $fh;
  114. }
  115. my $data = decode_json($json);
  116. # pour chaque valeur on vérifie que ce n'est pas nul, si c'est le cas on
  117. # indique N/A (not available)
  118. if (!length $data->{"ispformat"}->{"memberCount"}) {
  119. $nb_adh = "N/A";
  120. }
  121. else {
  122. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  123. }
  124. if (!length $data->{"ispformat"}->{"subscriberCount"}) {
  125. $nb_abo = "N/A";
  126. }
  127. else {
  128. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  129. }
  130. my $filename = "/home/daga/bot-irc/wrk-dir/". $fai.".adh";
  131. my $fh2;
  132. open ($fh2, '>', $filename) or die "Impossible d'ouvrir le fichier $filename en écriture";
  133. print $fh2 $nb_adh ." adhérent·e·s (et ".$nb_abo." abonné·e·s).\n";
  134. close $fh2;
  135. }
  136. # indique le nombre d'adh et d'abo dans l'ensemble des FAI ; fichier : ffdn.adh
  137. sub adh_abo_ffdn {
  138. my @filesdump = </home/daga/bot-irc/dump/*>;
  139. my $filedump;
  140. my $adh_ffdn = 0;
  141. my $abo_ffdn = 0;
  142. foreach $filedump (@filesdump) {
  143. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  144. my $json;
  145. {
  146. local $/; #Enable 'slurp' mode
  147. open my $fh, "<", "/home/daga/bot-irc/dump/". $filedump;
  148. $json = <$fh>;
  149. close $fh;
  150. }
  151. my $data = decode_json($json);
  152. # si pas d'info, tant pis
  153. unless (!length $data->{"ispformat"}->{"memberCount"}) {
  154. $nb_adh = $data->{"ispformat"}->{"memberCount"};
  155. }
  156. unless (!length $data->{"ispformat"}->{"subscriberCount"}) {
  157. $nb_abo = $data->{"ispformat"}->{"subscriberCount"};
  158. }
  159. my $fh2;
  160. open ($fh2, '>', $filedump) or die "Impossible d'ouvrir le fichier $filedump en écriture";
  161. # on incrémente pour chaque FAI
  162. $adh_ffdn += $nb_adh;
  163. $abo_ffdn += $nb_abo;
  164. close $fh2;
  165. }
  166. my $filenameadh = "/home/daga/bot-irc/wrk-dir/ffdn.adh";
  167. my $fh3;
  168. # on met l'info dans le fichier
  169. open ($fh3, '>', $filenameadh) or die "Impossible d'ouvrir le fichier $filenameadh en écriture";
  170. print $fh3 "La fédé compte ".$adh_ffdn ." adhérent·e·s et ". $abo_ffdn ." abonné·e·s";
  171. close $fh3;
  172. }
  173. # pour chaque FAI on génère l'info
  174. my @filesdump = </home/daga/bot-irc/dump/*>;
  175. my $filedump;
  176. foreach $filedump (@filesdump) {
  177. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  178. info ("$filedump");
  179. }
  180. # pour chaque FAI on génère l'info du nombre d'adhérents
  181. foreach $filedump (@filesdump) {
  182. $filedump =~ s{/home/daga/bot-irc/dump/}{};
  183. adh ("$filedump");
  184. }
  185. # on génère l'info du nombre d'adhérents de la fédé
  186. adh_abo_ffdn();