2021-10-22 14:19:54 +02:00
|
|
|
<?php
|
|
|
|
if (!defined('_ECRIRE_INC_VERSION')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************
|
|
|
|
* Importation du JSON des chapitres
|
|
|
|
* et des autres informations (durée, taille, etc)
|
|
|
|
***************************************/
|
2021-10-25 15:51:52 +02:00
|
|
|
include_spip('inc/actions');
|
2021-10-22 14:19:54 +02:00
|
|
|
|
|
|
|
function action_importer_emission_dist($arg = null) {
|
2021-10-25 15:51:52 +02:00
|
|
|
|
2021-10-22 14:19:54 +02:00
|
|
|
if (!$arg) {
|
|
|
|
$securiser_action = charger_fonction('securiser_action', 'inc');
|
|
|
|
$arg = $securiser_action();
|
|
|
|
}
|
|
|
|
$id_article = $arg;
|
2021-11-05 14:21:46 +01:00
|
|
|
$texteReponse = libreavous_import_run($id_article);
|
|
|
|
ajax_retour($texteReponse, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_run($id_article) {
|
2021-10-22 14:19:54 +02:00
|
|
|
$existant = sql_select("podcast_code","spip_articles","id_article=".$id_article);
|
|
|
|
if ($existant and sql_count($existant)>0) {
|
|
|
|
$r = sql_fetch($existant);
|
|
|
|
$code_emission = $r["podcast_code"];
|
|
|
|
if(strlen($code_emission) == 0) {
|
|
|
|
return "Le code de l'émission n'est pas renseigné";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "Émission inconnue = ".$id_article;
|
|
|
|
}
|
|
|
|
$erreurs = array();
|
2021-10-27 16:45:55 +02:00
|
|
|
$logs = array();
|
2021-10-22 14:19:54 +02:00
|
|
|
$json = false;
|
|
|
|
$podcast_duree = "";
|
|
|
|
$ogg_size = "";
|
|
|
|
$mp3_size = "";
|
|
|
|
try {
|
2021-11-05 12:51:53 +01:00
|
|
|
$jsonUrl = "https://forge.april.org/libreavous/lav-outils/raw/branch/master/podcasts/config/lav-".$code_emission.".json";
|
|
|
|
$jsonContents = file_get_contents($jsonUrl);
|
|
|
|
if ($jsonContents === false) {
|
|
|
|
$erreurs[] = "Erreur d'accès au fichier JSON :".$jsonUrl;
|
|
|
|
} else {
|
|
|
|
$json = json_decode($jsonContents, true);
|
|
|
|
if (!$json) {
|
|
|
|
$erreurs[] = "JSON incorrect :".$jsonUrl;
|
|
|
|
}
|
2021-10-22 14:19:54 +02:00
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$podcast_duree = libreavous_import_getDuration($code_emission, null);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$ogg_size = libreavous_import_getSize($code_emission, null, "ogg");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$mp3_size = libreavous_import_getSize($code_emission, null, "mp3");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
$chapitres = array();
|
|
|
|
if (array_key_exists("chapters", $json)) {
|
|
|
|
$chapters = $json["chapters"];
|
|
|
|
$count = count($chapters);
|
|
|
|
if ($count > 0) {
|
|
|
|
$duree_sujetlong = 30 * 60;
|
|
|
|
$sujetlongIndex = -1;
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
$chapter = $chapters[$i];
|
|
|
|
$chapter["type"] = "";
|
|
|
|
$debutSecondes = libreavous_import_toSeconde($chapter["start_timestamp"]);
|
|
|
|
$finSecondes = libreavous_import_toSeconde($chapter["end_timestamp"]);
|
|
|
|
if ($debutSecondes === false) {
|
|
|
|
$erreurs[] = "start_timestamp incorrect : ".$chapter["start_timestamp"];
|
|
|
|
} else if ($finSecondes === false) {
|
|
|
|
$erreurs[] = "end_timestamp incorrect : ".$chapter["end_timestamp"];
|
|
|
|
} else if ($debutSecondes == $finSecondes) {
|
|
|
|
$erreurs[] = "start_timestamp et end_timestamp identiques : ".$chapter["end_timestamp"];
|
|
|
|
} else {
|
|
|
|
$duree = $finSecondes - $debutSecondes;
|
|
|
|
if ($duree > $duree_sujetlong) {
|
|
|
|
$sujetlongIndex = $i;
|
|
|
|
$duree_sujetlong = $duree;
|
|
|
|
}
|
|
|
|
$chapter["duree"] = $duree;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists("chapter_title", $chapter)) {
|
|
|
|
$chapter["chapter_title"] = "";
|
|
|
|
$erreurs[] = "Titre de chapitre manquant";
|
|
|
|
}
|
|
|
|
$chapter["ogg_size"] = "";
|
|
|
|
$chapter["mp3_size"] = "";
|
|
|
|
if (!array_key_exists("short_chapter_name", $chapter)) {
|
|
|
|
$chapter["short_chapter_name"] = "";
|
|
|
|
$erreurs[] = "Nom de fichier manquant";
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$chapter["ogg_size"] = libreavous_import_getSize($code_emission, $chapter["short_chapter_name"], "ogg");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$chapter["mp3_size"] = libreavous_import_getSize($code_emission, $chapter["short_chapter_name"], "mp3");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$erreurs[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$chapter["chronique"]="";
|
|
|
|
$chapitres[] = $chapter;
|
|
|
|
}
|
|
|
|
for($i = 0; $i < count($chapitres); $i++) {
|
|
|
|
if ($i == $sujetlongIndex) {
|
|
|
|
$chapitres[$i]["type"] = "SL";
|
|
|
|
} else {
|
|
|
|
$array = libreavous_import_checkType($chapitres[$i]);
|
|
|
|
$chapitres[$i]["type"] = $array[0];
|
|
|
|
if (count($array) > 1) {
|
|
|
|
$chapitres[$i]["chronique"] = $array[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$emissionArray = array(
|
|
|
|
'podcast_duree' =>$podcast_duree,
|
|
|
|
'podcast_mp3' => $mp3_size,
|
|
|
|
'podcast_ogg' => $ogg_size
|
|
|
|
);
|
2021-10-27 16:45:55 +02:00
|
|
|
$logs[] = sql_updateq("spip_articles", $emissionArray, "id_article=".$id_article,'','',false);
|
2021-10-22 14:19:54 +02:00
|
|
|
sql_updateq("spip_articles", $emissionArray, "id_article=".$id_article);
|
|
|
|
foreach ($chapitres as $chapitre) {
|
|
|
|
$code_chapitre = $chapitre["short_chapter_name"];
|
|
|
|
$chapitreArray = array(
|
|
|
|
'titre' => $chapitre["chapter_title"],
|
|
|
|
'code' => $code_chapitre,
|
|
|
|
'id_article' => $id_article,
|
|
|
|
'code_fichier' => $chapitre["short_chapter_name"],
|
|
|
|
'code_podcast' => $code_emission,
|
|
|
|
'debut' => $chapitre["start_timestamp"],
|
|
|
|
'fin' => $chapitre["end_timestamp"],
|
|
|
|
'type_sujet' => $chapitre["type"],
|
|
|
|
'chronique' => $chapitre["chronique"],
|
|
|
|
'mp3' => $chapitre["mp3_size"],
|
|
|
|
'ogg' => $chapitre["ogg_size"]
|
|
|
|
);
|
|
|
|
$chapitreExistant = sql_select("id_chapitre","spip_chapitres",array("id_article=".$id_article, "code=".sql_quote($code_chapitre)));
|
|
|
|
if ($chapitreExistant and sql_count($chapitreExistant)>0) {
|
|
|
|
$r = sql_fetch($chapitreExistant);
|
|
|
|
$id_chapitre = $r["id_chapitre"];
|
2021-10-27 16:45:55 +02:00
|
|
|
$logs[] = sql_updateq("spip_chapitres", $chapitreArray, "id_chapitre=".$id_chapitre,'','',false);
|
2021-10-22 14:19:54 +02:00
|
|
|
sql_updateq("spip_chapitres", $chapitreArray, "id_chapitre=".$id_chapitre);
|
|
|
|
} else {
|
2021-10-27 16:45:55 +02:00
|
|
|
$logs[] = sql_insertq("spip_chapitres", $chapitreArray, '', '', false);
|
2021-10-22 14:19:54 +02:00
|
|
|
$id_nouveau_chapitre = sql_insertq("spip_chapitres", $chapitreArray);
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 16:45:55 +02:00
|
|
|
$texteReponse = "<p>Émission traitée : ". $code_emission." / ".$titre_emission."</p>";
|
2021-10-22 14:19:54 +02:00
|
|
|
if (count($erreurs) > 0) {
|
2021-10-27 16:45:55 +02:00
|
|
|
$texteReponse .= "<h2>Erreurs</h2>";
|
|
|
|
$texteReponse .= "<ul>";
|
|
|
|
foreach ($erreurs as $erreur) {
|
|
|
|
$texteReponse .= "<li><p>".$erreur."</p></li>";
|
|
|
|
}
|
|
|
|
$texteReponse .= "</ul>";
|
|
|
|
}
|
|
|
|
if (count($logs) > 0) {
|
|
|
|
$texteReponse .= "<h2>Requêtes effectuées</h2>";
|
|
|
|
$texteReponse .= "<ul>";
|
|
|
|
foreach ($logs as $log) {
|
|
|
|
$texteReponse .= "<li><p>".$log."</p></li>";
|
|
|
|
}
|
|
|
|
$texteReponse .= "</ul>";
|
2021-10-22 14:19:54 +02:00
|
|
|
}
|
2021-11-05 14:21:46 +01:00
|
|
|
return $texteReponse;
|
2021-10-22 14:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_getDuration($code_emission, $code_sujet) {
|
|
|
|
$fileName = "libre-a-vous-".$code_emission;
|
|
|
|
if ($code_sujet) {
|
|
|
|
$fileName .= "-".$code_sujet;
|
|
|
|
}
|
|
|
|
$fileName .= ".ogg.duration";
|
|
|
|
return libreavous_import_getMediaFileContent($code_emission, $fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_getSize($code_emission, $code_sujet, $extension) {
|
|
|
|
$fileName = "libre-a-vous-".$code_emission;
|
|
|
|
if ($code_sujet) {
|
|
|
|
$fileName .= "-".$code_sujet;
|
|
|
|
}
|
|
|
|
$fileName .= ".".$extension.".size";
|
|
|
|
return libreavous_import_getMediaFileContent($code_emission, $fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_getMediaFileContent($code, $fileName) {
|
|
|
|
$url = "https://media.april.org/audio/radio-cause-commune/libre-a-vous/emissions/".$code."/".$fileName;
|
|
|
|
$contents = file_get_contents($url);
|
|
|
|
$resultat = trim($contents);
|
|
|
|
if (strlen($resultat) == 0) {
|
|
|
|
throw new Exception("Fichier manquant ou vide : ".$url);
|
|
|
|
}
|
|
|
|
return $resultat;
|
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_toSeconde($time) {
|
|
|
|
$test = preg_match('/^(\d+):(\d+):(\d+)/', $time, $matches);
|
|
|
|
if ($test !== false) {
|
|
|
|
return ($matches[1] * 60 * 60) + ($matches[2] * 60) + ($matches[3]);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function libreavous_import_checkType($chapitre) {
|
|
|
|
$code = $chapitre["short_chapter_name"];
|
|
|
|
$result = array();
|
|
|
|
if ($code == "annonces") {
|
|
|
|
$result[] = "AN";
|
|
|
|
} else {
|
|
|
|
$idx = strpos($code, "chronique-");
|
|
|
|
if ($idx === 0) {
|
|
|
|
$result[] = "CH";
|
|
|
|
$nom_chronique = substr($code, strlen("chronique-"));
|
2021-10-27 16:45:55 +02:00
|
|
|
$result[] = $nom_chronique;
|
2021-10-22 14:19:54 +02:00
|
|
|
} else {
|
|
|
|
$result[] = "SC";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|