Nettoyage du code et copie des scripts d'import

This commit is contained in:
Vincent Calame 2021-10-15 13:37:55 +02:00 committed by root
parent 15bf26307e
commit fcb6fb310e
3 changed files with 219 additions and 180 deletions

View File

@ -16,172 +16,6 @@ function filtre_libreavous_supp_numero($texte) {
return trim(substr($texte, $idx +1)); return trim(substr($texte, $idx +1));
} }
function balise_LIBREAVOUS_IMPORT($p) {
$id_rubrique = "''";
$id_secteur = "''";
if (($v = interprete_argument_balise(1,$p))!==NULL){
$id_rubrique = $v;
if (($v = interprete_argument_balise(2,$p))!==NULL) {
$id_secteur = $v;
}
}
$p->code = "libreavous_Import()";
return $p;
}
function libreavous_Import_Historique() {
$historique = simplexml_load_file ("/var/www/exemole.fr/libreavous/historique.xml");
foreach ($historique->item as $item) {
$code = $item['code'];
$array = array(
'drupal_guid' => $item['guid'],
'drupal_pubdate' => $item['pubdate']
);
$type = $item['type'];
if ($type == 'emission') {
$existant = sql_select("id_article","spip_articles","podcast_code=".sql_quote($code));
if ($existant and sql_count($existant)>0) {
$r = sql_fetch($existant);
$id_article = $r["id_article"];
sql_updateq("spip_articles", $array, "id_article=".$id_article);
} else {
echo "Émission inconnue = $code<br>" ;
}
} else {
$chapitreExistant = sql_select("id_chapitre","spip_chapitres",array("code_podcast=".sql_quote($code), "code_fichier=".sql_quote($item['chapitre'])));
if ($chapitreExistant and sql_count($chapitreExistant)>0) {
$r = sql_fetch($chapitreExistant);
$id_chapitre = $r["id_chapitre"];
sql_updateq("spip_chapitres", $array, "id_chapitre=".$id_chapitre);
} else {
echo "Chapitre inconnu = $code / fichier = ".$item['chapitre']."<br>" ;
}
}
}
}
function libreavous_Import($id_rubrique, $id_secteur) {
$emissions = simplexml_load_file ("/var/www/exemole.fr/libreavous/verspip.xml");
foreach ($emissions->emission as $emission) {
$code = $emission["code"];
$emissionArray = array(
'lang' => 'fr',
'surtitre' => $emission["numero"],
'titre' => $emission["numero"].' - '.$emission["titre"],
'soustitre' => "",
'id_rubrique' => $id_rubrique,
'id_secteur' => $id_secteur,
'descriptif' => $emission["description"],
'chapo' => $emission["programme"],
'texte' => $emission["references"],
'ps' => "",
'statut' => 'publie',
'accepter_forum' => 'non',
'date' => libreavous_toDateIso($emission["code"]),
'podcast_code' => $emission["code"],
'podcast_duree' => $emission["duree"],
'podcast_mp3' => $emission["mp3"],
'podcast_ogg' => $emission["ogg"],
'podcast_transcription' => $emission["transcription"],
'podcast_personnes' => $emission["personnes"],
'podcast_galeriephotos' => $emission["galeriephotos"],
'podcast_musique' => $emission["musique"]
);
$existant = sql_select("id_article","spip_articles","podcast_code=".sql_quote($code));
if ($existant and sql_count($existant)>0) {
$r = sql_fetch($existant);
$id_article = $r["id_article"];
sql_updateq("spip_articles", $emissionArray, "id_article=".$id_article);
} else {
$id_article = sql_insertq("spip_articles", $emissionArray);
}
foreach ($emission->chapitre as $chapitre) {
$code_chapitre = $chapitre["code"];
$chapitreArray = array(
'titre' => $chapitre["titre"],
'code' => $chapitre["code"],
'id_article' => $id_article,
'code_fichier' => $chapitre["fichier"],
'code_podcast' => $emission["code"],
'debut' => $chapitre["debut"],
'fin' => $chapitre["fin"],
'type_sujet' => $chapitre["type"],
'chronique' => $chapitre["chronique"],
'mp3' => $chapitre["mp3"],
'ogg' => $chapitre["ogg"],
'references_sujet' => $chapitre["references"]
);
$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"];
sql_updateq("spip_chapitres", $chapitreArray, "id_chapitre=".$id_chapitre);
} else {
$id_nouveau_chapitre = sql_insertq("spip_chapitres", $chapitreArray);
}
}
}
}
function libreavous_toDateIso($code) {
$texte = substr($code, 0, 4)."-".substr($code,4,2)."-".substr($code,6,2)." 15:30:00";
return $texte;
}
function libreavous_conversionDrupal($shortdate) {
$annee = substr($shortdate, 0, 4);
$mois = substr($shortdate, 4, 2);
$jour = substr($shortdate, 6, 2);
if (substr($jour, 0, 1) == "0") {
$jour = substr($jour, 1, 1);
}
return $jour."-".libreavous_conversionMoisDrupal($mois)."-".$annee;
}
function libreavous_conversionMoisDrupal($mois) {
switch($mois) {
case "01":
return "janvier";
case "02":
return "fevrier";
case "03":
return "mars";
case "04":
return "avril";
case "05":
return "mai";
case "06":
return "juin";
case "07":
return "juillet";
case "08":
return "aout";
case "09":
return "septembre";
case "10":
return "octobre";
case "11":
return "novembre";
case "12":
return "decembre";
default:
return "XX";
}
}
function libreavous_nomtypesujet($abbr) {
switch($abbr) {
case "CH":
return "Chronique";
case "SL":
return "Sujet libre";
case "AN":
return "Annonce";
default:
return $abbr;
}
}
// plutot utiliser la fonction spip taille_en_octets // plutot utiliser la fonction spip taille_en_octets
function libreavous_mio($number) { function libreavous_mio($number) {
@ -230,3 +64,213 @@ function libreavous_aveczerod($chaine) {
return $chaineaveczero; return $chaineaveczero;
} }
/***************************************
* Importation du JSON des chapitres
* et des autres informations (durée, taille, etc)
***************************************/
function balise_LIBREAVOUS_IMPORT($p) {
$id_article = "''";
if (($v = interprete_argument_balise(1,$p))!==NULL){
$id_article = $v;
}
$p->code = "libreavous_import_run($id_article)";
return $p;
}
function libreavous_import_run($id_article) {
$id_article = intval($id_article);
$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();
$json = false;
$podcast_duree = "";
$ogg_size = "";
$mp3_size = "";
try {
$jsonContents = file_get_contents("https://forge.april.org/libreavous/lav-outils/src/branch/master/podcasts/config/lav-".$code_emission.".json");
$json = json_decode($jsonContents, true);
if (!$json) {
$erreurs[] = "JSON incorrect";
}
} 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
);
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"];
sql_updateq("spip_chapitres", $chapitreArray, "id_chapitre=".$id_chapitre);
} else {
$id_nouveau_chapitre = sql_insertq("spip_chapitres", $chapitreArray);
}
}
$texteReponse = "Émission traitée : ". $code_emission;
if (count($erreurs) > 0) {
$texteReponse .= "<br><br>Erreurs : <br>".implode(" <br> ", $erreurs);
}
return $texteReponse;
}
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-"));
} else {
$result[] = "SC";
}
}
return $result;
}

View File

@ -1,14 +0,0 @@
<BOUCLE_principale(RUBRIQUES) {id_rubrique}>
<!DOCTYPE html>
<html>
<head>
<title>[(#TITRE|couper{80}|textebrut) - ][(#NOM_SITE_SPIP|textebrut)]</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Importation vers : #TITRE</p>
#LIBREAVOUS_IMPORT{#ID_RUBRIQUE,#ID_SECTEUR}
</body>
</html>
</BOUCLE_principale>

View File

@ -0,0 +1,9 @@
<html lang="fr">
<head>
<title>Import</title>
</head>
<body>
<p>
#LIBREAVOUS_IMPORT{#ENV{id_article}}
</body>
</html>