1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
| <?php
/**
* ParserAlloCine 0.1 : Recupere les infos d'un film via Allocine
*
* @author : LECOMTE Cyril <cyrhades@free.fr>
* @version : 14/11/2009
*/
class ParserAlloCine
{
// ON CONSERVE LES PORTIONS UTILE POUR ACCELERER L'EXECUTION (300 a 500 lignes au lieu de +6000 lignes)
const PAGE_RECHERCHE = 'http://www.allocine.fr/recherche/1/?q=';
const DEBUT_RECHERCHE = '<table class="totalwidth noborder purehtml">';
const FIN_RECHERCHE = '</table>';
const PAGE_FICHE = 'http://www.allocine.fr/film/fichefilm_gen_cfilm=';
const DEBUT_FICHE = '<div class="boxbasic breadcrumb">';
const FIN_FICHE = '<div class="morezone">';
// N'hésitez pas a ameliorer les patterns ils sont vraiment mal étudié (enfin pas étudié du tout ^^)
const PATERN_ID_FILM = '/fichefilm_gen_cfilm=([0-9]*)\.html/';
const PATERN_POCHETTE_FILM = '/src=\'(http:\/\/.*)\'alt/';
const PATERN_TITRE_ORIGINAL_FILM = '/alt=\'(.*)\' \/>/';
const PATERN_TITRE_FILM = '/fichefilm_gen_cfilm=[0-9]*\.html\'>(.*)<\/a>/';
const PATERN_INFOS_FILMS = '/<span class="fs11">(.*)<br \/>(.*)<br \/>(.*)<\/span>/';
const PATERN_GENRE = '/Genre:(.*)<\/a><br\/>/';
const PATERN_ANNEE = '/ Année de production :(.*)<\/a><br\/>/';
const PATERN_DUREE = '/<br\/> Dur.*([0-9]{1,2})h([0-9]{2}) min/'; // Bug si ecrit Durée pk ???
const PATERN_NOTE = '/<span class="moreinfo">\(([0-9]{1},[0-9]{1})\)<\/span>/';
const PATERN_DATE_SORTIE_CINE = '/\?week=([0-9]{4}-[0-9]{2}-[0-9]{2})/';
const PATERN_SYNOPSIS = "/Synopsis : <\/span>(.*)<\/p><\/div><\/div><ul id/"; // Bug si chaine de fin plus courte Pk???
//-- Attribut privé
private $reponseAlloCine;
private $reponseAlloCineToJson;
private $ficheFilmAlloCine;
private $ficheFilmAlloCineToJson;
private $ResultatRechercheFilm;
//constructeur inutile ici
public function __construct() { }
/**************************** RECHERCHE FILM(s) ****************************/
/**
* interrogerAllocine : Execute une recherche (longue) sur Allocine
* et renvoi le resultat dans une chaine JSON
*
* @param : $p_nomFilm (string) => nom du film recherché
* @return : (sting/json) => resultatde la recherche
*/
public function interrogerAllocine( $p_nomFilm )
{
// on remplace les espaces par des + et les guillemet pour que les recherches fonctionnent
$p_nomFilm = str_replace ( array(' ', "'"), array('+', "+"), $p_nomFilm);
// ici on a la page de reponse de la recherche
$this->reponseAlloCine = file_get_contents(self::PAGE_RECHERCHE.$p_nomFilm);
// on va cionserver que le strict nécessaire dans la page renvoyer par allocine
self::minimiserReponse();
// On retourne la chaine JSON
return $this->reponseAlloCineToJson;
}
/**
* minimiserReponse : Traite la recherche pour recuperer une seule ligne par film trouvé
*/
private function minimiserReponse()
{
// on retire tout les espaces et saut de lignes (sauf les espaces unique)
$this->reponseAlloCine = preg_replace('/[\s]{2,}/','',$this->reponseAlloCine);
// on recupere la zone de resultat de la recherche
$debutr = strpos($this->reponseAlloCine, self::DEBUT_RECHERCHE)+strlen(self::DEBUT_RECHERCHE);
$finr = strpos($this->reponseAlloCine, self::FIN_RECHERCHE, $debutr);
$chaine = substr($this->reponseAlloCine, $debutr, $finr-$debutr);
// on se sert des commentaire pour faire un saut de ligne entre chaque film
$separateurFilm = '/\<\!-- \/IsItemWithPoster \? --\>\<\!-- \/ResType--\>\<\!-- \/IsInit\?--\>/';
$chaine = preg_replace($separateurFilm,"\n",$chaine);
// la reponse d'allocine est devenu notre chaine
$this->reponseAlloCine = $chaine;
// On va generer la liste
$this->creerListingResultatRecherche();
}
/**
* creerListingResultatRecherche : Recupere les infos essentiel pour l'affichage
*/
private function creerListingResultatRecherche()
{
// on decoupe chaque ligne (chaque film)
$films = explode("\n", $this->reponseAlloCine);
$data = array();
foreach ( $films as $index => $value )
{
$this->ResultatRechercheFilm = $value;
$data[$index]['id'] = (int) $this->idFilm();
$data[$index]['pochette'] = (string) strip_tags( $this->pochetteFilm() );
$data[$index]['titre'] = (string) utf8_encode(html_entity_decode( $this->titreFilm() ));
$data[$index]['titre_original'] = (string) utf8_encode(html_entity_decode( $this->titreOriginalFilm() ));
if($data[$index]['titre'] == '')
$data[$index]['titre'] = $data[$index]['titre_original'];
// Les infos (annee, realisateur, acteurs)
$lesInfos = $this->infosFilm();
$data[$index]['annee'] = (int) $lesInfos[1];
$data[$index]['realisateur'] = (string) addslashes(strip_tags( str_replace('de ','',$lesInfos[2]) ));
$data[$index]['acteurs'] = (string) addslashes(strip_tags( str_replace('avec ','',$lesInfos[3]) ));
}
// on crée la chaine Json de retour
$this->reponseAlloCineToJson = json_encode($data);
}
/**
* Retourne l'id du film
*/
private function idFilm()
{
$retour = 0; // retour par defaut
if( preg_match(self::PATERN_ID_FILM, $this->ResultatRechercheFilm, $id) )
{
$retour = (int) $id[1]; // retour si le preg_match a fonctionné
}
return $retour;
}
/**
* Retourne l'url (allocine) de la pochette du film
*/
private function pochetteFilm()
{
$retour = ''; // retour par defaut
if( preg_match(self::PATERN_POCHETTE_FILM,$this->ResultatRechercheFilm,$url) )
{
$retour = $url[1]; // retour si le preg_match a fonctionné
}
return $retour;
}
/**
* Retourne le titre du film
*/
private function titreFilm()
{
$retour = '';
if( preg_match(self::PATERN_TITRE_FILM,$this->ResultatRechercheFilm,$titre) )
{
$retour = utf8_decode(strip_tags($titre[1]));
}
return $retour;
}
/**
* Retourne le titre original du film
*/
private function titreOriginalFilm()
{
$retour = '';
if( preg_match(self::PATERN_TITRE_ORIGINAL_FILM,$this->ResultatRechercheFilm,$titre_original) )
{
$retour = utf8_decode(strip_tags($titre_original[1]));
}
return $retour;
}
private function infosFilm()
{
$retour = '';
if( preg_match(self::PATERN_INFOS_FILMS,$this->ResultatRechercheFilm,$infos) )
{
$retour = $infos;
}
return $retour;
}
/**************************** FICHE FILM ****************************/
public function ficheAllocine($p_idFilm)
{
// ici on a la page de reponse de la recherche
$this->ficheFilmAlloCine = file_get_contents(self::PAGE_FICHE.$p_idFilm.'.html');
self::minimiserFicheFilm();
// On renvoi la chaine JSON
return $this->reponseAlloCineToJson;
}
/**
* minimiserFicheFilm : Traite le retour pour conserver que la fiche du film
*/
private function minimiserFicheFilm()
{
// on retire tout les espaces et saut de lignes (sauf les espaces unique)
$this->ficheFilmAlloCine = preg_replace('/[\s]{2,}/','',$this->ficheFilmAlloCine);
// on recupere la zone d'affichage
$debutf = strpos($this->ficheFilmAlloCine, self::DEBUT_FICHE)+strlen(self::DEBUT_FICHE);
$finf = strpos($this->ficheFilmAlloCine, self::FIN_FICHE, $debutf);
$chaine = substr($this->ficheFilmAlloCine, $debutf, $finf-$debutf);
// on crée l'attribut de la classe avec la fiche elle même
$this->ficheFilmAlloCine = $chaine;
// c'est ici que sera créé le resultat
self::resultatFicheFilm();
}
private function resultatFicheFilm()
{
$data = array();
$data['date_sortie_cine'] = (string) utf8_encode(html_entity_decode( $this->dateSortieCine() ));
$data['genre'] = (string) utf8_encode(html_entity_decode( $this->genreFilm() ));
$data['duree'] = (int) $this->dureeFilm();
$data['note'] = (float) $this->noteFilm();
$data['synopsis'] = (string) utf8_encode(html_entity_decode( strip_tags($this->synopsis())) );
// on crée la chaine Json de retour
$this->reponseAlloCineToJson = json_encode($data);
}
private function genreFilm()
{
$retour = '';
if(preg_match(self::PATERN_GENRE,$this->ficheFilmAlloCine,$genre))
{
$retour = strip_tags(utf8_decode($genre[1]));
}
return $retour;
}
/**
* Retourne la durée du film en minutes
*/
private function dureeFilm()
{
$retour = array();
if(preg_match(self::PATERN_DUREE,$this->ficheFilmAlloCine,$duree))
{
$duree[1] = (int) strip_tags($duree[1]); // Heure
$duree[2] = (int) strip_tags($duree[2]); // Minute
$retour = ($duree[1]*60)+$duree[2];
}
return $retour;
}
/**
* Retourne la note sur 10 (allocine note sur 5)
* J'additionne la note de la presse avec la note spectateur
* si une des note est a 0 (on peux supposer que ce n'est pas noté alors je multiplie par 2 la note)
*/
private function noteFilm()
{
$retour = '';
if(preg_match_all(self::PATERN_NOTE,$this->ficheFilmAlloCine,$note))
{
$presse = (float) str_replace(',','.', $note[1][0]);
$spectateur = (float) str_replace(',','.', $note[1][1]);
$retour = ( $presse + $spectateur ); // Note presse + Note Spectateur
// si la presse ou les spectateurs n'ont pas noté
if( $presse == 0 or $spectateur == 0)
{
$retour = ($retour * 2);
}
}
return $retour;
}
private function dateSortieCine()
{
$retour = '';
if(preg_match(self::PATERN_DATE_SORTIE_CINE,$this->ficheFilmAlloCine,$sortieCine))
{
$retour = strip_tags(utf8_decode($sortieCine[1]));
}
return $retour;
}
/**
* Retourne le synopsis du film
*/
private function synopsis()
{
$retour = '';
if(preg_match(self::PATERN_SYNOPSIS,$this->ficheFilmAlloCine,$description))
{
$retour = utf8_decode($description[1]);
}
return $retour;
}
} |
Partager