numrobis Code ami: 0726-2419-2891 | Voici:
le script getAllocine.php a besoin du second pour fonctionner.
Code :
- #!/usr/bin/php
- <?php
- // INIT :
- $patterns = array('720p', '1080p', 'tvhd', 'vostfr', 'vost', 'divx', 'xvid', 'x264', 'h264');
- $forcedId = NULL;
- $proxyUse = false;
- $nbFilms=$nbFilmsMT=0;
- $urlApiAllocineMovieInfo='http://api.allocine.fr/rest/v3/movie?partner=YW5kcm9pZC12M3M&code=unCode';
- $urlApiAllocineSearchMovies='http://api.allocine.fr/rest/v3/search?partner=YW5kcm9pZC12M3M&q=unTitre';
- //fonction pour parser les paramètres :
- function parseParameters($noopt = array()) {
- $result = array();
- $params = $GLOBALS['argv'];
- // could use getopt() here (since PHP 5.3.0), but it doesn't work relyingly
- reset($params);
- while (list($tmp, $p) = each($params)) {
- if ($p{0} == '-') {
- $pname = substr($p, 1);
- $value = true;
- if ($pname{0} == '-') {
- // long-opt (--<param> )
- $pname = substr($pname, 1);
- if (strpos($p, '=') !== false) {
- // value specified inline (--<param>=<value> )
- list($pname, $value) = explode('=', substr($p, 2), 2);
- }
- }
- // check if next parameter is a descriptor or a value
- $nextparm = current($params);
- if (!in_array($pname, $noopt) && $value === true && $nextparm !== false && $nextparm{0} != '-') list($tmp, $value) = each($params);
- $result[$pname] = $value;
- } else {
- // param doesn't belong to any option
- $result[] = $p;
- }
- }
- return $result;
- }
- include 'xml_regex.php';
- $options=parseParameters();
- //Traitement des options
- foreach ( $options as $opt => $value ) {
- // echo "Debug opt / value : $opt / $value \n";
- //on force $opt à etre lu comme une chaine
- $opt .= '';
- switch ($opt) {
- case "help";
- case "h";
- echo "Syntaxe : $argv[0] -t \"Titre de film\" [-i <id_de_film>] \n
- Détail :
- -t, --titre <titre de film>
- -i, --id <id de film> : L'id de film allocine (recup via l'url allocine)
- -p, --proxy <ip:port>\n";
- exit();
- break;
- case "titre";
- case "t";
- $titre = $fileName = $value;
- break;
- case "id";
- case "i";
- $forcedId = $value;
- break;
- case "p";
- case "proxy";
- $proxyUse = true;
- $proxyAdd = $value;
- break;
- }
- }
- //Mise en place du proxy si besoin
- if ( $proxyUse ) {
- $proxy = array(
- 'http' => array (
- 'proxy'=>'tcp://'.$proxyAdd,
- 'request_fulluri' => true
- ),
- 'https' => array (
- 'proxy'=>'tcp://'.$proxyAdd,
- 'request_fulluri' => true
- )
- );
- $context = stream_context_create($proxy);
- }
- //Test des valeurs obligatoires
- if ( $titre === NULL) {
- echo "L'option -t est obligatoire \n";
- exit();
- }
- //Definition par defaut du nom de fichier à la valeur du premier argument
- //Encodage du titre en format URL pour l'utiliser dans la requete API
- $titre = urlencode($titre);
- //Recuperation du XML de resultat de recherche allocine
- $url=str_replace('unTitre',$titre,$urlApiAllocineSearchMovies);
- $xmlReqId = ($proxyUse) ? file_get_contents($url, false, $context) : file_get_contents($url);
- //Comptage des resultats pour les type "films" : <results type="movie">VALUE</results>
- $listeMovie = $listeMT = NULL;
- $listeMovie = element_set('movie', $xmlReqId);
- $listeMT = element_set('movietheater', $xmlReqId);
- $nbMovie = $nbMovieMT = 0;
- $nbMovie = ($listeMovie) ? sizeof($listeMovie) : 0;
- $nbMovieMT = ($listeMT) ? sizeof($listeMT) : 0;
- $nbFilms = $nbMovie + $nbMovieMT;
- echo "Found $nbFilms \n";
- if ($forcedId == NULL) {
- //Si on a plus d'un film on propose le choix sous forme de liste
- if ( $nbFilms != 1 ) {
- $indice = 1;
- //Permettre le choix entre les films (afficher annee/Titre)
- for ($i = 0 ; $i < $nbMovie ; $i ++) {
- $currentXml = $listeMovie[$i];
- $titre = value_in('originalTitle', $currentXml);
- $year = value_in('productionYear', $currentXml);
- echo "Film $indice : $titre / $year \n";
- $indice ++;
- }
- for ($i = 0 ; $i < $nbMovieMT ; $i ++) {
- $currentXml = $listeMT[$i];
- $titre = value_in('originalTitle', $currentXml);
- $year = value_in('productionYear', $currentXml);
- echo "Film $indice : $titre / $year \n";
- $indice ++;
- }
-
- fwrite(STDOUT, "Movie choice from the above list : <1,2,3 ...> : " );
- $choice = trim(fgets(STDIN));
- $choice = $choice - 1 ;
- if ($choice <= $nbMovie){
- $idFilmArray = element_attributes('movie', $listeMovie[$choice]);
- }
- else {
- $choice = $choice - $nbMovie;
- $idFilmArray = element_attributes('movie', $listeMT[$choice]);
- }
- //echo "Values : NBMOV / NBMT / CHOICE : $nbMovie / $nbMovieMT / $choice \n";
-
- //TODO Tests d'erreur sur le choix (!= entier toussa)
-
- //$idFilmArray = element_attributes('movie', $listeCode[$choice]);
- $idFilm = $idFilmArray['code'];
- echo "ID Allocine : $idFilm \n";
- }
- //Si on a qu'un seul film on suppose que c'est le bon et on garde l'ID du resultat
- else {
- $idFilmArray = element_attributes('movie', $xmlReqId);
- $idFilm = $idFilmArray['code'];
- echo "ID Allocine : $idFilm \n";
- }
- //Generation de l'url de requete de la fiche film sur l'API
- $url=str_replace('unCode',$idFilm,$urlApiAllocineMovieInfo);
- }
- else{
- //Si l'ID est forcé en deuxième paramètre
- $url=str_replace('unCode',$forcedId,$urlApiAllocineMovieInfo);
- $idFilm=$forcedId;
- }
- //recupération du XML de la fiche film
- $xmlFiche = ($proxyUse) ? file_get_contents($url, false, $context) : file_get_contents($url);
- //Récupération des valeurs principales pour génération du NFO
- $titreFR = value_in('title', $xmlFiche);
- $titreOrig = value_in('originalTitle', $xmlFiche);
- $runtime = value_in('runtime', $xmlFiche);
- $annee = value_in('releaseDate', $xmlFiche);
- $rating = value_in('userRating', $xmlFiche);
- $pitch = value_in('synopsisShort', $xmlFiche);
- $genreArray = element_set('genre', $xmlFiche);
- $resume = value_in('synopsis', $xmlFiche);
- $realisateur = value_in('directors', $xmlFiche);
- $acteurs = element_set('name', $xmlFiche);
- $acteurs2 = value_in('actors', $xmlFiche);
- $posterArray = element_attributes('poster', $xmlFiche);
- $posterLink = $posterArray['href'];
- //Download Poster with french title
- $localImage = "./$titreFR.metathumb";
- $imageStream = ($proxyUse) ? @file_get_contents($posterLink, false, $context) : @file_get_contents($posterLink);
- $image = fopen($localImage, 'w');
- fwrite($image, $imageStream);
- fclose($image);
- //Generate YAMJ NFO
- $nfoFile = "./$titreFR.xml";
- $nfo = fopen($nfoFile, 'w');
- // Si on specifie encodage
- fwrite($nfo, "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" );
- fwrite($nfo, "<details>\n" );
- fwrite($nfo, "<title>$titreFR</title>\n" );
- fwrite($nfo, "<year>$annee</year>\n" );
- // fwrite($nfo, "<originalTitle>$titreOrig</originalTitle>\n" );
- $runtime = $runtime / 60;
- fwrite($nfo, "<runtime>$runtime</runtime>\n" );
- // $rating = $rating * 2;
- // fwrite($nfo, "<rating>$rating</rating>\n" );
- foreach($genreArray as $genreXml){
- $genre = value_in('genre', $genreXml);
- fwrite($nfo, "<genre>$genre</genre>\n" );
- }
- fwrite($nfo, "<plot>$pitch</plot>\n" );
- fwrite($nfo, "<overview>$resume</overview>\n" );
- // fwrite($nfo, "<id moviedb=\"allocine\">$idFilm</id>\n" );
- fwrite($nfo, "<director>$realisateur</director>\n" );
- fwrite($nfo, "<actor>\n" );
- foreach($acteurs as $genreXml2){
- $name = value_in('name', $genreXml2);
- fwrite($nfo, "<actor><name>$name</name></actor>\n" );
- }
- // fwrite($nfo, "<thumbnail>$titreFR.jpg</thumbnail>\n" );
- // fwrite($nfo, "<thumbnail>$titreFR.jpg</thumbnail>\n" );
- fwrite($nfo, "</details>" );
- fclose($nfo);
- //output STDOUT to check
- echo "Film : \t\t$titreFR \n";
- echo "Annee de production : $annee \n";
- echo "Note Moyenne : \t$rating \n";
- echo "Resume : \t$resume \n";
- echo "Lien Image : \t$posterLink \n";
- ?>
|
xml_regex.php
Code :
- <?php
- /*
- ** xml_regex.php
- ** by Bobulous.
- ** www.bobulous.org.uk
- **
- ** A couple of very simple XML parsing functions for grabbing at
- ** content in well-structured XML data files.
- ** NOTE: These functions are not suitable for some XML structures,
- ** see http://www.bobulous.org/coding/php-xml-regex.html
- ** for information about the limitations of these functions.
- **
- ** Created on 12 Jun 2007.
- **
- */
- /*
- ** Useful constants for use with the third parameter of these
- ** functions (to discard or preserve the enclosing tags of the
- ** named element).
- */
- define('ELEMENT_CONTENT_ONLY', true);
- define('ELEMENT_PRESERVE_TAGS', false);
- /*
- ** This function returns the content of the first element that
- ** exactly matches the provided name, found in the provided XML
- ** data. If this can't be found, then false is returned.
- ** DO NOT USE THIS FUNCTION ON AN ELEMENT THAT CONTAINS ANOTHER
- ** ELEMENT OF THE SAME NAME (SUCH AS A div WITHIN A div ELEMENT).
- **
- ** $element_name - the name of the element whose content you desire;
- ** $xml - the XML data to search in;
- ** $content_only - if true, the tags enclosing the named element are
- ** discarded. If false, the whole pattern match is returned, and
- ** the enclosing tags are preserved. Defaults to true.
- */
- function value_in($element_name, $xml, $content_only = true) {
- if ($xml == false) {
- return false;
- }
- $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.
- '</'.$element_name.'>#s', $xml, $matches);
- if ($found != false) {
- if ($content_only) {
- return $matches[1]; //ignore the enclosing tags
- } else {
- return $matches[0]; //return the full pattern match
- }
- }
- // No match found: return false.
- return false;
- }
- /*
- ** This function returns an array of elements whose name matches the
- ** provided string, found in the provided XML.
- ** If no match is found, this function returns false.
- ** DO NOT USE THIS FUNCTION ON AN ELEMENT THAT CONTAINS ANOTHER
- ** ELEMENT OF THE SAME NAME (SUCH AS A div WITHIN A div ELEMENT).
- **
- ** $element_name - the name of the elements to search for;
- ** $xml - the XML document to search through;
- ** $content_only - if true, the tags enclosing the named element are
- ** discarded. If false, the whole pattern match is returned, and
- ** the enclosing tags are preserved. Defaults to false.
- */
- function element_set($element_name, $xml, $content_only = false) {
- if ($xml == false) {
- return false;
- }
- $found = preg_match_all('#<'.$element_name.'(?:\s+[^>]+)?>' .
- '(.*?)</'.$element_name.'>#s',
- $xml, $matches, PREG_PATTERN_ORDER);
- if ($found != false) {
- if ($content_only) {
- return $matches[1]; //ignore the enlosing tags
- } else {
- return $matches[0]; //return the full pattern match
- }
- }
- // No match found: return false.
- return false;
- }
- /*
- ** This function extracts the attributes from the first element that
- ** matches the provided name in the provided XML sample.
- ** The function returns an associative array, where the key is the
- ** attribute name, and the value is the attribute value.
- ** If the regular expression cannot find or extract attributes
- ** this function will return false.
- ** THIS FUNCTION CAN ONLY MATCH THE FIRST ELEMENT OF THE PROVIDED
- ** NAME THAT HAS ATTRIBUTES.
- **
- ** $element_name - the name of the element to extract the attributes
- ** from;
- ** $xml - the XML sample to search for the named element.
- */
- function element_attributes($element_name, $xml) {
- if ($xml == false) {
- return false;
- }
- // Grab the string of attributes inside an element tag.
- $found = preg_match('#<'.$element_name.
- '\s+([^>]+(?:"|\'))\s?/?>#',
- $xml, $matches);
- if ($found == 1) {
- $attribute_array = array();
- $attribute_string = $matches[1];
- // Match attribute-name attribute-value pairs.
- $found = preg_match_all(
- '#([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*" )#',
- $attribute_string, $matches, PREG_SET_ORDER);
- if ($found != 0) {
- // Create an associative array that matches attribute
- // names to attribute values.
- foreach ($matches as $attribute) {
- $attribute_array[$attribute[1]] =
- substr($attribute[2], 1, -1);
- }
- return $attribute_array;
- }
- }
- // Attributes either weren't found, or couldn't be extracted
- // by the regular expression.
- return false;
- }
- ?>
|
Utilisation:
Soit vous connaissez le numéro Allocine de votre film alors vous lancez la commande suivante:
Code :
- php getAllocine.php -t -i 177603
|
ou alors directement avec le nom du film. (s'il trouve plusieurs occurence, il vous propose une liste de 10 films parmis lesquels il faut choisir)
Code :
- php getAllocine.php -t mad max
|
Donc vous aurez 2 fichiers:
- 1 fichier .metathumb qui est l'affiche du film;
- 1 fichier .xml qui est les informations du film.
Par contre, je n'ai pas trouvé pour générer le backdrop (l'affiche du fond) du film.
---------------
- Feed-back - - Jeu du moment: J’ai un backlog monstre sur Switch - C'est moi Bananecafe
|