/** * This function uses the file and directory patterns to pull out extra tag * information. * parses TV show name variations: * 1. title.[date].S#[#]E#[#].ext (Upper/lower case) * 2. title.[date].#[#]X#[#].ext (both upper/lower case letters * 3. title.[date].Season #[#] Episode #[#].ext * 4. title.[date].###.ext (maximum of 9 seasons) * parse directory path for name, season and episode numbers * /TV shows/show name [(year)]/[season ]##/##.Episode.Title.ext * parse movie names: * title.[date].ext * /movie title [(date)]/title.ext */ private function _parse_filename($filepath) { $origin = $filepath; $results = array(); if (strpos($filepath, '/') !== false) { $slash_type = '/'; $slash_type_preg = $slash_type; } else { $slash_type = "\\"; $slash_type_preg = $slash_type . $slash_type; } $file = pathinfo($filepath, PATHINFO_FILENAME); if (in_array('tvshow', $this->gather_types)) { $season = array(); $episode = array(); $tvyear = array(); $temp = array(); preg_match("~(?<=\\(\\[\\<\\{)[1|2][0-9]{3}|[1|2][0-9]{3}~", $filepath, $tvyear); $results['year'] = !empty($tvyear) ? intval($tvyear[0]) : null; if (preg_match("~[Ss](\\d+)[Ee](\\d+)~", $file, $seasonEpisode)) { $temp = preg_split("~(((\\.|_|\\s)[Ss]\\d+(\\.|_)*[Ee]\\d+))~", $file, 2); preg_match("~(?<=[Ss])\\d+~", $file, $season); preg_match("~(?<=[Ee])\\d+~", $file, $episode); } else { if (preg_match("~[\\_\\-\\.\\s](\\d{1,2})[xX](\\d{1,2})~", $file, $seasonEpisode)) { $temp = preg_split("~[\\.\\_\\s\\-\\_]\\d+[xX]\\d{2}[\\.\\s\\-\\_]*|\$~", $file); preg_match("~\\d+(?=[Xx])~", $file, $season); preg_match("~(?<=[Xx])\\d+~", $file, $episode); } else { if (preg_match("~[S|s]eason[\\_\\-\\.\\s](\\d+)[\\.\\-\\s\\_]?\\s?[e|E]pisode[\\s\\-\\.\\_]?(\\d+)[\\.\\s\\-\\_]?~", $file, $seasonEpisode)) { $temp = preg_split("~[\\.\\s\\-\\_][S|s]eason[\\s\\-\\.\\_](\\d+)[\\.\\s\\-\\_]?\\s?[e|E]pisode[\\s\\-\\.\\_](\\d+)([\\s\\-\\.\\_])*~", $file, 3); preg_match("~(?<=[Ss]eason[\\.\\s\\-\\_])\\d+~", $file, $season); preg_match("~(?<=[Ee]pisode[\\.\\s\\-\\_])\\d+~", $file, $episode); } else { if (preg_match("~[\\_\\-\\.\\s](\\d)(\\d\\d)[\\_\\-\\.\\s]*~", $file, $seasonEpisode)) { $temp = preg_split("~[\\.\\s\\-\\_](\\d)(\\d\\d)[\\.\\s\\-\\_]~", $file); $season[0] = $seasonEpisode[1]; if (preg_match("~[\\_\\-\\.\\s](\\d)(\\d\\d)[\\_\\-\\.\\s]~", $file, $seasonEpisode)) { $temp = preg_split("~[\\.\\s\\-\\_](\\d)(\\d\\d)[\\.\\s\\-\\_]~", $file); $season[0] = $seasonEpisode[1]; $episode[0] = $seasonEpisode[2]; } } } } } $results['tvshow_season'] = $season[0]; $results['tvshow_episode'] = $episode[0]; $results['tvshow'] = $this->formatVideoName($temp[0]); $results['original_name'] = $this->formatVideoName($temp[1]); // Try to identify the show information from parent folder if (!$results['tvshow']) { $folders = preg_split("~" . $slash_type . "~", $filepath, -1, PREG_SPLIT_NO_EMPTY); if ($results['tvshow_season'] && $results['tvshow_episode']) { // We have season and episode, we assume parent folder is the tvshow name $filetitle = end($folders); $results['tvshow'] = $this->formatVideoName($filetitle); } else { // Or we assume each parent folder contains one missing information if (preg_match('/[\\/\\\\]([^\\/\\\\]*)[\\/\\\\]Season (\\d{1,2})[\\/\\\\]((E|Ep|Episode)\\s?(\\d{1,2})[\\/\\\\])?/i', $filepath, $matches)) { if ($matches != null) { $results['tvshow'] = $this->formatVideoName($matches[1]); $results['tvshow_season'] = $matches[2]; if (isset($matches[5])) { $results['tvshow_episode'] = $matches[5]; } else { //match pattern like 10.episode name.mp4 if (preg_match("~^(\\d\\d)[\\_\\-\\.\\s]?(.*)~", $file, $matches)) { $results['tvshow_episode'] = $matches[1]; $results['original_name'] = $this->formatVideoName($matches[2]); } else { //Fallback to match any 3-digit Season/Episode that fails the standard pattern above. preg_match("~(\\d)(\\d\\d)[\\_\\-\\.\\s]?~", $file, $matches); $results['tvshow_episode'] = $matches[2]; } } } } } } $results['title'] = $results['tvshow']; } if (in_array('movie', $this->gather_types)) { $results['original_name'] = $results['title'] = $this->formatVideoName($file); } if (in_array('music', $this->gather_types) || in_array('clip', $this->gather_types)) { // Combine the patterns $pattern = preg_quote($this->_dir_pattern) . $slash_type_preg . preg_quote($this->_file_pattern); // Remove first left directories from filename to match pattern $cntslash = substr_count($pattern, preg_quote($slash_type)) + 1; $filepart = explode($slash_type, $filepath); if (count($filepart) > $cntslash) { $filepath = implode($slash_type, array_slice($filepart, count($filepart) - $cntslash)); } // Pull out the pattern codes into an array preg_match_all('/\\%\\w/', $pattern, $elements); // Mangle the pattern by turning the codes into regex captures $pattern = preg_replace('/\\%[Ty]/', '([0-9]+?)', $pattern); $pattern = preg_replace('/\\%\\w/', '(.+?)', $pattern); $pattern = str_replace('/', '\\/', $pattern); $pattern = str_replace(' ', '\\s', $pattern); $pattern = '/' . $pattern . '\\..+$/'; // Pull out our actual matches preg_match($pattern, $filepath, $matches); if ($matches != null) { // The first element is the full match text $matched = array_shift($matches); debug_event('vainfo', $pattern . ' matched ' . $matched . ' on ' . $filepath, 5); // Iterate over what we found foreach ($matches as $key => $value) { $new_key = translate_pattern_code($elements['0'][$key]); if ($new_key) { $results[$new_key] = $value; } } $results['title'] = $results['title'] ?: basename($filepath); if ($this->islocal) { $results['size'] = Core::get_filesize(Core::conv_lc_file($origin)); } } } return $results; }
/** * _parse_filename * * This function uses the file and directory patterns to pull out extra tag * information. */ private function _parse_filename($filename) { $origin = $filename; $results = array(); if (in_array('music', $this->gather_types) || in_array('clip', $this->gather_types)) { // Correctly detect the slash we need to use here if (strpos($filename, '/') !== false) { $slash_type = '/'; $slash_type_preg = $slash_type; } else { $slash_type = '\\'; $slash_type_preg = $slash_type . $slash_type; } // Combine the patterns $pattern = preg_quote($this->_dir_pattern) . $slash_type_preg . preg_quote($this->_file_pattern); // Remove first left directories from filename to match pattern $cntslash = substr_count($pattern, preg_quote($slash_type)) + 1; $filepart = explode($slash_type, $filename); if (count($filepart) > $cntslash) { $filename = implode($slash_type, array_slice($filepart, count($filepart) - $cntslash)); } // Pull out the pattern codes into an array preg_match_all('/\\%\\w/', $pattern, $elements); // Mangle the pattern by turning the codes into regex captures $pattern = preg_replace('/\\%[Ty]/', '([0-9]+?)', $pattern); $pattern = preg_replace('/\\%\\w/', '(.+?)', $pattern); $pattern = str_replace('/', '\\/', $pattern); $pattern = str_replace(' ', '\\s', $pattern); $pattern = '/' . $pattern . '\\..+$/'; // Pull out our actual matches preg_match($pattern, $filename, $matches); if ($matches != null) { // The first element is the full match text $matched = array_shift($matches); debug_event('vainfo', $pattern . ' matched ' . $matched . ' on ' . $filename, 5); // Iterate over what we found foreach ($matches as $key => $value) { $new_key = translate_pattern_code($elements['0'][$key]); if ($new_key) { $results[$new_key] = $value; } } $results['title'] = $results['title'] ?: basename($filename); if ($this->islocal) { $results['size'] = Core::get_filesize(Core::conv_lc_file($origin)); } } } if (in_array('tvshow', $this->gather_types)) { $pathinfo = pathinfo($filename); $filetitle = $pathinfo['filename']; $results = array_merge($results, $this->parseEpisodeName($filetitle)); if (!$results['tvshow']) { // Try to identify the show information from parent folder $filetitle = basename($pathinfo['dirname']); $results = array_merge($results, $this->parseEpisodeName($filetitle)); if (!$results['tvshow']) { if ($results['tvshow_season'] && $results['tvshow_episode']) { // We have season and episode, we assume parent folder is the tvshow name $pathinfo = pathinfo($pathinfo['dirname']); $filetitle = basename($pathinfo['dirname']); $results['tvshow'] = $this->fixSerieName($filetitle); } else { // Or we assume each parent folder contains one missing information if (preg_match('/[\\/\\\\]([^\\/\\\\]*)[\\/\\\\]Season (\\d{1,2})[\\/\\\\]((E|Ep|Episode)\\s?(\\d{1,2})[\\/\\\\])?/i', $filename, $matches)) { if ($matches != null) { $results['tvshow'] = $this->fixSerieName($matches[1]); $results['tvshow_season'] = $matches[2]; if (isset($matches[5])) { $results['tvshow_episode'] = $matches[5]; } } } } } } } if (in_array('movie', $this->gather_types)) { $pathinfo = pathinfo($filename); $filetitle = $pathinfo['filename']; $results['title'] = $this->fixVideoReleaseName($filetitle); if (!$results['title']) { // Try to identify the movie information from parent folder $filetitle = basename($pathinfo['dirname']); $results['title'] = $this->fixVideoReleaseName($filetitle); } } return $results; }