-
-
Save anonymous/0ef3a8522e83185b552ff36eee1ee3c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class mediaMetaData { | |
public $imdb; | |
public $allmusic; | |
private $var = array(); | |
protected $db; | |
public function __construct( $tag, $db ) { | |
$this->setTag( $tag ); | |
$this->db = $db; | |
$this->initialiseVars(); | |
} | |
private function initialiseVars() { | |
switch (true) { | |
case $this->is_imdb(): | |
$this->var["imagePath"] = "./images/movieposters/" . $this->imdb . ".jpg"; | |
$this->var["url"] = "http://www.imdb.com/title/" . $this->imdb . "/"; | |
$this->var["table"] = "imdb"; | |
break; | |
case $this->is_allmusic(): | |
$this->var["imagePath"] = "./images/albumartwork/" . $this->allmusic . ".jpg"; | |
$this->var["url"] = "http://www.allmusic.com/album/" . $this->allmusic . "/"; | |
$this->var["table"] = "allmusic"; | |
break; | |
} | |
} | |
private function setTag( $tag ) { | |
//make sure $imdb is a string so it can be inserted into the db | |
$this->imdb = $tag; | |
$this->allmusic = $tag; | |
switch (true) { | |
case $this->is_imdb(): | |
$this->allmusic = ""; | |
return $this->imdb; | |
break; | |
case $this->is_allmusic(): | |
$this->imdb = ""; | |
return $this->allmusic; | |
break; | |
default: | |
$this->imdb = ""; | |
$this->allmusic = ""; | |
break; | |
} | |
} | |
private function cleanString( $string ) { | |
$cleanString = trim( str_replace( array( " ", "\n" ), "", strip_tags( trim( $string ) ) ) ); | |
return $cleanString; | |
} | |
//methods for checking and comparing. start | |
private function getMatch( $regex, $content ) { | |
preg_match( $regex, $content, $matches ); | |
return $matches[1]; | |
} | |
public function is_imdb() { | |
if( preg_match( "/^tt[0-9]{7}$/", $this->imdb ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function is_allmusic() { | |
if( preg_match( "/^mw[0-9]{10}$/", $this->allmusic ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private function imageExists() { | |
if ( file_exists( $this->var["imagePath"] ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private function contentExists( $string ) { | |
if( strlen( $string ) > 0 ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private function tagExists() { | |
switch (true) { | |
case $this->is_imdb(): | |
$query = $this->db->select( $this->var["table"], 'tag', ['tag' => $this->imdb] ); | |
break; | |
case $this->is_allmusic(): | |
$query = $this->db->select( $this->var["table"], 'tag', ['tag' => $this->allmusic] ); | |
break; | |
default: | |
return false; | |
break; | |
} | |
if( $query ) { | |
if( is_array( $query ) ) { | |
return true; | |
} | |
} | |
} | |
//methods for checking and comparing. end | |
//get and sort the metadata. start | |
private function getData() { | |
$ch = curl_init(); | |
$timeout = 5; | |
curl_setopt( $ch, CURLOPT_URL, $this->var["url"] ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); | |
$data = curl_exec( $ch ); | |
curl_close( $ch ); | |
return $data; | |
} | |
public function sortData() { | |
//get the content from imdb and put it into a array | |
$content = $this->getData(); | |
switch (true) { | |
case $this->is_imdb(): | |
$regex = [ | |
"title" => "/<div class=\"title_wrapper\">.*class=\"\">(.*) <span/isU", | |
"poster" => "/<div class=\"poster.*src=\"(.*)\"/isU", | |
"director" => "/<h4[^>]*>Director:<\/h4>(.*)<\/span>/isU", | |
"plot" => "/<div class=\"plot_summary.*description\">(.*)<\/div>/isU", | |
"rating" => "/<span itemprop=\"ratingValue\">(.*)<\/span>/isU", | |
"metascore" => "/<div class=\"metacriticScore.*<span>(.*)<\/span>/isU", | |
"release_date" => "/Release Date:<\/h4>(.*)<span/isU", | |
"cast" => "/<div class=\"article\" id=\"titleCast\">(.*)See full cast/isU" | |
]; | |
break; | |
case $this->is_allmusic(): | |
$regex = [ | |
"cover" => "/div class=\"album-contain\">.*<img src=\"(.*)\"/isU", | |
"artist" => "/<h2 class=\"album-artist\".*<span itemprop=\"name\">(.*)<\/h2>/isU", | |
"album" => "/<h1 class=\"album-title\" itemprop=\"name\">(.*)<\/h1>/isU", | |
"overview" => "/div class=\"text\" itemprop=\"reviewBody\".*>(.*)<\/div>/isU", | |
"tracklist" => "/<section class=\"track-listing\">.*<div class=\"title\" itemprop=\"name\">(.*)<\/section>/isU", | |
"rating" => "/<span>AllMusic Rating<\/span>(.*)<\/div>/isU" | |
]; | |
break; | |
} | |
$output = array(); | |
foreach ( $regex as $key => $value ) { | |
$$key = $this->getMatch( $value, $content ); | |
$output[$key] = $$key; | |
} | |
return $output; | |
} | |
public function insertData() { | |
if ( !$this->tagExists() ) { | |
// insert the data if the tag is new. | |
$data = $this->sortData(); | |
switch (true) { | |
case $this->is_imdb(): | |
$insertArray = [ | |
"tag" => $this->imdb, | |
"title" => $this->cleanString( $data["title"] ), | |
"plot" => $this->cleanString( $data["plot"] ), | |
"director" => $this->cleanString( $data["director"] ), | |
"rating" => $this->cleanString( $data["rating"] ), | |
"metascore" => $this->cleanString( $data["metascore"] ), | |
"release_date" => $this->cleanString( $data["release_date"] ), | |
"cast" => $this->formatCast(), | |
"timestamp" => time() | |
]; | |
$poster = $data["poster"]; | |
break; | |
case $this->is_allmusic(): | |
$insertArray = [ | |
"tag" => $this->allmusic, | |
"artist" => $this->cleanString( $data["artist"] ), | |
"album" => $this->cleanString( $data["album"] ), | |
"overview" => $this->cleanString( $data["overview"] ), | |
"tracklist" => $this->formatTracklist(), | |
"rating" => $this->cleanString( $data["rating"] ), | |
"timestamp" => time() | |
]; | |
$poster = $data["cover"]; | |
break; | |
} | |
if ( !$this->imageExists() ) { | |
copy( $data["poster"], $this->var["imagePath"] ); | |
} | |
return $this->db->insert( $this->var["table"], $insertArray ); | |
} else { | |
return false; | |
} | |
} | |
public function updateData( $id ) { | |
//update the rating,metascore and timestamp | |
$data = $this->sortData(); | |
if ( !$this->imageExists() ) { | |
copy( $data["cover"], $this->var["imagePath"] ); | |
} | |
switch (true) { | |
case $this->is_imdb(): | |
$updateArray = [ | |
"rating" => $this->cleanString( $data["rating"] ), | |
"metascore" => $this->cleanString( $data["metascore"] ), | |
"timestamp" => time() | |
]; | |
break; | |
case $this->is_allmusic(): | |
$updateArray = [ | |
"rating" => $this->cleanString( $data["rating"] ), | |
"timestamp" => time() | |
]; | |
break; | |
} | |
return $this->db->update( $this->var["table"], $id, $updateArray ); | |
} | |
public function selectData() { | |
switch (true) { | |
case $this->is_imdb(): | |
$data = $this->db->select( $this->var["table"], '*', ['tag' => $this->imdb] ); | |
break; | |
case $this->is_allmusic(): | |
$data = $this->db->select( $this->var["table"], '*', ['tag' => $this->allmusic] ); | |
break; | |
default: | |
return false; | |
break; | |
} | |
return $data; | |
} | |
//get and sort the metadata. end | |
//format and output the data, start. | |
private function formatCast() { | |
//make the cast nice and tidy. | |
$data = explode( "<td class=\"primary_photo\">", $this->sortData()["cast"] ); | |
$count = 1; | |
$limit = 6; | |
foreach ( array_slice( $data, 1, $limit ) as $value ) { | |
$actor = $this->getMatch( "/<span class=\"itemprop\" itemprop=\"name\">(.*)<\/span>/isU", $value ); | |
$character = $this->getMatch( "/<td class=\"character\">(.*)<\/td>/isU", $value ); | |
$output .= $this->cleanString( $actor ) . " -- " . $this->cleanString( $character ); | |
$output .= ( $count < $limit ? "</br>" : "" ); | |
$count++; | |
} | |
return str_replace( "</br>", "\n", $output ); | |
} | |
public function formatTracklist() { | |
$data = explode( "<div class=\"title\" itemprop=\"name\">", $this->sortData()["tracklist"] ); | |
$numItems = count( $data ); | |
$count = 0; | |
foreach ( $data as $value) { | |
$track = $this->getMatch( "/itemprop=\"url\">(.*)<\/a>/isU", $value ); | |
$length = $this->getMatch( "/<\/span>(.*)<meta/isU", $value ); | |
$output .= $this->cleanString( $track ) . " -- " . $this->cleanString( $length ); | |
if ( ++$count !== $numItems ) { | |
$output .= "</br>"; | |
} | |
} | |
return str_replace( "</br>", "\n", $output ); | |
} | |
public function outputData() { | |
switch (true) { | |
case $this->is_imdb(): | |
$data = $this->db->select( $this->var["table"], '*', ['tag' => $this->imdb] ); | |
$order = [ | |
"title" => 0, | |
"plot" => 1, | |
"cast" => 0, | |
"rating" => 1, | |
"metascore" => 1, | |
"release_date" => 1, | |
"director" => 1 | |
]; | |
break; | |
case $this->is_allmusic(): | |
$data = $this->db->select( $this->var["table"], '*', ['tag' => $this->allmusic] ); | |
$order = [ | |
"artist" => 1, | |
"album" => 1, "rating" =>1, | |
"overview" => 0, | |
"tracklist" => 0 | |
]; | |
break; | |
} | |
if( $data ) { | |
//update when viewed once a week. | |
if ( time() > $data["timestamp"]+10 ) { | |
$this->updateData( $data["id"] ); | |
} | |
//format & build the output string. | |
( $this->imageExists() ? $output .= "<img src=\"{$this->var["imagePath"]}\"></br>" : "" ); | |
foreach ( $order as $index => $title ) { | |
if( $this->contentExists( $data[$index] ) ) { | |
if( $title === 1 ) { | |
$output .= "<b>" . ucwords( str_replace( "_", " ", $index ) ) . ":</b> "; | |
} | |
$output .= nl2br( $data[$index] ) . "</br>"; | |
} | |
} | |
return $output; | |
} | |
} | |
//format and output the data, end | |
} | |
/*usage | |
//database | |
table 'imdb' | |
columns 'id' auto inc, 'tag', 'title', 'plot', 'cast', 'director', 'rating', 'metascore', 'release_date', 'timestamp' | |
table 'allmusic' | |
columns 'id' auto inc, 'tag', 'artist', 'album', 'overview', 'tracklist', 'rating', 'timestamp' | |
//insert into the database | |
$medeaMetaData = new medeaMetaData( "tt3450958", $db ); | |
insert $medeaMetaData~>imdb and/or $medeaMetaData->allmusic into a table | |
$medeaMetaData->insertData(); | |
//display imdb data from the database | |
$medeaMetaData = new medeaMetaData( "tt3450958", $db ); | |
echo $medeaMetaData->outputData();*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment