Skip to content

Commit

Permalink
Fixed #5
Browse files Browse the repository at this point in the history
  • Loading branch information
smad2005 committed Jan 23, 2016
1 parent 0eb9ddd commit 5024d69
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 56 deletions.
39 changes: 17 additions & 22 deletions Tests/searchloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ function sendToTorrentFake($runComand) {
class searchloadTest extends PHPUnit_Framework_TestCase {

private $jsonConfigPath = "Tests/config.json";
private $dirInfo;
private static $downloadCount;

static function sendToTorrentFake($runComand) {
searchloadTest::$downloadCount++;
}

protected function setUp() {
$this->dirInfo = array("dirnameRaw" => TEST_DIR, "dirname" => escapeshellarg(TEST_DIR));
searchloadTest::$downloadCount = 0;
$this->deleteTestFolder();
}
Expand All @@ -30,6 +32,10 @@ function getFullPath($path) {
return TEST_DIR . '/' . $path;
}

function getContext() {
return stream_context_create(array('http' => array('timeout' => 20000, 'user_agent' => USERAGENT)));
}

function createFiles($list) {
if (!file_exists(TEST_DIR)) {
mkdir(TEST_DIR);
Expand Down Expand Up @@ -77,40 +83,32 @@ function testInitSubtitlesList() {
}

function testHandleSubtitleFile_CantfindWithoutQuotes_FindWithQuotes() {
$this->createFiles(array("[HorribleSubs] GATE - 13 [720p].ass"));
$this->createFiles(array(base64_decode("W0hvcnJpYmxlU3Vic10gR0FURSAtIDEzIFs3MjBwXS5hc3M=")));
$subtitlesList = initSubtitlesList(TEST_DIR);
$dirInfo = array("dirnameRaw" => TEST_DIR, "dirname" => escapeshellarg(TEST_DIR));
$context = stream_context_create(array('http' => array('timeout' => 20000, 'user_agent' => USERAGENT)));
$torrents = array();
foreach ($subtitlesList as $sub)
handleSubtitleFile($dirInfo, $sub, $context);
foreach ($subtitlesList as $sub) {
handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(1, searchloadTest::$downloadCount);
}

function testHandleSubtitleFile_CantfindWithoutQuotesWithBody_FindWithQuotes() {
$filename = base64_decode("W0hvcnJpYmxlU3Vic10gR0FURSAtIDEzIFs3MjBwXS5hc3M=");
$filename = "1.ass";
$this->createFiles(array($filename));
file_put_contents($this->getFullPath($filename), 'Video File: ' . $filename);
file_put_contents($this->getFullPath($filename), 'Video File: ' . base64_decode("W0hvcnJpYmxlU3Vic10gR0FURSAtIDEzIFs3MjBwXS5hc3M="));
$subtitlesList = initSubtitlesList(TEST_DIR);
$dirInfo = array("dirnameRaw" => TEST_DIR, "dirname" => escapeshellarg(TEST_DIR));
$context = stream_context_create(array('http' => array('timeout' => 20000, 'user_agent' => USERAGENT)));
$torrents = array();
foreach ($subtitlesList as $sub)
handleSubtitleFile($dirInfo, $sub, $context);
foreach ($subtitlesList as $sub) {
handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(1, searchloadTest::$downloadCount);
}

function testHandleSubtitleFile() {

$this->createFiles(array(base64_decode('W0hvcnJpYmxlU3Vic10gRmF0ZSBLYWxlaWQgTGluZXIgUFJJU01BIElMWUEgMndlaSBIZXJ6ISAtIDA2IFs3MjAuYXNz'), '4.srt', '5.ass'));
$badsubPath = $this->getFullPath("5.ass");
file_put_contents($badsubPath, 'Video File: ' . base64_decode('W09oeXMtUmF3c10gUHJpc29uIFNjaG9vbCAtIDA5IChNWCAxMjgweDcyMCB4MjY0IEFBQykubXA0'));
$subtitlesList = initSubtitlesList(TEST_DIR);
$dirInfo = array("dirnameRaw" => TEST_DIR, "dirname" => escapeshellarg(TEST_DIR));
$context = stream_context_create(array('http' => array('timeout' => 20000, 'user_agent' => USERAGENT)));
$torrents = array();
foreach ($subtitlesList as $sub) {
handleSubtitleFile($dirInfo, $sub, $context);
handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(2, searchloadTest::$downloadCount);
$this->assertFileNotExists($badsubPath);
Expand All @@ -121,11 +119,8 @@ function testHandleSubtitleFile_wrongVideoFile_fixedName() {
$badsubPath = $this->getFullPath("5.ass");
file_put_contents($badsubPath, 'Video File: ' . base64_decode('W09oeXMtUmF3c10gVmFsa3lyaWUgRHJpdmUgTWVybWFpZCAtIDEx'));
$subtitlesList = initSubtitlesList(TEST_DIR);
$dirInfo = array("dirnameRaw" => TEST_DIR, "dirname" => escapeshellarg(TEST_DIR));
$context = stream_context_create(array('http' => array('timeout' => 20000, 'user_agent' => USERAGENT)));
$torrents = array();
foreach ($subtitlesList as $sub) {
handleSubtitleFile($dirInfo, $sub, $context);
handleSubtitleFile($this->dirInfo, $sub, $this->getContext());
}
$this->assertEquals(1, searchloadTest::$downloadCount);
$this->assertFileExists(TEST_DIR . '/' . base64_decode('W09oeXMtUmF3c10gVmFsa3lyaWUgRHJpdmUgTWVybWFpZCAtIDExIChBVC1YIDEyODB4NzIwIHgyNjQgQUFDKS5hc3M='));
Expand Down
105 changes: 71 additions & 34 deletions searchload.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function initSubtitlesList($dirname) {
}
}
if (!$videoFound) {
$subtitlesList[] = array("name" => $subsMatch[1], "flag" => 0, "ext" => $subsMatch[2], "needQuotes" => 0);
$subtitlesList[] = array("name" => $subsMatch[1], "ext" => $subsMatch[2]);
}
}
}
Expand All @@ -96,50 +96,87 @@ function initConfig($jsonConfigPath) {
define('TORCLI', $torrentPath); // Путь к торрент-клиенту
}

function handleSubtitleFile($dirInfo, $torname, $context) {
$dirnameRaw = $dirInfo["dirnameRaw"];
$dirname = $dirInfo["dirname"];
function getLinkName($torname) {
$name = $torname["name"];
$linkname = rawurlencode($name);
return 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));
if (preg_match('~<div class="viewdownloadbutton">\s*<a href="([^"]*tid=(\d+)[^"]*)~', $html, $linkmath)) {
// Найдена ссылка на загрузку файла - качаем
$tmpFl = mktmpfile($linkmath[2] . '.torrent');
$dwnUrl = html_entity_decode($linkmath[1]);
@copy($dwnUrl, $tmpFl, $context) or ( file_put_contents($tmpFl, file_get_contents(str_replace("&#38;", "&", $dwnUrl))));
$runComand = TORCLI . ' /DIRECTORY ' . $dirname . ' ' . escapeshellarg($tmpFl);

if (preg_match('~<td class="viewtorrentname">(.*?)</td>~', $html, $torFileName)) {
$torFileName = htmlentities2utf8($torFileName[1]);
$torFileNameWithoutExt = get_path_without_ext($torFileName);
if (strcasecmp($torname["name"], $torFileNameWithoutExt) != 0) {
$torname["name"] = $torFileNameWithoutExt;
}
}
if (isset($torname["oldname"])) {
my_rename($torname["oldname"], $dirnameRaw . "/" . trim($torname["name"] . ".$torname[ext]"));
return preg_match('~<div class="viewdownloadbutton">\s*<a href="([^"]*tid=(\d+)[^"]*)~', $html, $linkmath);
}

function renameAndDownload($dirInfo, $torname, $context, $linkmath, $html) {
$dirnameRaw = $dirInfo["dirnameRaw"];
$dirname = $dirInfo["dirname"];
// Найдена ссылка на загрузку файла - качаем
$tmpFl = mktmpfile($linkmath[2] . '.torrent');
$dwnUrl = html_entity_decode($linkmath[1]);
@copy($dwnUrl, $tmpFl, $context) or ( file_put_contents($tmpFl, file_get_contents(str_replace("&#38;", "&", $dwnUrl))));
$runComand = TORCLI . ' /DIRECTORY ' . $dirname . ' ' . escapeshellarg($tmpFl);

if (preg_match('~<td class="viewtorrentname">(.*?)</td>~', $html, $torFileName)) {
$torFileName = htmlentities2utf8($torFileName[1]);
$torFileNameWithoutExt = get_path_without_ext($torFileName);
if (strcasecmp($torname["name"], $torFileNameWithoutExt) != 0) {
$torname["name"] = $torFileNameWithoutExt;
}
}
if (isset($torname["oldname"])) {
my_rename($torname["oldname"], $dirnameRaw . "/" . trim($torname["name"] . ".$torname[ext]"));
}

sendToTorrent($runComand);
} else {
sendToTorrent($runComand);
}

$fullname = $dirnameRaw . "/$torname[name].ass";
if (!$torname["flag"] && ($nameFromAss = get_path_without_ext(tryGetNameFromASS($fullname))) && $nameFromAss != $torname['name']) {
$torname = array("name" => $nameFromAss, "flag" => 1, "oldname" => $fullname, "ext" => $torname["ext"], "needQuotes" => 1);
return handleSubtitleFile($dirInfo, $torname, $context);
} else {
if (!$torname["needQuotes"]) {
$torname["needQuotes"] = 1;
$torname["name"] = '"' . $torname["name"] . '"';
return handleSubtitleFile($dirInfo, $torname, $context);
function HasFoundAndDownloaded($dirInfo, $torname, $context) {
$linkmath = null;
$html = null;
if (hasFoundAnime($dirInfo, $torname, $context, $linkmath, $html)) {
renameAndDownload($dirInfo, $torname, $context, $linkmath, $html);
return true;
}
return false;
}

function renameWithNameFromSub($dirInfo, &$torname) {
$dirnameRaw = $dirInfo["dirnameRaw"];
$fullname = $dirnameRaw . "/$torname[name].ass";
$nameFromAss = get_path_without_ext(tryGetNameFromASS($fullname));
if ($nameFromAss != $torname['name']) {
$torname = array("name" => $nameFromAss, "oldname" => $fullname, "ext" => $torname["ext"]);
}
}

function handleSubtitleFile($dirInfo, $torname, $context) {

if (!HasFoundAndDownloaded($dirInfo, $torname, $context)) { //general
addQuotes($torname);
if (!HasFoundAndDownloaded($dirInfo, $torname, $context)) { //with quotes
removeQutes($torname);
renameWithNameFromSub($dirInfo, $torname);
if (!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>';
}
}
// Файл не найден - будем выводить ссылку на поиск
return '<p><a target="_blank" href="' . LINKSUFFIX . $linkname . '">' . $torname["name"] . '</a></p>';
}
}
}

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

function removeQutes(&$torname) {
$torname["name"] = trim($torname["name"], '"');
}

function pathWithEnv_callback($match) {
return getenv($match[1]);
}
Expand Down

0 comments on commit 5024d69

Please sign in to comment.