Titres

<?php
// nabal: page appelée par Zetta pour le titrage
// https://xxxxxxxx.frequence3.net/input/track.php?station=test&title=Justin%20Timberlake%20-%20Rock%20Your%20Body
// dans zetta : http://xxxxxxx.frequence3.net/input/track.php?station=frequence3&ASSETTYPE=%ASSETTYPE%&AIRDATETIME_UTC=%AIRDATETIME_UTC%&ID=%GUID%&ARTIST=%ARTIST%&TITLE=%TITLE%&RUNTIME=%RUNTIME%
//

require_once dirname(__FILE__).'/pdo.php';

$cur_track=file_get_contents($_GET['station'].'.txt');

if ($_GET['ARTIST'].' - '.$_GET['TITLE'] == $cur_track) {
    echo "titre en cours";
    exit;
}

if (!empty($_GET['ARTIST']) && !empty($_GET['TITLE'])) {
    file_put_contents($_GET['station'].'.txt', $_GET['ARTIST'].' - '.$_GET['TITLE']);
}


if ($_GET['station'] == 'f3' || $_GET['station'] == 'frequence3') {
        $station_id = 1;
}
if ($_GET['station'] == 'u3' || $_GET['station'] == 'urbansession' || $_GET['station'] == 'frequence3urban') {
        $station_id = 2;
}
if ($_GET['station'] == 'test') {
        $station_id = 3;
        //file_put_contents('test/'.$_GET['station'].date('His').'.txt', $_GET['data']);
        $xml = simplexml_load_string($_GET['data']);
        $json = json_encode($xml);
        file_put_contents($_GET['station'].'.json', $json);
        //--uid:'.$_GET['uid'] .'--suid:'.$_GET['suid'] .'--smd5:'.$_GET['smd5']);
        include ('testinsert.php');
}

if ($_GET['station'] == 'generationfm' || $_GET['station'] == 'frequence3fm') {
    $station_id = 4;
}
if ($_GET['station'] == 'abf' || $_GET['station'] == 'frequence3dance') {
    $station_id = 5;
}
if ($_GET['station'] == 'abfcult') {
    // station morte
    $station_id = 6;
}
if ($_GET['station'] == 'abfgold' || $_GET['station'] == 'frequence3gold') {
    $station_id = 7;
}
if ($_GET['station'] == 'abflove') {
    // station morte
    $station_id = 8;
}
if ($_GET['station'] == 'abfworld' || $_GET['station'] == 'frequence3world') {
    $station_id = 9;
}

if ($_GET['station'] == 'xmas') {
    $station_id = 10;
}

$artist=$_GET['ARTIST'];
$title=$_GET['TITLE'];
$type=$_GET['ASSETTYPE'];
if ($type!="Song") {
exit;
}

$timestamp = time();

// si c'est pas la station de test
if ($station_id!=3) {
    $conn->beginTransaction();
    $sql = "INSERT INTO playlist (artist, title, station_id, created_at) VALUES (?, ?, ?, ?);";
    $sth = $conn->prepare($sql);
    $sth->execute([$artist, $title, $station_id, $timestamp]);
    $playlist_id = $conn->lastInsertId();
echo "<br>in if<br>";
echo $playlist_id;
    var_dump([$artist, $title, $station_id, $timestamp]);
    $conn->commit();
}

$conn->beginTransaction();
$sql = "INSERT INTO artist (name, created_at) VALUES (?, ?);";
$sth = $conn->prepare($sql);
$sth->execute([$artist, $timestamp]);
$artist_id = $conn->lastInsertId();
$conn->commit();
echo $artist_id;

if ($artist_id == 0) {
    $conn->beginTransaction();
    $sql = "SELECT id FROM artist WHERE name=?";
    $sth = $conn->prepare($sql);
    $sth->execute([$artist]);
    $result = $sth->fetch(PDO::FETCH_ASSOC);
    $artist_id = $result['id'];
echo "<br>in if<br>";
echo $artist_id;
    $conn->commit();
}

$conn->beginTransaction();
$sql = "INSERT INTO title (artist_id, name, created_at) VALUES (?, ?, ?);";
$sth = $conn->prepare($sql);
$sth->execute([$artist_id, $title, $timestamp]);
$title_id = $conn->lastInsertId();
$conn->commit();
echo $title_id;

if ($title_id == 0) {
    $conn->beginTransaction();
    $sql = "SELECT id FROM title WHERE name=? and artist_id=?";
    $sth = $conn->prepare($sql);
    $sth->execute([$title,$artist_id]);
    $result = $sth->fetch(PDO::FETCH_ASSOC);
    $title_id = $result['id'];
echo "<br>in if<br>";
echo $title_id;
    $conn->commit();
}

$conn->beginTransaction();
$sql = "INSERT INTO chart (artist_id, title_id, station_id, created_at) VALUES (?, ?, ?, ?);";
$sth = $conn->prepare($sql);
$sth->execute([$artist_id, $title_id, $station_id, $timestamp]);
$conn->commit();