Skip to content

Commit

Permalink
Fixed #8
Browse files Browse the repository at this point in the history
  • Loading branch information
smad2005 committed Jan 24, 2016
1 parent 5024d69 commit c54cc23
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
29 changes: 28 additions & 1 deletion Tests/searchloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ function testHandleSubtitleFile_CantfindWithoutQuotesWithBody_FindWithQuotes() {
$this->assertEquals(1, searchloadTest::$downloadCount);
}

function testHandleSubtitleFile_WrongName_BuildHtml() {
$filename = "test";
$this->createFiles(array($filename . ".ass"));
$subtitlesList = initSubtitlesList(TEST_DIR);
$html = null;
foreach ($subtitlesList as $sub) {
$html.=handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(getSearchLink($filename), $html);
$this->assertEquals(0, searchloadTest::$downloadCount);
}

function testHandleSubtitleFile_WrongNameWithBody_BuildHtml() {
$filename = "test";
$newFilename = "test2";
$subname = $filename . ".ass";
$badsubPath = $this->getFullPath($subname);
$this->createFiles(array($subname));
file_put_contents($badsubPath, 'Video File: ' . $newFilename);
$subtitlesList = initSubtitlesList(TEST_DIR);
$html = null;
foreach ($subtitlesList as $sub) {
$html.=handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(getSearchLink($newFilename), $html);
$this->assertEquals(0, searchloadTest::$downloadCount);
}

function testHandleSubtitleFile() {
$this->createFiles(array(base64_decode('W0hvcnJpYmxlU3Vic10gRmF0ZSBLYWxlaWQgTGluZXIgUFJJU01BIElMWUEgMndlaSBIZXJ6ISAtIDA2IFs3MjAuYXNz'), '4.srt', '5.ass'));
$badsubPath = $this->getFullPath("5.ass");
Expand Down Expand Up @@ -137,7 +165,6 @@ function testChr_utf8() {
}

function testHtmlentities2utf8() {
echo htmlentities2utf8(" ");
$this->assertEquals(' ', htmlentities2utf8(" "));
$this->assertEquals('¡', htmlentities2utf8("¡"));
$this->assertEquals('¢', htmlentities2utf8("¢"));
Expand Down
35 changes: 20 additions & 15 deletions searchload.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
foreach ($subtitlesList as $sub) {
$tmpSearchHTML.= handleSubtitleFile($dirInfo, $sub, $context);
}


if ($tmpSearchHTML) {
// Вывод ссылок на поиск для файлов, которые небыли найдены
// Вывод ссылок на поиск для файлов, которые небыли найдены
file_put_contents($tmpFl = mktmpfile('htm'), $tmpSearchHTML);
system($tmpFl);
}
Expand Down Expand Up @@ -96,15 +94,13 @@ function initConfig($jsonConfigPath) {
define('TORCLI', $torrentPath); // Путь к торрент-клиенту
}

function getLinkName($torname) {
$name = $torname["name"];
return rawurlencode($name);
function getLinkName($name) {
return LINKSUFFIX . rawurlencode($name);
}

function hasFoundAnime($dirInfo, $torname, $context, &$linkmath, &$html) {
$linkname = getLinkName($torname);
$urlPath = LINKSUFFIX . $linkname;
$html = @file_get_contents($urlPath, false, $context) or ( $html = file_get_contents($urlPath));
$linkname = getLinkName($torname["name"]);
$html = @file_get_contents($linkname, false, $context) or ( $html = file_get_contents($linkname));
return preg_match('~<div class="viewdownloadbutton">\s*<a href="([^"]*tid=(\d+)[^"]*)~', $html, $linkmath);
}

Expand Down Expand Up @@ -145,9 +141,11 @@ function renameWithNameFromSub($dirInfo, &$torname) {
$dirnameRaw = $dirInfo["dirnameRaw"];
$fullname = $dirnameRaw . "/$torname[name].ass";
$nameFromAss = get_path_without_ext(tryGetNameFromASS($fullname));
if ($nameFromAss != $torname['name']) {
if ($nameFromAss != null && $nameFromAss != $torname['name']) {
$torname = array("name" => $nameFromAss, "oldname" => $fullname, "ext" => $torname["ext"]);
return true;
}
return false;
}

function handleSubtitleFile($dirInfo, $torname, $context) {
Expand All @@ -156,19 +154,26 @@ function handleSubtitleFile($dirInfo, $torname, $context) {
addQuotes($torname);
if (!HasFoundAndDownloaded($dirInfo, $torname, $context)) { //with quotes
removeQutes($torname);
renameWithNameFromSub($dirInfo, $torname);
if (!HasFoundAndDownloaded($dirInfo, $torname, $context)) { //with name from sub
$foundInSub = renameWithNameFromSub($dirInfo, $torname);
if ($foundInSub && !HasFoundAndDownloaded($dirInfo, $torname, $context)) { //with name from sub
addQuotes($torname);
if (!HasFoundAndDownloaded($dirInfo, $torname, $context)) { //with name from sub with quotes
// Файл не найден - будем выводить ссылку на поиск
$linkname = getLinkName($torname);
return '<p><a target="_blank" href="' . LINKSUFFIX . $linkname . '">' . $torname["name"] . '</a></p>';
// Файл не найден - будем выводить ссылку на поиск
removeQutes($torname);
return getSearchLink($torname["name"]);
}
} else {
// Файл не найден - будем выводить ссылку на поиск
return getSearchLink($torname["name"]);
}
}
}
}

function getSearchLink($name) {
return '<p><a target="_blank" href="' . getLinkName($name) . '">' . $name . '</a></p>';
}

function addQuotes(&$torname) {
$torname["name"] = '"' . $torname["name"] . '"';
}
Expand Down

0 comments on commit c54cc23

Please sign in to comment.