bot_plus/utils_cli/bot_plus_cli_mencions_json_cli_refer.php

194 lines
6.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
$crmn = microtime(true);
if (!empty($_SERVER["argv"][1])){
$argument = $_SERVER["argv"][1];
} else {
// Presenta l'ajuda
echo "NO HE REBUT la variable\n";
echo "Cal el directori on s'executa el bot_plus.\n";
exit;
}
$lloc = $argument;
// ara pot carregar la configuració i el logger
include("".$lloc."/configuracio/bot_plus_cfg.php");
include("".$lloc."/utils/bot_plus_funcions.php");
require_once("".$lloc."/lib/logger.php");
//echo $lloc;
$fitxer = $lloc."/fitxers/mencions.json";
//echo $fitxer;
//matriu a partir del fitxer
$json_decc = json_decode(file_get_contents($fitxer), true);
//echo "matriu fitxer\n";
//print_r($json_decc);
//echo is_array($json_decc)."\n";
//sleep(2);
$reg_arx = count($json_decc);
echo "L'arxiu té ".$reg_arx." ".(($reg_arx<>1)?"registres":"registre")."\n";
$dades_url = ["mention"];
//http_build_query_for_curl($dades_url, $limita_enq);
// consulta els existents per afegir-los a la matriu existent si no hi son
//$menc_mat_nova = ConsultaNotificacions($token,$base_url,$limit_m,$headers);
$limit_m = 80;
$menc_mat_nova = ConsultaNotificacions($token,$base_url,$limit_m,$headers);
//print_r($menc_mat_nova);
//exit;
$nom_mat = count($menc_mat_nova);
echo "A la instància he repassat les darreres ".$nom_mat." ".(($nom_mat<>1)?"notificacions":"notificació")."\n";
// fer matriu de mencions noves
$menc_mat = array();
if($nom_mat>=1) {
$mc = 0;
for($f=0;$f<$nom_mat;$f++) {
$men_id = $menc_mat_nova[$f]['id'];
$men_tip = $menc_mat_nova[$f]['type'];
$men_qui = $menc_mat_nova[$f]['account']['acct'];
$men_qua = $menc_mat_nova[$f]['created_at'];
if($men_tip == "mention") { $men_mid = $menc_mat_nova[$f]['status']['id'];
$men_que = html_entity_decode(strip_tags($menc_mat_nova[$f]['status']['content'])); }
else { $men_mid = 0; $men_que = null; }
if($men_tip == "mention") {
$men_mid = $menc_mat_nova[$f]['status']['id'];
$men_que = str_replace(["&#39;"]," ", $men_que);
$men_que = str_replace(["'","`","","\n"],'', $men_que);
$men_que = str_replace(['#','&','"','“','”'],'', $men_que);
$comp_info = strpos($men_que,"infobot");
$comp_imps = strpos($men_que,"impulsosbot");
$comp_orat = strpos($men_que,"oratgebot");
$comp_salu = strpos($men_que,"saludabot");
if( ($comp_info == false)&&($comp_salu == false)&&($comp_orat == false)&&($comp_imps == false) ) {
$menc_mat[] = array ( 'id' => (int)$men_id, 'mis_id' => (int)$men_mid, 'qui' => $men_qui, 'mencio' => $men_tip, 'quan' => $men_qua, 'que' => $men_que );
$mc++;
}
// sleep(2);
}
/** else {
// $menc_mat[] = array ( );
$menc_mat[] = array ( 'id' => (int)0, 'mis_id' => (int)0, 'qui' => null, 'mencio' => null, 'quan' => null );
}
**/
}
$comprova_mat = is_array($menc_mat);
echo "comprova: ".$comprova_mat."\n";
//echo "menc_mat_f\n";
//print_r($menc_mat);
//sleep(2);
//exit;
// evita repetir
$fins = count($menc_mat);
//if( ($mc<0) ){
echo "De les ".$nom_mat." notificacions, ".$mc." ".(($mc<>1)?"són":"és")." una menció.\n";
$cont_fit = 0;
//echo $fins."\n";
for($g=0;$g<$fins;$g++) {
$id_busc = $menc_mat[$g]['id'];
$qu_busc = $menc_mat[$g]['qui'];
$men_que = $menc_mat[$g]['que'];
// echo "id_busc: ".$id_busc."\n";
$troba = array_search($id_busc, array_column($json_decc, 'id'));
// echo "troba: ".$troba."\n";
if( ($troba !== false ) ) {
// echo "La menció ".$id_busc." de ".$qu_busc." ja està al registre\n";
} else {
// if($id_busc<>0) {
$comp_info = strpos($men_que,"infobot");
$comp_imps = strpos($men_que,"impulsosbot");
$comp_orat = strpos($men_que,"oratgebot");
$comp_salu = strpos($men_que,"saludabot");
if( ($id_busc<>0)&&($comp_info == false)&&($comp_salu == false)&&($comp_orat == false)&&($comp_imps == false) ) {
echo "NOVA MENCIÓ ".$id_busc." de ".$qu_busc."\n";
array_push($json_decc,
['id' => (int)$menc_mat[$g]['id'], 'mis_id' => (int)$menc_mat[$g]['mis_id'],
'qui' => $menc_mat[$g]['qui'], 'mencio' => $menc_mat[$g]['mencio'],
'quan' => $menc_mat[$g]['quan'],
'que' => $menc_mat[$g]['que']
]);
}
$cont_fit++;
}
}
//echo "matriu després de push\n";
//print_r($json_decc);
if($cont_fit >= 1) {
// actualitza el fitxer json
// $fes_json = json_encode($json_decc, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
$fes_json = json_encode($json_decc, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
$escriu_f_json = fopen($lloc."/fitxers/mencions.json", "w") or die("No puc escriure al registre!");
fwrite($escriu_f_json, $fes_json);
fclose($escriu_f_json);
// unset($json_decc);
}
//fin if matriu de notificacions té registres
}
else {
echo "No hi han mencions noves.\n";
}
// per a fer els repas linia a linia
$maxim = count($json_decc);
$e = 0;
$reg = 0;
//$ara = strtotime("today");
//$ara = strtotime(filemtime($fitxer));
$ara = (filemtime($fitxer));
//echo $ara."\n";
echo "ACTUALITZE ".(($maxim<>1)?"ELS ".$maxim." REGISTRES":"EL REGISTRE").".\n";
$escriu_mencions_detall = fopen($lloc."/fitxers/mencions.txt", "w") or die("No puc escriure al registre!");
for($e=0;$e<$maxim;$e++) {
$men_id = $json_decc[$e]['id'];
$men_mi = $json_decc[$e]['mis_id'];
$men_qi = $json_decc[$e]['qui'];
$men_qa = $json_decc[$e]['quan'];
$men_qe = $json_decc[$e]['que'];
$men_tp = $json_decc[$e]['mencio'];
// echo "nº".$e." -> ".$men_id." | ".$men_mi. " -> ".$men_tp." - ".utf8_decode($men_qi)." \t-> ".$men_qa."\n";
fwrite($escriu_mencions_detall, $men_id.":::".$men_mi.":::".$men_qi.":::".$men_tp.":::".$men_qa.":::".$men_qe."\n");
// pausa d'un segon
//sleep(1);
//pausa de mig segon (amb microsegons => unitat més sis ceros == segons );
//usleep(200000);
$reg++;
}
fclose($escriu_mencions_detall);
if($maxim > $max_linies) {
$json_decc = array_splice($json_decc,($maxim-$max_linies),$maxim);
//print_r($json_decc);
// actualitza el fitxer json
//$fes_json = json_encode($json_decc, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
$fes_json = json_encode($json_decc, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
$escriu_f_json = fopen($lloc."/fitxers/mencions.json", "w") or die("No puc escriure al registre!");
fwrite($escriu_f_json, $fes_json);
fclose($escriu_f_json);
unset($json_decc);
}
$crmnf = microtime(true);
$tmf = $crmnf - $crmn;
echo "Temps mencions: ".round($tmf,4)."";
//$log->lwrite("Temps mencions: ".round($tmf,4)."");
?>