Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
Search fulltext (#10)
Browse files Browse the repository at this point in the history
* Improves search of subtitles

Adds a search by fulltext.

* Bump version
  • Loading branch information
jonag authored Nov 1, 2016
1 parent f9100ce commit c578553
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bin/episodes
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set_time_limit(0);
*/
$loader = require __DIR__.'/../vendor/autoload.php';

$application = new \jonag\Episodes\Application('Episodes', '2.0');
$application = new \jonag\Episodes\Application('Episodes', '2.1');

$container = $application->getContainer();
$container['pdo'] = function () {
Expand Down
44 changes: 35 additions & 9 deletions src/jonag/Episodes/Command/SearchSubtitlesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace jonag\Episodes\Command;

use jonag\Episodes\Helper\EpisodeHelper;
use jonag\OpenSubtitlesSDK\Client;
use jonag\OpenSubtitlesSDK\Exception\OpenSubtitlesException;
use jonag\OpenSubtitlesSDK\Helper\Hash;
Expand Down Expand Up @@ -58,10 +59,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
$hash = Hash::calculateHash($filePath);
$progressBar->advance();

$searchOptions = [
'hash' => [
'movieHash' => $hash,
'movieSize' => filesize($filePath)
],
];

$episode = EpisodeHelper::parseFileName($fileInfo->getBasename('.'.$fileInfo->getExtension()));
if ($episode !== false) {
$searchOptions['query'] = [
'showName' => $episode->getShowName(),
'season' => $episode->getSeason(),
'episode' => $episode->getEpisode(),
];
}

/** @var Client $osClient */
$osClient = $container['osClient'];
try {
$subtitles = $osClient->getSubtitles('eng', $hash, filesize($filePath));
$subtitles = $osClient->getSubtitles('eng', $searchOptions);
$progressBar->advance();
} catch (OpenSubtitlesException $e) {
$progressBar->finish();
Expand All @@ -70,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 4;
}

$link = $this->findBestSubtitle($fileInfo->getFilename(), $subtitles);
$link = $this->findBestSubtitle($subtitles, $episode);
if ($link === null) {
$progressBar->finish();
$io->warning('Unable to find matching subtitles');
Expand Down Expand Up @@ -104,11 +121,11 @@ protected function execute(InputInterface $input, OutputInterface $output)


/**
* @param $file
* @param $subtitles
* @return string|null
* @param array $subtitles
* @param EpisodeHelper|false $episode
* @return null|string
*/
protected function findBestSubtitle($file, $subtitles)
protected function findBestSubtitle($subtitles, $episode)
{
$bestScore = -1;
$bestDownloadsCount = -1;
Expand All @@ -119,13 +136,22 @@ protected function findBestSubtitle($file, $subtitles)
continue;
}

if ($episode !== false
&& $subtitle['MatchedBy'] === 'fulltext'
&& $episode->getTeam() !== null
&& strpos($subtitle['MovieReleaseName'], $episode->getTeam()) === false) {
continue;
}

$score = 0;

if ($subtitle['UserRank'] === 'trusted' || $subtitle['UserRank'] === 'administrator') {
$score += 4;
if ($subtitle['MatchedBy'] === 'moviehash') {
$score += 10;
}

if ($subtitle['UserRank'] === 'platinum member' || $subtitle['UserRank'] === 'gold member') {
if ($subtitle['UserRank'] === 'trusted' || $subtitle['UserRank'] === 'administrator') {
$score += 4;
} elseif ($subtitle['UserRank'] === 'platinum member' || $subtitle['UserRank'] === 'gold member') {
$score += 3;
}

Expand Down
18 changes: 15 additions & 3 deletions src/jonag/Episodes/Helper/EpisodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

class EpisodeHelper
{
const PATTERN = '/^(?<showName>.+)[\. ]S?(?<season>\d+)[Ex](?<episode>\d+).*?(?<source>\[\w+\])?$/i';
const PATTERN = '/^(?<showName>.+)[\. ]S?(?<season>\d+)[Ex](?<episode>\d+).*?(?:-(?<team>\w+))?(?<source>\[\w+\])?$/i';

private $showName;
private $season;
private $episode;
private $proper;
private $releaseName;
private $sample;
private $team;

/**
* EpisodeHelper constructor.
Expand All @@ -22,15 +23,17 @@ class EpisodeHelper
* @param string $releaseName
* @param boolean $isProper
* @param boolean $isSample
* @param string $team
*/
public function __construct($showName, $season, $episode, $releaseName, $isProper, $isSample)
public function __construct($showName, $season, $episode, $releaseName, $isProper, $isSample, $team)
{
$this->showName = $showName;
$this->season = $season;
$this->episode = $episode;
$this->releaseName = $releaseName;
$this->proper = $isProper;
$this->sample = $isSample;
$this->team = $team;
}

/**
Expand All @@ -44,10 +47,11 @@ public static function parseFileName($fileName)
$season = (int) $matches['season'];
$episode = $matches['episode'];
$releaseName = isset($matches['source']) ? str_replace($matches['source'], '', $fileName) : $fileName;
$team = isset($matches['team']) ? $matches['team'] : null;
$isProper = stripos($releaseName, 'PROPER') !== false || stripos($releaseName, 'REPACK') !== false;
$isSample = stripos($releaseName, 'SAMPLE') !== false;

return new EpisodeHelper($showName, $season, $episode, $releaseName, $isProper, $isSample);
return new EpisodeHelper($showName, $season, $episode, $releaseName, $isProper, $isSample, $team);
} else {
return false;
}
Expand Down Expand Up @@ -100,4 +104,12 @@ public function isSample()
{
return $this->sample;
}

/**
* @return string
*/
public function getTeam()
{
return $this->team;
}
}
10 changes: 5 additions & 5 deletions src/jonag/Episodes/Tests/Helper/EpisodeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function testParse($fileName, $expected)
public function fileNameProvider()
{
return [
['Angie.Tribeca.S02E02.720p.HDTV.X264-DIMENSION', ['showName' => 'Angie Tribeca', 'season' => 2, 'episode' => 2, 'releaseName' => 'Angie.Tribeca.S02E02.720p.HDTV.X264-DIMENSION', 'proper' => false, 'sample' => false]],
['Angie.Tribeca.S02E02.720p.HDTV.X264-DIMENSION', ['showName' => 'Angie Tribeca', 'season' => 2, 'episode' => 2, 'releaseName' => 'Angie.Tribeca.S02E02.720p.HDTV.X264-DIMENSION', 'proper' => false, 'sample' => false, 'team' => 'DIMENSION']],
['Angie.Tribeca.720p.HDTV.X264-DIMENSION', false],
['angie.tribeca.S01E08.720p.HDTV.X264-DIMENSION', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 8, 'releaseName' => 'angie.tribeca.S01E08.720p.HDTV.X264-DIMENSION', 'proper' => false, 'sample' => false]],
['Angie.Tribeca.S01E07.720p.HDTV.X264-DIMENSION[rarbg]', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 7, 'releaseName' => 'Angie.Tribeca.S01E07.720p.HDTV.X264-DIMENSION', 'proper' => false, 'sample' => false]],
['angie.tribeca.S01E08.720p.HDTV.X264-FLEET', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 8, 'releaseName' => 'angie.tribeca.S01E08.720p.HDTV.X264-FLEET', 'proper' => false, 'sample' => false, 'team' => 'FLEET']],
['Angie.Tribeca.S01E07.720p.HDTV.X264-DIMENSION[rarbg]', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 7, 'releaseName' => 'Angie.Tribeca.S01E07.720p.HDTV.X264-DIMENSION', 'proper' => false, 'sample' => false, 'team' => 'DIMENSION']],
['Angie.Tribeca.S01E06.PROPER.720p.HDTV.X264-DIMENSION', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 6, 'releaseName' => 'Angie.Tribeca.S01E06.PROPER.720p.HDTV.X264-DIMENSION', 'proper' => true, 'sample' => false]],
['Angie.Tribeca.S01E05.720p.HDTV.X264-DIMENSION-sample', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 5, 'releaseName' => 'Angie.Tribeca.S01E05.720p.HDTV.X264-DIMENSION-sample', 'proper' => false, 'sample' => true]],
['The.Flash.S03E01', ['showName' => 'The Flash', 'season' => 3, 'episode' => 1, 'releaseName' => 'The.Flash.S03E01', 'proper' => false, 'sample' => false]]
['Angie.Tribeca.S01E05.720p.HDTV.X264.SAMPLE-DIMENSION', ['showName' => 'Angie Tribeca', 'season' => 1, 'episode' => 5, 'releaseName' => 'Angie.Tribeca.S01E05.720p.HDTV.X264.SAMPLE-DIMENSION', 'proper' => false, 'sample' => true, 'team' => 'DIMENSION']],
['The.Flash.S03E01', ['showName' => 'The Flash', 'season' => 3, 'episode' => 1, 'releaseName' => 'The.Flash.S03E01', 'proper' => false, 'sample' => false, 'team' => null]]
];
}
}
35 changes: 27 additions & 8 deletions src/jonag/OpenSubtitlesSDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,38 @@ public function logOut()
}
}

public function getSubtitles($language, $movieHash, $movieSize)
public function getSubtitles($language, $options)
{
$search = [];
if (isset($options['hash'])) {
$search[] = [
'sublanguageid' => $language,
'moviehash' => $options['hash']['movieHash'],
'moviebytesize' => $options['hash']['movieSize']
];
}

if (isset($options['tag'])) {
$search[] = [
'sublanguageid' => $language,
'tag' => $options['tag']['releaseName'],
];
}

if (isset($options['query'])) {
$search[] = [
'sublanguageid' => $language,
'query' => $options['query']['showName'],
'season' => $options['query']['season'],
'episode' => $options['query']['episode']
];
}

$result = $this->call(
'SearchSubtitles',
[
$this->getToken(),
[
[
'sublanguageid' => $language,
'moviehash' => $movieHash,
'moviebytesize' => $movieSize,
],
],
$search,
]
);

Expand Down

0 comments on commit c578553

Please sign in to comment.