1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-05 19:41:55 +02:00

Modified Tvdb and Tmdb plugins to correctly parse filenames.

This commit is contained in:
wagnered 2015-09-02 21:12:53 -07:00
parent eb8a9d23b0
commit ded0fed1bc
3 changed files with 153 additions and 22 deletions

View file

@ -1002,6 +1002,62 @@ class vainfo
return $parsed;
}
public static function parseFileName($filename, $gather_types)
{
$file = pathinfo($filename,PATHINFO_FILENAME);
if (in_array('tvshow', $gather_types)) {
if (preg_match("~[Ss](\d+)[Ee](\d+)~", $file, $seasonEpisode)) {
$temp = preg_split("~([1|2][0-9]{3})?(((\.|_|\s)[Ss]\d+(\.|_)*[Ee]\d+)|((\.|_|\s)\d+[x|X]\d+))~",$file,2);
preg_match("~[sS](\d+)[eE](\d+)~",$seasonEpisode[0],$tmp);
} else {
if (preg_match("~[\.\s](\d)[xX](\d{2})[\.\s]~", $file, $seasonEpisode)) {
$temp = preg_split("~([1|2][0-9]{3})?[\._\s]\d[xX]\d{2}[\.\s]~",$file,2);
preg_match("~[\.\s](\d)[xX](\d+)[\.\s]~",$seasonEpisode[0],$tmp);
} else {
if (preg_match("~[S|s]eason[-\.\s](\d+)[\.\-\s\,]?\s?[e|E]pisode[\s-\.\s](\d+)[\.\s-]?~", $file, $seasonEpisode)) {
$temp = preg_split("~[\s\.-]?([1|2][0-9]{3})?[\.\s-][S|s]eason[\s-\.\,](\d+)[\.\s-,]?\s?[e|E]pisode[\s-\.](\d+)~",$file,2);
$tmp = $seasonEpisode;
} else {
if (preg_match("~\.(\d)(\d\d)[\.\z]~", $file, $seasonEpisode)) {
$temp = preg_split("~([1|2][0-9]{3})?\.(\d){3}[\.\z]~",$file,3);
preg_match("~\.(\d)(\d\d)\.?~",$seasonEpisode[0],$tmp);
} else {
if (strpos($filename, '/') !== false) {
$slash_type = '~/~';
} else {
$slash_type = "~\\\\~";
}
$matches = preg_split($slash_type,$filename, -1,PREG_SPLIT_DELIM_CAPTURE);
$rmatches = array_reverse($matches);
$episode = preg_split('~^(\d{1,2})~',$rmatches[0], 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$episode[0] = ltrim($episode[0], "0");
preg_match('~\d{1,2}\z~', $rmatches[1], $season);
$season[0] = ltrim($season[0], "0");
$title = ucwords($rmatches[2]);
return [$title, $season[0], $episode[0], null];
}
}
}
}
preg_match("~[1|2][0-9]{3}~", $file, $tyear);
$year = isset($tyear[0]) ? $tyear[0] : null;
$seasonEpisode = array_reverse($tmp);
$episode = ltrim($seasonEpisode[0],"0");
$season = ltrim($seasonEpisode[1],"0");
$title = str_replace(['.','_'], ' ',trim($temp[0], " \t\n\r\0\x0B\.\_"));
return [ucwords($title), $season, $episode, $year];
}
if (in_array('movie', $gather_types)) {
$temp = preg_split("~(\.(\(([12][0-9]{3})\)))?(?(1)|\.[12][0-9]{3})~",$file,2);
$title = str_replace(['.','_'], ' ',trim($temp[0], " \t\n\r\0\x0B\.\_"));
preg_match("~[1|2][0-9]{3}~", $file, $tyear);
$year = isset($tyear[0]) ? $tyear[0] : null;
return [ucwords($title), $year];
}
}
/**
* _parse_filename
*

View file

@ -26,7 +26,7 @@ class AmpacheTmdb {
public $categories = 'metadata';
public $description = 'Tmdb metadata integration';
public $url = 'https://www.themoviedb.org';
public $version = '000001';
public $version = '000002';
public $min_ampache = '370009';
public $max_ampache = '999999';
@ -110,9 +110,25 @@ class AmpacheTmdb {
$title = $media_info['original_name'] ?: $media_info['title'];
$results = array();
$release_info = Vainfo::parseFileName($media_info['file'], $gather_types);
if (empty($release_info[0])) {
debug_event('tmdb', 'Could not parse title, skipped.', '5');
return null;
}
if (in_array('tvshow', $gather_types)) {
$results['tvshow'] = trim($release_info[0]);
$results['tvshow_season'] = $release_info[1];
$results['tvshow_episode'] = $release_info[2];
$results['year'] = $release_info[3];
}
else {
$results['title'] = $release_info[0];
$results['year'] = $release_info[1];
}
if (in_array('movie', $gather_types)) {
if (!empty($title)) {
$apires = $client->getSearchApi()->searchMovies($title);
if (!empty($results['title'])) {
$apires = $client->getSearchApi()->searchMovies($results['title']);
if (count($apires['results']) > 0) {
$results['tmdb_id'] = $apires['results'][0]['id'];
$release = $client->getMoviesApi()->getMovie($results['tmdb_id']);
@ -134,8 +150,8 @@ class AmpacheTmdb {
}
if (in_array('tvshow', $gather_types)) {
if (!empty($media_info['tvshow'])) {
$apires = $client->getSearchApi()->searchTv($media_info['tvshow']);
if (!empty($results['tvshow'])) {
$apires = $client->getSearchApi()->searchTv($results['tvshow']);
if (count($apires['results']) > 0) {
// Get first match
$results['tmdb_tvshow_id'] = $apires['results'][0]['id'];
@ -152,14 +168,14 @@ class AmpacheTmdb {
}
$results['genre'] = self::get_genres($release);
if ($media_info['tvshow_season']) {
$release = $client->getTvSeasonApi()->getSeason($results['tmdb_tvshow_id'], $media_info['tvshow_season']);
if ($results['tvshow_season']) {
$release = $client->getTvSeasonApi()->getSeason($results['tmdb_tvshow_id'], $results['tvshow_season']);
if ($release['id']) {
if ($release['poster_path']) {
$results['tvshow_season_art'] = $imageHelper->getUrl($release['poster_path']);
}
if ($media_info['tvshow_episode']) {
$release = $client->getTvEpisodeApi()->getEpisode($results['tmdb_tvshow_id'], $media_info['tvshow_season'], $media_info['tvshow_episode']);
$release = $client->getTvEpisodeApi()->getEpisode($results['tmdb_tvshow_id'], $results['tvshow_season'], $results['tvshow_episode']);
if ($release['id']) {
$results['tmdb_id'] = $release['id'];
$results['tvshow_season'] = $release['season_number'];
@ -205,6 +221,34 @@ class AmpacheTmdb {
}
return $genres;
}
private function getResultByTitle($results, $title, $gather_type, $year)
{
$titles = array();
foreach ($results as $index)
{
if (in_array('movie', $gather_type)) {
if ((strtoupper($title) == strtoupper($index['title'])) && (strtoupper($index['original_title']) == strtoupper($title))) {
$titles[] = $index;
}
}
else {
if ((strtoupper($title) == strtoupper($index['name'])) && (strtoupper($index['original_name']) == strtoupper($title))) {
$titles[] = $index;
}
}
}
if ((count($titles) > 1) && ($year != null)) {
foreach ($titles as $index)
{
$y = in_array('movie', $gather_type) ? date("Y",strtotime($index['release_date'])) : date("Y",strtotime($index['first_air_date']));
if ($year == $y) {
return $index;
}
}
}
return count($titles) > 0 ? $titles[0] : $results[0];
}
} // end AmpacheTmdb
?>

View file

@ -26,7 +26,7 @@ class AmpacheTvdb {
public $categories = 'metadata';
public $description = 'Tvdb metadata integration';
public $url = 'http://thetvdb.com';
public $version = '000001';
public $version = '000002';
public $min_ampache = '370009';
public $max_ampache = '999999';
@ -103,14 +103,21 @@ class AmpacheTvdb {
try {
$tvdburl = 'http://thetvdb.com';
$client = new Moinax\TvDb\Client($tvdburl, $this->api_key);
$title = $media_info['original_name'] ?: $media_info['title'];
$show_info = Vainfo::parseFileName($media_info['file'], $gather_types);
$results = array();
if (!empty($media_info['tvshow'])) {
$releases = $client->getSeries($media_info['tvshow']);
if (count($releases) > 0) {
$results['tvshow'] = trim($show_info[0]);
$results['tvshow_season'] = $show_info[1];
$results['tvshow_episode'] = $show_info[2];
$results['year'] = $show_info[3];
$title = $media_info['original_name'] ?: $media_info['title'];
if ($results['tvshow']) {
$releases = $client->getSeries($results['tvshow']);
if (count($releases) == 0) {
throw new Exception("TV Show not found");
}
// Get first match
$release = $releases[0];
$release = $this->getReleaseByTitle($releases, $results['tvshow'], $results['year']);
$results['tvdb_tvshow_id'] = $release->id;
$results['tvshow_imdb_id'] = $release->imdbId ;
$results['summary'] = $release->overview;
@ -121,8 +128,10 @@ class AmpacheTvdb {
if ($release->banner) {
$results['tvshow_banner_art'] = $tvdburl . '/banners/' . $release->banner;
}
if (count($results->genres) > 0) {
$results['genre'] = $results->genres;
$baseSeries = $client->getSerie($results['tvdb_tvshow_id']);
if (count($baseSeries->genres) > 0) {
$results['genre'] = $baseSeries->genres;
}
$banners = $client->getBanners($results['tvdb_tvshow_id']);
@ -142,8 +151,8 @@ class AmpacheTvdb {
}
}
if ($media_info['tvshow_season'] && $media_info['tvshow_episode']) {
$release = $client->getEpisode($results['tvdb_tvshow_id'], $media_info['tvshow_season'], $media_info['tvshow_episode']);
if ($results['tvshow_season'] && $results['tvshow_episode']) {
$release = $client->getEpisode($results['tvdb_tvshow_id'], $results['tvshow_season'], $results['tvshow_episode']);
if ($release->id) {
$results['tvdb_id'] = $release->id;
$results['tvshow_season'] = $release->season;
@ -160,7 +169,6 @@ class AmpacheTvdb {
}
}
}
}
}
} catch (Exception $e) {
debug_event('tvdb', 'Error getting metadata: ' . $e->getMessage(), '1');
@ -175,5 +183,28 @@ class AmpacheTvdb {
return Art::gather_metadata_plugin($this, $type, $options);
}
private function getReleaseByTitle($results, $title, $year)
{
$titles = array();
foreach ($results as $index)
{
$pos = strpos($index->name, $title);
if ($pos !== false) {
$titles[] = $index;
}
}
if ((count($titles) > 1) && ($year != null)) {
foreach ($titles as $index)
{
$y = $index->firstAired->format('Y');
if ($year == $y) {
return $index;
}
}
}
return count($titles) > 0 ? $titles[0] : $results[0];
}
} // end AmpacheTvdb
?>